Plotting a figure with variables selected from popups
18 次查看(过去 30 天)
显示 更早的评论
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 个评论
Adam
2016-9-29
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 )
采纳的回答
Brandon Eidson
2016-10-4
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Migrate GUIDE Apps 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!