# How "hold on" function works?

62 ビュー (過去 30 日間)
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 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.
##### 0 件のコメント-2 件の古いコメントを表示-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
##### 0 件のコメント-2 件の古いコメントを表示-2 件の古いコメントを非表示

サインインしてコメントする。

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 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 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 Center および File ExchangeAnnotations についてさらに検索

### Community Treasure Hunt

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

Start Hunting!

Translated by