简体   繁体   中英

How to zoom in on figures when I use inputdlg in my code?

First, when I use inputdlg , Matlab does not let me to zoom in on my figure.

Second, when I enter a number using the command and try to convert it to cell using num2cell , I get this error: 'error using cellstr input must be a string'.

This is the piece of code that I am using:

No = cell2mat(inputdlg('Type in number: '));

(this is where I can't zoom in anymore!)

prompt = num2cell(1:2*No);
title = 'Numbers';
answer = inputdlg(prompt,title);

(this is where I get the error!)

Do you have any ideas how I can resolve these issues? I am using Matlab on a Mac system.

To programmatically zoom in or zoom out in a figure, you can use the zoom function.

An example could be:

% Create a Figure
my_fig=figure
% Plot something in the figure
plot(randi(10,10,1))
grid minor

% Get the zoom factor
zoom_factor=str2double(inputdlg('Type in number: '))

% Zoom the axes of the selected factor
zoom(my_fig,zoom_factor)

This will zoom in the plot by the value defined in the inputdlg .

The zoom will be action will be centered in the center of the axes, as it happens when you select the zoom icon in the figure toolbar and click on the centre of the figure.

Also, you can simply call

% Enable zooming
zoom(my_fig,'on')

to enable the zoom, this has the same effect than clicking on the zoom icon on the figure toolbar.

If you want to zoom a particular area of the graph, you can change the values of xlim and ylim .

With respect to the plot created in the example, you can use inputdlg to get the new limits and then update the plot

In this case, you have to input the 4 values in the inputdlg separated by a space (eg 2.5 5.5 5.5 7.5 )

% Get the axes handle
ax=gca;
% Store the original X anf Y Limit
orig_xlim=ax.XLim;
orig_ylim=ax.YLim;

zoom_factor=inputdlg('Type in new lim: ')
new_lim=str2num(char(zoom_factor))

ax.XLim=[new_lim(1) new_lim(2)];
ax.YLim=[new_lim(3) new_lim(4)];

Having stored the original values of the limits, you zoom out setting back them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM