How "hold on" function works?

58 次查看(过去 30 天)
Daniele Morello
Daniele Morello 2015-10-2
hi everyone. i have a problem with "hold on" function.
I need to plot many lines to "axes" in matlab gui. I'm using "hold on" to keep all the lines on the graph, but there's a problem. I read on "hold on documentation" that:
"MATLAB® adjusts axes limits, tick marks, and tick labels to display the full range of data."
but if i plot a set of data that has, for example, a range on the x-axis that goes from 1000 to 1200 i visualize the line on the graph, then, if i plot another set of data that goes from 3000 to 4000, the range of x-axis still remain the previous one (1000-1200), so i can't visualize the second line.
How can i hold all the lines on the graph that i plot with x-axis that automatically update the range?

回答(3 个)

Walter Roberson
Walter Roberson 2019-2-18
After you
hold on
you can do
ax = gca;
set(ax, 'XLimMode', 'auto', 'YLimMode', 'auto')
before you draw the new items.
Note: if you set XLimMode or YLimMode to auto after you have added the new data, then MATLAB will typically not rescan the data to determine what it should use.

Kevin Doherty
Kevin Doherty 2015-10-2
编辑:Kevin Doherty 2015-10-2
I'm not sure if there is a direct way to tell Matlab to update the axes to include all the plots. But you could use the axis function to return the limits of each plot. Then, after you've plotted everything, you could update the axes in order to view everything. For example,
t1 = 0:10;
x1 = 2*t1;
t2 = 5:15;
x2 = t2+1;
plot(t1,x1)
a(1,:) = axis; % save axis values as a row of the matrix a
hold on
plot(t2,x2)
a(2,:) = axis;
axis([min(a(:,1)) max(a(:,2)) min(a(:,3)) max(a(:,4))]); % update the current axes to include the full ranges of the axes from all the plots so far

Matlaber
Matlaber 2019-2-18
Is "hold on" function only work in plotting?
csvwrite('1test.csv',a, 0,1);
hold on;
csvwrite('1test.csv',b, 0,2);
It seemed not working as I want to input multiple input into a same/similar/one csv file
  2 个评论
Walter Roberson
Walter Roberson 2019-2-18
"hold on" has no connection at all to csvwrite().
csvwrite() can never append to an existing file. csvwrite() is a simplified interface to dlmwrite(). dlmwrite() itself includes an option to append.
Matlaber
Matlaber 2019-2-18
Thanks.
I had tried dlmwrite() with apend, but only can write in column and not row.
Link for this question is here

请先登录,再进行评论。

类别

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

标签

Community Treasure Hunt

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

Start Hunting!

Translated by