MATLAB Answers

Multiple plots in uitab/uitabgroup

108 views (last 30 days)
Suppose I have a uitabgroup with multiple uitabs. In each tab, a plot of some analyzed data is shown. How do I code the uitabgroup or each uitab so that, when the analyzed data is changed, the old graph REMAINS in the uitab to be plotted against. I am operating inside of a while loop. I've tried the 'hold on' function many different ways but to no avail
My code looks something like shown below. I'd paste the code directly but it is far too long. No other functionality than what is shown has been applied. Thanks in advance
h_figure_1 = figure(1);
h_tabgroup = uitabgroup(h_figure_1);
tab1 = uitab(h_tabgroup,'Title','y relative to z');
tab2 = uitab(h_tabgroup,'Title','y relative to z and n');
start =1;
while start == 1
% Ask user to upload data for z and n, given x and y
axes('parent',tab1)
plot(x,y/z)
axes('parent',tab2)
subplot(2,1,1)
plot(x,y/(z+n))
subplot(2,1,2)
plot(x,y/n)
start = input('Run again? 1 - yes, 0 - no');
end

  0 Comments

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 19 Oct 2017
Each of your
axes('parent',tab1)
and similar is creating a new axes and making it the default axes. The default position is used for the axes. You are not setting the axes color to 'none' so the new axes is "on top of" the old and hiding it.
A better structure:
h_figure_1 = figure(1);
h_tabgroup = uitabgroup(h_figure_1);
tab1 = uitab(h_tabgroup,'Title','y relative to z');
tab2 = uitab(h_tabgroup,'Title','y relative to z and n');
tab1_axes = axes('parent', tab1);
hold(tab1_axes, 'on');
tab2_axes1 = subplot(tab2, 2, 1, 1);
hold(tab2_axes1, 'on');
tab2_axes2 = sbplot(tab2, 2, 1, 2);
hold(tab2_axes2, 'on');
while true
% Ask user to upload data for z and n, given x and y
plot(tab1_axes, x, y/z);
plot(tab2_axes1, x, y/(z+n));
plot(tab2_axes2, x, y/n);
if input('Run again? 1 - yes, 0 - no') ~= 1; break; end
end

  5 Comments

Show 2 older comments
Seth Schoenhardt
Seth Schoenhardt on 21 Oct 2017
Worked like a charm. Thank you. One last thing, is there a way to create a Legend that holds for each iteration of the loop? I tried using
legend('-DynamicLegend')
However, that doesn't represent the data well enough as some plots show multiple functions for each iteration. I also tried using legend 'regularly' but it didn't display at all. I've placed the legend outside of the loop if you're wondering.
For a user unfamiliar with the shown data, they wouldn't know what they were looking at. Maybe the legend might look something like below?:
- Simulation data1
- Actual data1
-- Simulation data2
-- Actual data2
Walter Roberson
Walter Roberson on 21 Oct 2017
Do you want a legend for each plot() call, or for each line that gets drawn? You do not appear to be making sure that you get a consistent color for each line if multiple plots are being drawn.
Seth Schoenhardt
Seth Schoenhardt on 21 Oct 2017
I would like one for each line drawn, denoting which data set is represented

Sign in to comment.

More Answers (1)

Mehmed Saad
Mehmed Saad on 24 Jun 2020
Edited: Mehmed Saad on 24 Jun 2020

The Dumb Way

I found another way to do that (maybe it's not that efficient)
Basically subplot will plot all data on figure which is behind the tab. once it completed plotting just copy objects from figure
(once you are finished with a subplot, the property NextPlot must be set to replace and not add that is why hold off is necessary)
fh= figure;
utb = uitabgroup(fh);
for ii = 1:2
f = uitab(utb,'Title',sprintf('Tab %d',ii));
subplot(221)
plot(rand(1,10))
hold on;
plot(rand(1,10))
hold off; % necessary
subplot(222)
plot(rand(1,10))
hold on;
plot(rand(1,10))
hold off;% necessary
subplot(223)
plot(rand(1,10))
subplot(224)
plot(rand(1,10))
copyobj(fh.Children(2:end),f) %Copy subplots from figure to tab
end
delete(fh.Children(2:end)) % delete subplots from figure

  0 Comments

Sign in to comment.

Tags