Matlab function that accepts figure handle or file name as input?

9 views (last 30 days)
I want to write a matlab function that except one input argument, it can be a figure handle or file name to .fig file. If no input is provided I will invoke uigetfile. So far I can not get my function accept figure handle and modify the plot. Remember code should also check if the input is figure-handle or file name. Please help me with this.
function []=changexlim(varargin)
% This function changes xlim for various values and
narginchk(0,1)
if nargin==0
[file,path]=(uigetfile('*.fig','Select matlab figure'));
fil=fullfile(path,file);
openfig(fil)
xlim([0 5])
end
if nargin==1
check=ishandle(varargin)
if check==1
gca=get(varargin,'CurrentAxis')
xlim(gca,[0 5])
else if check==0
openfig(varargin)
xlim([0 5])
end
end
figure(varargin)
end

Accepted Answer

Walter Roberson
Walter Roberson on 10 Mar 2016
If you are using R2014b or later, it is recommended that you use isgraphics instead of ishandle()
Once you think you have a graphics handle, you should examine its type property to determine whether it is a figure. Or you could use ancestor(TheHandle,'figure') to find the figure in case you were handed some other graphics object.
In your code you are operating on varargin . You should instead be operating on varargin{1} for the first parameter.
The correct property name is CurrentAxes not CurrentAxis . And you need to be careful because it can be empty for various reasons.
You should never assign to a variable named "gca" as doing so interferes with using the gca function.
In the case where you are handed a string, check is false, and you openfig() . That is fine so far once you correct varargin to varargin{1} . But after the "end" of the "else", you assume you can figure(varargin) and even if you figure(varargin{1}) you have made the assumption that you can figure() a string .
What you should be doing is assigning the output of openfig() to a variable, and then figure() the variable.
  4 Comments
Walter Roberson
Walter Roberson on 10 Mar 2016
Naming a variable "path" can cause nasty problems as "path" is used by MATLAB to figure out where to find routines.
You cannot open a figure in a new window: a figure is a window. But you can make a copy of it:
groot = 0; %See note!
fh = copyobj(varargin{1}, groot);
Note: remove the assignment statement
groot = 0;
if you are using R2014b or later! R2014b and later already assigns it and may get confused if you re-assign it.
Samir
Samir on 10 Mar 2016
Thank you for the prompt answer. I will take care of path variable. Also I found a work around by temporary saving the figure as .fig. See code below.
if nargin==1
check=ishandle(varargin{1});
if check==1
fh=varargin{1};
disp('User supplied figure handle returned to fh')
fsave=tempname;
saveas(fh,fsave);
disp('Temporary figure saved')
fh=openfig(fsave);

Sign in to comment.

More Answers (0)

Categories

Find more on Graphics Object Programming 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!