I want to create a dropdown menu for different figures

40 次查看(过去 30 天)
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 个评论
Rik
Rik 2023-10-2
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.

请先登录,再进行评论。

回答(2 个)

Jinal
Jinal 2023-10-6
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:
  1. Open your App Designer and create a new app or open your existing app.
  2. Drag and drop a “DropDown” component and an “Axes” component from the "Common" section of the "Component Library" onto your app's canvas.
  3. 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.
  4. 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:
I hope this helps.
  3 个评论
Raul
Raul 2023-10-6
Hello i am reading the comments, i have a question about the code.
Since I have 4 plots in 4 figures. How can I display each figure.
I mean:
My code is something like:
figure(1) Subplot() Plot(x,y) Subplot(2) Plot(x2,y2)
Figure(2) Subplot() Plot() Subplot() Plot()
So, I have 4 figures with 4 plots. How can I display each figure with each selection and not display just 1 plot.
Thanks in advance
Rik
Rik 2023-10-6
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
Ender Gürleyen 2023-10-19
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.

类别

Help CenterFile Exchange 中查找有关 Annotations 的更多信息

标签

产品


版本

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by