Reset to Original View with zoom

85 ビュー (過去 30 日間)
Christophe
Christophe 2011 年 12 月 7 日
コメント済み: Martin 2021 年 8 月 16 日
I have a problem with the "Reset to Original View" command when I use the zoom:
If I enter:
figure; plot(rand(1,500)); h=gca; set(h,'xlim',[100 300]);
the curve if plotted between x=100 and x=300 and each time I use the "Reset to Original View" command after a zoom, the curve is still plotted between 100,300 and not 0,500 as it was originally.
Now, if I enter:
figure; plot(rand(1,500));
I zoom on the figure and I use the "Reset to Original View" command and then I enter:
h=gca; set(h,'xlim',[100 300]);
the curve if plotted between x=100 and x=300 but each time I use the "Reset to Original View" command after a zoom, the curve is plotted between 0,500.
How does this work?
How can I reset the curve between 0,500 without doing a zoom between the command "plot" and the command "set" ?
  1 件のコメント
Jerry Gregoire
Jerry Gregoire 2015 年 6 月 3 日
When you set 'xlim', the xlimmode is set to manual. This disables the ability to go 'reset' to the true extents of the plot. Do this set(gca,'xlimMode', 'auto') and similarly for y and/or z

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

回答 (1 件)

Martin
Martin 2018 年 2 月 13 日
編集済み: Martin 2021 年 8 月 16 日
Hello Christophe,
A little bit late..... but here is a solution. (Maybe I can help someone else with this problem.)
Add the next command before setting the limits:
resetplotview(hA,'InitializeCurrentView');
So the total code will look like:
figure;
plot(rand(1,500));
hA = gca;
% resetplotview(hA,'InitializeCurrentView'); %tested in 2013b
zoom(hA, 'reset'); %tested in 2019a
set(hA,'xlim',[100 300]);
Now you are able to use "Reset to Original View" to zoom out to the full axes (1 till 500).
NOTE: "resetplotview" is not a official documented function. Matlab can remove or change the function in the future.
EDIT: The solution of @Thomas Carpenter is working voor more recent versions.
  3 件のコメント
Thomas Carpenter
Thomas Carpenter 2021 年 8 月 15 日
For future readers, it seems the proper documented(-ish) way of doing this is to use the zoom() function.
Instead of the call to resetplotview(hA,...), do zoom(hA,'reset'). This will accomplish the same thing, setting the extents of the zoom when clicking "Reset to Original View", but using a documented interface.
The only slightly undocumented thing about using zoom in this way is that all the documentation says that the first argument should be a figure handle. In fact it works perfectly fine if the first argument is an axes handle ( zoom uses ancestor to find the appropriate figure and then uses the provided axis handle ) .
Martin
Martin 2021 年 8 月 16 日
thanks @Thomas Carpenter I update my solution

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

カテゴリ

Help Center および File ExchangeGrid Lines, Tick Values, and Labels についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by