I am looking for a workaround for the following problem:
Create a script foo.m containing fun = #(x)(x*x)
Run foo. This creates the variable fun.
Delete foo.m
Try running fun(2).
In recent versions of MATLAB (I am using R2019b) this results in an error:
Previously accessible file "foo.m" is now inaccessible.
Somehow, the anonymous function is tied to the file in which it was defined.
Is it possible to somehow "detach" it so that it would continue working even after the file was deleted?
For those curious why I need this, it is for fixing MATLink, the Mathematica/MATLAB interface, for recent versions of MATLAB.
You can use func2str and str2func to construct a new function handle that will work:
more_fun = str2func(func2str(fun));
more_fun(2)
Note that after this, fun can still not be found, but you can also assign to fun directly to make it accessible again.
Edit: just found a (documented) limitation: if the anonymous function uses outside variables, this method will not work, because
Function handles created using str2func do not have access to variables outside of their local workspace or to nested functions. If your function handle contains these variables or functions, MATLABĀ® throws an error when you invoke the handle.
Related
I am having a problem with the findpeaks function, this function is in the signal processing toolbox and also the program has another version of it (user defined function). I need to call the on in the signal processing toolbox not the user defined one, also I can't rename the user defined function for many reasons. Can anyone help me in calling the toolbox function.
The precedence order used by MATLAB is described in their help pages. It states that functions in the current folder (9.) are preferred over functions elsewhere in the path (10.). Then, the first appearance of the function in the path is chosen. This allows for a number of possible solutions:
1. cd to folder
A very simple method is simply to change the current workspace directory to the folder of the function you need to call, i.e. cd either to the place where your user-defined function is, or cd to the toolbox path. Note: This is rather inelegant, but probably sometimes the simplest solution.
2. Reorder path
As mentioned, MATLAB choses the first occurence of the function in the path. You can thus re-sort the path variable, so the folder where your user-defined function is, appears last. The path variable can be viewed and manipulated using the path function. Note: Then you can only call the toolbox function. Otherwise you'd have to resort the path again.
3. Function handles
If you need to be able to call both functions, it can be useful to create a function handle for both versions. For that, you have to cd into the folders where the functions are defined and create a new handle there:
cd('path/to/userdefined/function')
userFindPeaks = #findpeaks;
cd('path/to/MATLAB/installation/toolbox/signal/signal')
toolboxFindPeaks = #findpeaks;
You can then call the functions using feval.
Of course, as Adriaan mentions in the comments, it is best not to use the names of already defined functions for your own functions or for variable names.
I just came here looking for the same thing... I ended up using builtin.
https://uk.mathworks.com/help/matlab/ref/builtin.html
[y1,...,yn] = builtin(function,x1,...,xn)
#arr_sea actually posted a link in one of the folded comments which uses this function in a different context.
So I have been trying to call a button callback function in test_gui.m function in non-gui m file say test2.m.
But I fail all the time. I tried using findall or findobj but still I get a problem. could you tell me if there is anything I have to do?
You cannot access any functions in an m file except for the function by which the file is named.
For example, you have an m file named func.m which contains functions func(), helper_func1(), and helper_func2(); only func() will be available to any code outside of that file. See http://www.mathworks.com/help/matlab/ref/function.html: "Local functions are only available to other functions within the same file."
What you want is to create a new file named helper_func1.m containing helper_func1() and then call that function from test2.m as well as inside test_gui.m.
I am using Matlab R2013a and I am trying to use the 'who' function within a function to retrieve a list of variables that begin with a name.
Let's say I have a list of variable in my workspace as follows:
a = 1
a_2 = 2
a_3 = 3
when I run this:
who('a*');
it works fine.
But when I run the same thing inside a function like this:
function someFunction()
who('a*');
end
or
function someFunction()
disp(who('a*'));
end
It doesn't. No error, just no output.
If I had saved those variables in a Matlab file called
myVariables.mat
and run this within the same function like so:
function someFunction()
who('a*','myVariables');
end
It still doesn't work.
I can understand why the first might not work because of scope, but specifying the file to run the 'who' function on should work... what am I missing?
Any help would be appreciated.
Regards
Diaa
As mentioned by #Daniel, the workspace of a function is separate from the base workspace. There are two ways you could use who inside a m-file to inspect the base workspace:
Use a script instead of a function (i.e. omit the function- line; launch the script by its file name as you do with a function): A script shares the base workspace, and thus, who will be able to see all your variables.
Use evalin: evalin('base','who')
You are trying to access variables within a function. Only the input arguments and global variables are visible within a function. You have to do something like:
function someFunction(a1,a2)
who('a*');
end
If you are really trying to use dynamic variable names, I would strongly recommend to change your design.
What do I want?
I am looking for a way to detect all points in my code where a specific function is called.
Why do I want it?
Some examples:
Some output comes out sorted or randomized, and I want to know where this happens
I am considering to change/overload a function and want to know in which part of my code this could have impact
What have I tried?
I tried placing a breakpoint in the file that was called. This only works for non builtin functions which are called from short running code that always executes everything.
I tried 'find files', this way I can easily find direct calls to sort but it is not so easy to find a call to sort invoked by unique for example.
I have tried depfun, it tells me:
whether something will be called
from where non-builtin functions will be called
I thought of overloading the builtin function, but feels like a last resort for me as I am afraid to make a mess. | Edit: Also it probably won't help due to function precedence.
The question
What is the best way to track all potential (in)direct function calls from a specific function to a specific (built-in)function.
I don't exactly understand your use case, but I guess most of the information you want can be obtained using dbstack, which gives you the call-stack of all the parent functions calling a certain function. I think the easiest way is to overload built-in functions something like this (I tried to overload min):
function varargout = min(varargin)
% print info before function call
disp('Wrapped function called with inputs:')
disp(varargin)
[stack,I] = dbstack();
disp('Call stack:')
for i=1:length(stack)
fprintf('level %i: called from line %i in file %s\n', ...
i, stack(i).line, stack(i).file);
end
% call original function
[varargout{1:nargout}] = builtin('min', varargin{:});
% print info after function call
disp('Result of wrapped function:')
disp(varargout)
I tried to test this, but I could not make it work unfortunately, matlab keeps on using the original function, even after playing a lot with addpath. Not sure what I did wrong there, but I hope this gets you started ...
Built-in functions take precedence over functions in local folder or in path. There are two ways you can overload a built-in for direct calls from your own code. By putting your function in a private folder under the same directory where your other MATLAB functions are. This is easier if you are not already using private folder. You can rename your private folder once you are done investigating.
Another way is to use packages and importing them. You put all your override functions in a folder (e.g. +do_not_use). Then in the function where you suspect built-in calls are made add the line "import do_not_use.*;". This will make calls go to the functions in +do_not_use directory first. Once you are done checking you can use "clear import" to clear all imports. This is not easy to use if you have too many functions and do not know in which function you need to add import.
In addition to this, for each of the function you need to follow Bas Swinckels answer for the function body.
Function precedence order.
Those two methods does not work for indirect calls which are not from your own code. For indirect calls I can only think of one way where you create your own class based on built-in type. For example, if you work only on double precision types, you need to create your own class which inherits from double and override the methods you want to detect. Then pass this class as input to your code. Your code should work fine (assuming you are not using class(x) to decide code paths) since the new class should behave like a double data type. This option will not work if your output data is not created from your input data. See subclassing built-in types.
Did you try depfun?
The doc shows results similar to the ones you request.
doc depfun:
...
[list, builtins, classes, prob_files, prob_sym, eval_strings, called_from, java_classes] = depfun('fun') creates additional cell arrays or structure arrays containing information about any problems with the depfun search and about where the functions in list are invoked. The additional outputs are ...
Looks to me you could just filter the results for your function.
Though need to warn you - usually it takes forever to analyze code.
I'm learning MatLab & hit a roadblock.
I have an interface.fig file with interface.m which is acting as my 'main' GUI window. From there another file; bright.m is called.
The file bright needs to update global variables in the main file as well as call functions, I have worked out the global variables out but cannot call functions.
Tried everything, looked at doing things like:
reDisplay();
evalin('base','reDisplay()');
interface.reDisplay();
interface>reDisplay();
But had no luck.
Only the first function in an M file is callable from outside of that file. If you want your functions to be globally accessible then you need to save them in independent files.
If you need state to be globally accessible between these functions pass them as arguments or consider using an Object Oriented approach to solving your problem.
If a function in Matlab is defined inside an m-file with file name different than function name - then there is no way of calling this function from outside its m-file.
In order for your reDisplay function to be visible to bright.m, you should have this function in its own m-file called reDisplay.m