Matlab function that accepts figure handle or file name as input?
조회 수: 5 (최근 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!