问题
I created a GUI called "stack" in MATLAB. It has a .m
file associated with it. This GUI is called on multiple occasions by another GUI in the same folder.
Now I discovered that "stack" is a built-in function in MATLAB which I need to use for something else in the same working directory. All calls to the stack function somehow invoke the GUI by calling the stack.m
script.
I do not want to rename this because it is used in many places.
Is there a way to use the built-in function without needing to rename? Some way to reference the function and the script separately?
回答1:
Disclaimer: Please, please, please, do not do this.
Assuming that your own stack.m
is only in the search path because it is in the current folder, then the easiest fix is to create some dummy subfolder, navigate to it, execute Matlabs stack
function (which is the only stack
in the current searchpath) and navigate back.
Here I have exemplified it with magic
:
function a= magic
n=5;
cd dummy
a= magic(n);
cd ..
where dummy
is the name of the subfolder.
回答2:
A slight modification of Nicky's answer for the sake of repeatability: before navigating to the map where your stack.m
GUI is stored, run
builtinStack = @stack();
which creates a function handle. This way you can call builtinStack()
like the MATLAB function is supposed to be called, without having to cd
out of the directory every time you want to use it.
The use of builtin, as suggested by hoki doesn't work, since built-in functions are defined as
...Functions such as "ind2sub", "sub2ind", etc. are not MATLAB built-in functions.... Those functions that are shipped with MATLAB but are not defined as built-in functions can be referred to as "MATLAB functions" ...
As answered by MathWorks Technical Support. This means that functions like stack
aren't built-in in the sense that they are build in a different language, compiled and then called from MATLAB, but are actually written in MATLAB and are shipped with the release. The main way to check this is typing edit <functionname>
; when showing only comments the function is a built-in as defined by TMW, when it shows MATLAB code as well, like stack
, it's not a built-in as per the above definition.
An example of a built-in function is sum, whose associated .m-file looks as follows:
%SUM Sum of elements.
% S = SUM(X) is the sum of the elements of the vector X. If X is a matrix,
% S is a row vector with the sum over each column. For N-D arrays,
% SUM(X) operates along the first non-singleton dimension.
%
% S = SUM(X,DIM) sums along the dimension DIM.
%
% S = SUM(...,TYPE) specifies the type in which the
% sum is performed, and the type of S. Available options are:
%
% 'double' - S has class double for any input X
% 'native' - S has the same class as X
% 'default' - If X is floating point, that is double or single,
% S has the same class as X. If X is not floating point,
% S has class double.
%
% S = SUM(...,NANFLAG) specifies how NaN (Not-A-Number) values are
% treated. The default is 'includenan':
%
% 'includenan' - the sum of a vector containing NaN values is also NaN.
% 'omitnan' - the sum of a vector containing NaN values
% is the sum of all its non-NaN elements. If all
% elements are NaN, the result is 0.
%
% Examples:
% X = [0 1 2; 3 4 5]
% sum(X, 1)
% sum(X, 2)
%
% X = int8(1:20)
% sum(X) % returns double(210), accumulates in double
% sum(X,'native') % returns int8(127), because it accumulates in
% % int8 but overflows and saturates.
%
% See also PROD, CUMSUM, DIFF, ACCUMARRAY, ISFLOAT.
% Copyright 1984-2015 The MathWorks, Inc.
% Built-in function.
i.e. it can also be seen from the last line that this is a built-in as per definition. Note that everything contained in the first 'comment' is seen when help sum
is typed; in the sense that an empty line breaks the help file. The copyright and built-in information do thus not show up when simply typing help sum
on the command line, so for checking whether a function is a built-in you need edit <functionname>
.
来源:https://stackoverflow.com/questions/50832169/how-do-i-resolve-name-conflict-in-matlab