Clear Filters
Clear Filters

Plotting a figure with variables selected from popups

15 views (last 30 days)
Hello dear community,
for the moment I'm looking for a solution to make the my plots better looking than plotting every figure in separate windows e.g.
I have two solutions in mind, which I don't know are the most optimal ones.
1. Solution. Creating a figure window from where the user can choose of the x and y variable that has to be plotted. The figure stays open and the the user can change the inputs as he wants.
2. Creating a GUI where all the different cases i provide of plotting (e.g. "load vs. time", "extension vs. Time" etc.) The user can click on the buttons and the figure will appear.
Since I'm quiet new to the GUI, I'm not really good at understanding it fully (e.g. callback functions,..)
The code i wrote now, for what i had in mind for the first solution looks like the following:
if true
function myplot(out)
% out is a struct containing all informations from the measurement
% Create a figure and axes
header=fieldnames(out);
figure;
ax = axes('Units','pixels');
% Create pop-up menu X-Value
popup_x = uicontrol('Style', 'popup',...
'String',header,...
'Position', [20 340 100 50],...
'Callback', @changex);
% Create pop-up menu (Y-Value
popup_y = uicontrol('Style', 'popup',...
'String', header,...
'Position', [120 340 100 50],...
'Callback', @changey)
end
function changex(source,eventdata,out)
% get the x id
x = get(popup_x,'Value')
% get the attribute id
y = get(popup_y,'Value')
val = source.Value;
header = source.String;
plot(out.(header{val}),out.(header{val}));
end
I know the code is messy, i left some things that i tried inside of it, but I'm not sure how to use it, I'm really sorry...
I thank you all in advance for your help :)
  1 Comment
Adam
Adam on 29 Sep 2016
What is the problem exactly with the code you have now that you are asking about specifically?
One thing I notice is that 'out' will not be found as an input argument as you currently have it setup.
Either make changex a nested function, in which case it will already have access to out so you won't need to pass it in or you will need to change your callback syntax to pass out to it - e.g.
'Callback', @(src,evt) changex( src,evt,out )

Sign in to comment.

Accepted Answer

Brandon Eidson
Brandon Eidson on 4 Oct 2016
Hey Darko,
A good place to read to get started with app callbacks is at the document below. That will show you the different kind of callbacks you can implement and different ways to define what you want the callback to perform.
I wrote a brief example showing one way to implement callbacks that I think reflects your second proposed solution.
t = linspace(0,10e-3, 1000);
v = 3*cos(377*t);
p = 5*cos(2*377*t + pi/4);
fig = figure;
ax = axes;
ax.Position = [0.1 0.25 0.85 0.7];
line = plot(t,v); % initialize plot to v(t) vs. t
vButton = uicontrol(fig,'Style','pushbutton');
vButton.Units = 'Normalized';
vButton.Position = [0.25 0.05 0.1 0.1];
vButton.String = 'v(t) vs. t';
vButton.Callback = 'line.YData = v;'; % have callback update y-axis data
pButton = uicontrol(fig,'Style','pushbutton');
pButton.Units = 'Normalized';
pButton.Position = [0.65 0.05 0.1 0.1];
pButton.String = 'p(t) vs. t';
pButton.Callback = 'line.YData = p;'; % have callback update y-axis data
If you wanted your callbacks to also update the Y-axis label, legend, etc., you will probably want to write a separate function that your callbacks calls. As Adam mentioned, you will need to pass to the function a line handle, the relevant data and object handles.
  1 Comment
Darko Hündgen
Darko Hündgen on 18 Oct 2016
Hello Brandon,
thank you very much for your answer. It was really useful since I got it to work, by looking at your example of the syntax input. Now I have both options at my figure, the dropdown menu to chose the displayed parameters, as well as the buttons you suggested
header =fieldnames(out);
popup_x = uicontrol('Style', 'popup',...
'String', header,...
'Position', [120 340 100 50],...
'Callback', 'line.XData = out.(header{popup_x.Value});')
popup_y = uicontrol('Style', 'popup',...
'String', header,...
'Position', [250 340 100 50],...
'Callback', 'line.YData = out.(header{popup_y.Value});')
Thank you very much :)

Sign in to comment.

More Answers (0)

Categories

Find more on Interactive Control and Callbacks 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!