i want to connect different points on a 2d graph paper using matlab gui and want to show the result of total distance from a point to a different point what would be the possible way to do this in matlab gui
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
w
0 comentarios
Respuestas (1)
BhaTTa
el 2 de Sept. de 2024
Editada: BhaTTa
el 2 de Sept. de 2024
@mohit kumar, you can implement it using MATLAB App Designer , below I have provided a complete implementation using MATLAB App Designer. This example allows users to select points on a plot and calculates the total distance between them.
Below is the screenshot of the Design View:
Code:
classdef app1 < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
DistanceLabel matlab.ui.control.Label
SelectPointsButton matlab.ui.control.Button
UIAxes matlab.ui.control.UIAxes
end
properties (Access = private)
Points double = [] % Description
end
% Callbacks that handle component events
methods (Access = private)
% Callback function: not associated with a component
function ButtonPushed(app, event)
app.AddButton.Text = 'Clicked!';
end
% Callback function: not associated with a component
function AddButtonPushed(app, event)
num1 = app.EditField.Value;
num2 = app.EditField2.Value;
% Calculate the sum
result = num1 + num2;
% Display the result in the label
app.DistanceLabel.Text = ['Result: ', num2str(result)];
end
% Button pushed function: SelectPointsButton
function SelectPointsButtonPushed(app, event)
% Clear previous points
app.Points = [];
cla(app.UIAxes);
% Allow user to select points
hold(app.UIAxes, 'on');
for i = 1:2 % Allow selection of 2 points
[x, y] = ginput(1); % Get point from user click
plot(app.UIAxes, x, y, 'ro', 'MarkerSize', 8, 'MarkerFaceColor', 'r');
app.Points = [app.Points; x, y];
% Draw lines between points
if size(app.Points, 1) > 1
plot(app.UIAxes, app.Points(end-1:end, 1), app.Points(end-1:end, 2), 'b-');
end
end
hold(app.UIAxes, 'off');
% Calculate total distance
totalDistance = sum(sqrt(sum(diff(app.Points).^2, 2)));
app.DistanceLabel.Text = sprintf('Total Distance: %.2f', totalDistance);
end
end
% Component initialization
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
% Create UIFigure and hide until all components are created
app.UIFigure = uifigure('Visible', 'off');
app.UIFigure.Position = [100 100 640 480];
app.UIFigure.Name = 'MATLAB App';
% Create UIAxes
app.UIAxes = uiaxes(app.UIFigure);
title(app.UIAxes, 'Title')
xlabel(app.UIAxes, 'X')
ylabel(app.UIAxes, 'Y')
zlabel(app.UIAxes, 'Z')
app.UIAxes.Position = [182 203 300 185];
% Create SelectPointsButton
app.SelectPointsButton = uibutton(app.UIFigure, 'push');
app.SelectPointsButton.ButtonPushedFcn = createCallbackFcn(app, @SelectPointsButtonPushed, true);
app.SelectPointsButton.Position = [394 162 100 22];
app.SelectPointsButton.Text = 'SelectPoints';
% Create DistanceLabel
app.DistanceLabel = uilabel(app.UIFigure);
app.DistanceLabel.Position = [234 103 289 22];
app.DistanceLabel.Text = 'DistanceLabel';
% Show the figure after all components are created
app.UIFigure.Visible = 'on';
end
end
% App creation and deletion
methods (Access = public)
% Construct app
function app = app1
% Create UIFigure and components
createComponents(app)
% Register the app with App Designer
registerApp(app, app.UIFigure)
if nargout == 0
clear app
end
end
% Code that executes before app deletion
function delete(app)
% Delete UIFigure when app is deleted
delete(app.UIFigure)
end
end
end
0 comentarios
Ver también
Categorías
Más información sobre 2-D and 3-D Plots en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!