How to plot only some part of a vector based on the index of a knob in app designer

6 views (last 30 days)
Hi guys,
im new to matlab and even more to app designer but i need to make an app with a UIAxes and a discrete knob, since i have a very long code and uploading it here i'm sure would be painful and boring for y'all i'll make an example of my problem.
Let's say i have a UIAxes and i want to plot a vector here of lenght N (very long), and i have a discrete Knob that have, as an example, 4 values (1, 2, 3, 4). The vector is divided in 4 parts, not equal, and i have the 3 indexes of where it's sliptted and they are stored in a vector ind=[10;25;52].
The result i want is:
  1. At first it plots the entire vector as i open the app.
  2. If i move the knob the graph has to change (if Knob.Value=1 the portion of the vector displayed sould be from 0 to ind(1), if is more than 1 and less than 4 the portion should be from ind(i-1) to ind(i), and if it's 4 it has to be from the last index to the end).
  3. A "RES" button resets the graph showing the full vector.
  4. The vector cannot be modified
Thank you all for your help and your time, feel free to ask anything or give any general matlab advice. R.

Accepted Answer

Austin M. Weber
Austin M. Weber on 14 Feb 2024
I have made an example of such an app using MATLAB Online (see the attached file).
Just to walk you through the steps that I took to make the app:
  1. Create a new app
  2. Save the app and give it a good name (I named mine answer.mlapp)
  3. Drag-and-drop a UIAxes component, a discrete knob component, and a button component into the Design View
  4. Click on the knob and (in the Component Browser on the right side of the screen) change the Items and ItemsData values to 1,2,3,4
  5. Double-click the button and change the name to "Reset" or to whatever you like
  6. Switch from Design View to Code View
  7. Click the red P+ button at the top of the screen to add a private property. This is where you will define the x-values, the vector, and the index for your app. It might look something like this:
properties (Access = private)
xvals = linspace(0,pi*4,80);
vector = sin(linspace(0,pi*4,80));
ind = [10;25;52];
end
  1. Go back to the Design View, and, in the Component Browser (again, on the right side of the screen) right-click the name of your app (at the top of the Component Browser). Navigate to Callbacks -> Go to startupFcn callback
  2. Define your callback function so that upon opening the app the entire vector will be plotted. It make look like this:
function startupFcn(app)
plot(app.UIAxes,app.xvals,app.vector)
xlim(app.UIAxes,[0 4*pi])
end
  1. Return to the Design View and right-click the knob. Navigate to Callbacks -> Go to KnobValueChanged callback
  2. Define your callback function so that whenever the knob is changed the plot will update to match the index range of the knob value. It may look like this:
function KnobValueChanged(app, event)
value = app.Knob.Value;
if value == 1
plot(app.UIAxes,app.xvals(1:app.ind(1)),app.vector(1:app.ind(1)))
elseif value == 2
plot(app.UIAxes,app.xvals(app.ind(1):app.ind(2)),app.vector(app.ind(1):app.ind(2)))
elseif value == 3
plot(app.UIAxes,app.xvals(app.ind(2):app.ind(3)),app.vector(app.ind(2):app.ind(3)))
else
plot(app.UIAxes,app.xvals(app.ind(3):end),app.vector(app.ind(3):end))
end
% Keep the x-axis limits constant
xlim(app.UIAxes,[0 4*pi])
end
  1. Return to the Code View again. Now right-click your button and navigate to Callbacks -> Go to ResetButtonPushed callback
  2. Define your callback to be identical to the startupFcn from above:
function
plot(app.UIAxes,app.xvals,app.vector)
xlim(app.UIAxes,[0 4*pi])
end
  2 Comments
Riccardo
Riccardo on 14 Feb 2024
Thank you, that's awsome, but if the vector ind is unknown? I mean, if don't know the vector's length i have to change the if/elseif with 3 options: if it's in the first position of the Knob it has to show from 0 to the first index, if it's the last knob's value it has to show from the last index to the end, otherwise it has to show from ind(i-1) to ind(i) how can i do it?
Austin M. Weber
Austin M. Weber on 14 Feb 2024
I'm not sure I follow. In your original question you said that you know ind is [10,25,52]. If that is the case, then all of the code I have provided above does exactly what you are asking. If the knob is set to 1 then the plot will only display the values in vector from 0 to the first index; if the knob is set to 2 then the plot will only display the values in vector from the first index to the second index; if the knob is set to 3 then the plot will only display the values in vector from the second index to the third index; and if the knob is set to 4 then the plot will only display the values in vector from the third index to the end. You don't have to modify the if/esle statements at all.

Sign in to comment.

More Answers (0)

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!