Clear Filters
Clear Filters

How "hold on" function works?

78 views (last 30 days)
Daniele Morello
Daniele Morello on 2 Oct 2015
Answered: Walter Roberson on 18 Feb 2019
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?

Answers (3)

Walter Roberson
Walter Roberson on 18 Feb 2019
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 on 2 Oct 2015
Edited: Kevin Doherty on 2 Oct 2015
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;
a(1,:) = axis; % save axis values as a row of the matrix a
hold on
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 on 18 Feb 2019
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
Walter Roberson
Walter Roberson on 18 Feb 2019
"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 on 18 Feb 2019
I had tried dlmwrite() with apend, but only can write in column and not row.
Link for this question is here

Sign in to comment.


Find more on Labels and Annotations 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!