I want to create a dropdown menu for different figures
42 views (last 30 days)
Show older comments
Hello everyone.
I am doing a projecto where I need to get 4 different figures, each figure have 4 different plots. (With subplots)
I think it seems pretty bad to display all the four figures at once.
I have something like this:
figure(1)
subplot(2,2,1)
plot(x1,y1)
subplot(2,2,2)
plot(x2,y2)
figure(2)
subplot(2,2,1)
plot(x3,y3)
And so on, until i get the 4 figures with 4 plots on each figure.
I was wondering if there is a possiblity where I can get a dropdown menu or Buttons or something simple
just to display each graph when the person wants to see it. I haven't used GUI before so the more simpler the code
better forme.
Thanks and I really appreciate your kindness and help.
2 Comments
Rik
on 2 Oct 2023
A GUI in Matlab is nothing special, it simply provides an interface between the user and the functions you write separately. For general advice and examples for how to create a GUI (and avoid using GUIDE), have look at this thread.
Answers (2)
Jinal
on 6 Oct 2023
Hello Raul,
As per my understanding, you wish to create a GUI with a dropdown to display multiple plots.
The “DropDown” component of App Designer can be used to create a dropdown to display multiple plots. For each “Value” of the “DropDown” a plot can be created using an “Axes” component.
The stepwise workflow for creating a dropdown menu to display different plots is given below:
- Open your App Designer and create a new app or open your existing app.
- Drag and drop a “DropDown” component and an “Axes” component from the "Common" section of the "Component Library" onto your app's canvas.
- Create a callback function for the dropdown menu. To do so, right-click on the dropdown menu, select "View Callbacks," and then click on the 'ValueChangedFcn' for the dropdown menu.
- Refer the following sample code of Value changed callback to create different plots when different “Value” is selected for the “DropDown” component:
% Value changed function: DropDown
function DropDownValueChanged(app, event)
selectedOption = app.DropDown.Value;
% Define data for plots
x1=[1 2 3 4];
y1=[1 2 3 4];
x2=[1 2 3 4];
y2=[4 3 2 1];
% Plot the selected graph based on the user's choice
if selectedOption == "Graph 1"
plot(app.UIAxes1, x1, y1);
elseif selectedOption == "Graph 2"
plot(app.UIAxes1, x2, y2);
% Add more conditions for other graphs
end
end
To know more about developing apps using App Designer, refer the following link:
To know more about “DropDown”, “Axes”, and “Callbacks” in App Designer, refer the following links respectively:
- https://in.mathworks.com/help/matlab/ref/matlab.ui.control.dropdown-properties.html
- https://in.mathworks.com/help/matlab/ref/matlab.ui.control.uiaxes-properties.html
- https://in.mathworks.com/help/matlab/creating_guis/write-callbacks-for-gui-in-app-designer.html
I hope this helps.
3 Comments
Rik
on 6 Oct 2023
The solution is to not have different figure, but just one. You can create multiple axes and control the visibility with the callback of the dropdown.
Ender Gürleyen
on 19 Oct 2023
fig(1) = figure; % First figure
fig(1).WindowStyle = 'docked'; % Make it appear in a tab
fig(2) = figure; % Second Figure
fig(2).WindowStyle = 'docked';
Using the figure property WindowStyle you can create different Tabs of figures. This way you can have multiple figures in a single window with different content which can improve visibility.
This does not use a dropdown but can also be helpful.
0 Comments
See Also
Categories
Find more on Annotations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!