フィルターのクリア

How to use a slider's value as an index to plot a dot that moves in App Designer

21 ビュー (過去 30 日間)
Hi guys,
i'm new to App Designer and i want to plot two graphs and use one slider, to do so i wrote a code that basically plot both graphs (the first one is X and Y that are very long vectors, and the second one is r that is as long as the other two) when the PLOT button is pushed and then sets the limits of the 2 graphs. Then when i take the slider value and use it as a zoom in the x axis of the second graph, then i use the value of the slider as an index for plotting a single dot that should be moving along the first graph that should be already plotted.
Now, everything works fine, until it reaches the last line where there's an error that says "Array indices must be positive integers or logical values." so then i thought that it's the division that i do when calculating n so i rounded it as you all can see, but the error it's still there, please help me and also give me advices on how to get better at coding.
Thank you very much, R.
classdef app7 < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
Slider matlab.ui.control.Slider
SliderLabel matlab.ui.control.Label
YAWRATELabel matlab.ui.control.Label
TRACKLabel matlab.ui.control.Label
HOMEButton matlab.ui.control.Button
PLOTButton matlab.ui.control.Button
UIAxes2 matlab.ui.control.UIAxes
UIAxes matlab.ui.control.UIAxes
end
% Callbacks that handle component events
methods (Access = private)
% Button pushed function: HOMEButton
function HOMEButtonPushed(app, event)
delete(app.UIFigure);
end
% Button pushed function: PLOTButton
function PLOTButtonPushed(app, event)
X=evalin('base','X');
Y=evalin('base','Y');
r=evalin('base','yaw_rate');
xlim(app.UIAxes, [min(X), max(X)]);
ylim(app.UIAxes, [min(Y), max(Y)]);
xlim(app.UIAxes2, [0, length(r)]);
ylim(app.UIAxes2, [min(r), max(r)]);
plot(app.UIAxes2, r);
plot(app.UIAxes,X,Y)
end
% Value changing function: Slider
function SliderValueChanging(app, event)
X=evalin('base','X');
Y=evalin('base','Y');
v = event.Value;
n = length(X)*v/1000;
round(n);
xlim(app.UIAxes2, [n, 200+n]);
plot(app.UIAxes, X(v), Y(v), '.', 'LineWidth',2);
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 852 600];
app.UIFigure.Name = 'MATLAB App';
% Create UIAxes
app.UIAxes = uiaxes(app.UIFigure);
title(app.UIAxes, 'Track')
xlabel(app.UIAxes, 'X')
ylabel(app.UIAxes, 'Y')
zlabel(app.UIAxes, 'Z')
app.UIAxes.Position = [1 256 853 345];
% Create UIAxes2
app.UIAxes2 = uiaxes(app.UIFigure);
title(app.UIAxes2, 'Yaw Rate')
ylabel(app.UIAxes2, 'rad/sec')
zlabel(app.UIAxes2, 'Z')
app.UIAxes2.Position = [1 1 441 256];
% Create PLOTButton
app.PLOTButton = uibutton(app.UIFigure, 'push');
app.PLOTButton.ButtonPushedFcn = createCallbackFcn(app, @PLOTButtonPushed, true);
app.PLOTButton.FontSize = 20;
app.PLOTButton.Position = [469 185 96 50];
app.PLOTButton.Text = 'PLOT';
% Create HOMEButton
app.HOMEButton = uibutton(app.UIFigure, 'push');
app.HOMEButton.ButtonPushedFcn = createCallbackFcn(app, @HOMEButtonPushed, true);
app.HOMEButton.FontSize = 20;
app.HOMEButton.Position = [717 36 96 50];
app.HOMEButton.Text = 'HOME';
% Create TRACKLabel
app.TRACKLabel = uilabel(app.UIFigure);
app.TRACKLabel.HorizontalAlignment = 'center';
app.TRACKLabel.FontSize = 20;
app.TRACKLabel.FontWeight = 'bold';
app.TRACKLabel.Position = [727 197 75 26];
app.TRACKLabel.Text = 'TRACK';
% Create YAWRATELabel
app.YAWRATELabel = uilabel(app.UIFigure);
app.YAWRATELabel.HorizontalAlignment = 'center';
app.YAWRATELabel.FontSize = 20;
app.YAWRATELabel.FontWeight = 'bold';
app.YAWRATELabel.Position = [463 48 107 26];
app.YAWRATELabel.Text = 'YAW RATE';
% Create SliderLabel
app.SliderLabel = uilabel(app.UIFigure);
app.SliderLabel.HorizontalAlignment = 'right';
app.SliderLabel.Position = [469 140 36 22];
app.SliderLabel.Text = 'Slider';
% Create Slider
app.Slider = uislider(app.UIFigure);
app.Slider.Limits = [0 1000];
app.Slider.ValueChangingFcn = createCallbackFcn(app, @SliderValueChanging, true);
app.Slider.Position = [526 149 260 3];
% 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 = app7
% 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

採用された回答

Walter Roberson
Walter Roberson 2024 年 1 月 26 日
round(n);
That has no effect, as you are not assigning the result to anything.
But it is unnecessary anyhow: you use n as the xlim and non-integer xlim is not a problem.
You should be doing
v = round(v);
However... your slider limits are [0 1000] and you will have a problem when the slider is 0.
I suggest that you change the slider limits to [1 1000]
  2 件のコメント
Riccardo
Riccardo 2024 年 1 月 27 日
編集済み: Riccardo 2024 年 1 月 27 日
Thank you, that was very useful, but now when i move the slider it shows every dot that i do, how can i have only one dot moving along my first graph? Thank you again, R.
Walter Roberson
Walter Roberson 2024 年 1 月 27 日
I do not see any reason in your posted code why it would draw multiple points. But I suspect that you must have turned hold on on the axes at some point: your current arrangement would remove the axes labels every time you plotted anything.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeDevelop uifigure-Based Apps についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by