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

조회 수: 5 (최근 30일)
Samir
Samir 2016년 3월 10일
댓글: Samir 2016년 3월 10일
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

채택된 답변

Walter Roberson
Walter Roberson 2016년 3월 10일
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
Walter Roberson
Walter Roberson 2016년 3월 10일
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 2016년 3월 10일
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);

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Graphics Object Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by