2

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?

loudmummer
  • 544
  • 3
  • 19
  • 2
    No, you have to rename your script. In case there is some obscure way to do it, don't! you do not want to reference two different things by the same name. In the future always create a new script writing `edit *name*`, then if it opens an existing file, choose another name, if it opens a new file, you are good. – Nicky Mattsson Jun 13 '18 at 08:24
  • 2
    As far as I know, there is none. Shadowing built-in functions by variables (or in this case a GUI) will cause only problems, so I would suggest to rename your GUI no matter how many times it appears. In the end it will prove worth it and save you headaches. – Carlos Borau Jun 13 '18 at 08:24
  • 1
    @NickyMattsson the even easier option when creating new variables or functions is to just type the name and `(` and wait for MATLAB's tooltip for that function. If no tooltip appears after a second or so then it's not a built-in. – Wolfie Jun 13 '18 at 08:31
  • 2
    The best way to check would be to put it into the documentation, and see whether anything pops up, in case you have not installed certain toolboxes (cc @Wolfie). Anyhow, just rename the thing. Do it once and you're done with it, don't go hacking your way about to solve a problem which shouldn't exist in the first place. Solving it now *once* possibly prevents bigger problems in the future as well. – Adriaan Jun 13 '18 at 08:35
  • @Wolfie I do not see why it is easier. With your method you always have to check, with mine I only have to react in the few rare instances where I have a clash. Also I type `edit *name*` way faster than: clicking the "new script" button, pressing ctrl/cmd +s, typing the name and pressing enter. – Nicky Mattsson Jun 13 '18 at 08:37
  • Ah, well. Looks like I will have to rename it after all. I will remember the `edit *name*` command next time. I'd still like to know if there is a way to do it, though. – loudmummer Jun 13 '18 at 08:42
  • 1
    May be you can use the [`builtin`](https://uk.mathworks.com/help/matlab/ref/builtin.html) function call for the original Matlab function, and call your function directly to refer to your gui. However, I also think renaming your gui to not shadow built-in function is preferable. – Hoki Jun 13 '18 at 09:37
  • @Nicky I wasn't trying to be critical of your suggestion, it's probably easiest when creating a new function which is in its own file (or using `doc` instead of `edit`). The method I was suggesting would be entirely done in the command window or the file you're already typing in... I was trying to describe a habit I've developed of simply doing the bracket/tooltip check whenever I come up with a new name for anything (var/func/class/...), for instance you learn things like `flag` is a built-in function so shouldn't be used as a flag name! – Wolfie Jun 13 '18 at 09:47
  • 2
    @Hoki, `stack` is unfortunately not a builtin function per se. It is just an .m file made by Mathworks. – Nicky Mattsson Jun 13 '18 at 10:31
  • 1
    There are good arguments here about not writing a function with the same name as a MATLAB function, but these things are impossible to avoid in the long run: MATLAB keeps getting new functions. Name clashes will occur. It's not possible to keep renaming your own functions every time MATLAB decides to expand on their own toolbox. MATLAB has (fairly) recently added namespaces. These are directories that start with a `+`. Use them! – Cris Luengo Jun 13 '18 at 16:55
  • @CrisLuengo namespaces sounds like a lovely idea, thanks! – loudmummer Jun 14 '18 at 05:16

2 Answers2

3

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>.

Adriaan
  • 17,741
  • 7
  • 42
  • 75
3

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.

Nicky Mattsson
  • 3,052
  • 12
  • 28