Matlab function that accepts figure handle or file name as input?
9 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
Walter Roberson
2016-3-10
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
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Graphics Object Programming 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!