Calling sub-function (local function) from another file on Matlab - matlab

I'm trying to figure out how to properly call a sub function from another .m file. I've searched for documentation, and only found the way of using function handles (as can be seen here: https://www.mathworks.com/help/matlab/matlab_prog/call-local-functions-using-function-handles.html)
The thing is that I think I've managed to find a way to it without using function handles, and want to know what am i doing and how does it works:
on main.m file:
function main
%function main...
end
function z = sub_function(x,y)
%function sub_function...
end
on outside.m file:
function call_to_local_function_from_outside
z = main('sub_function', x,y);
end
Appernalty, this is working, but I need to know why?

Related

MATLAB : GUI pushbutton call function .m

I have some functions create on matlab (.m). And I would like to call them on an interface GUI : how my push button callback function can call a function .m (which is in the same workspace) ?
Moreover, my function returns some variables so I would like to keep these variables in my workspace in order to access them from other buttons of my interface.
And after, is it possible to put the result of a variable on my interface ?
Thank you in advance,
Best regards
Yes it's possible and is quite simple.
You can call your .m file directly in the pushbutton callback and fetch the results as you would in any other script.
Let's consider a simple example in which you call function A from the callback. Let's say function A outputs 2 arguments, out1 and out2.
In the .m file of function A, the function is defined as follows (input arguments can be anything of course):
function [ou1,out2] = A(Input arguments)
%// code here
end
Then in the pushbutton callback in your GUI, use this syntax to retrieve the outputs of function A and use them:
[B,C] = A(Input arguments); %// Or out1 and out2, the names don't matter here.
Better yet, to share those data with other callbacks from your GUI, you can store the variables in what is called guidata, or the data associated with the GUI. There is a convenient handles structure used to store any kind of data and easily share them between callbacks.
Therefore, in your case you would use:
[handles.B, handles.C] = A(Input arguments)
and as such handles.B and handles.C are accessible from any callback in your GUI. Don't forget to update the handles structure at the end of the callback with this command:
guidata(hObject,handles)
where hObject is the handles to the GUI's figure. For more info about that check the docs here.

Calling functions & scripts in Matlab/Octave

How do call a script to a function and vica versa in Matlab/Octave?
function mean_DNA_Microarray = Calc_mean_DNA_Microarray(M)
M = DNA_Microarray
mean_DNA_Microarray = M - ones(5,25)*mean(M(:,25))
end
The response is
error: invalid call to script
C:\Users\Nacho\Documents\Matlab\DNA_Microarray.m error: called from:
error: C:\Users\Nacho\Documents\Matlab\Calc_mean_DNA_Microarray.m at
line 3, column 3
Now this will work if I call DNA_Microarray a function, but the problem requires that it remain as a script.
First of all, you are not defining your function correctly, as the function does not know what M is (unless it is a global vairable, but I doubt so).
In ANY programming language, you need to tell a function which variables it is going to work with. This is not Matlab specific. In Matlab you will do it so:
function mean_DNA_Microarray = Calc_mean_DNA_Microarray(M) % Look! we are telling him what M is!
mean_DNA_Microarray = M - ones(5,25)*mean(M(:,25))
end
Then you want to all the function from somewhere else you would need to just type its name and pass in the arguments, in this case what inside the function is going to be called M
clear;
clc;
% Test code
Mnameoutofthefunction=rand(100,100);
DNAmean = DNA_Microarray(Mnameoutofthefunction); % here we are calling it!
Remember to save the function as functionname.m , in your case DNA_Microarray.m , else Matlab wont know which one it is.
But I HIGHLY recommend you to read a book about Matlab or just about programming in general, as it seems like you could benefit from some basic introduction.
Following #am304 suggestion, here you can find nice tutorials:
http://www.mathworks.co.uk/academia/student_center/tutorials/
EDIT What you want to do is create a function as follows:
function mean_DNA_Microarray = Calc_mean_DNA_Microarray(M) % Look! we are telling him what M is!
mean_DNA_Microarray = M - ones(5,25)*mean(M(:,25))
end
And then, inside your function DNA_Microarray call Calc_mean_DNA_Microarray with the input M

MATLAB symbolic variables couldn't be used in nested function

I have a MATLAB function to solve a Inertia Tensor , and I have a nested function in my program . All the variables in it are symbolics but it told me
“Error using assignin: Attempt to add ”x“ to a static workspace”
and I don't understand why this happens . Here is my test.m code:
function test
syms x y z
f=x
f1=f+1
f2=f1^2
function r=test2
r=f2^3;
end
f3=test2
end
After searching this web-forum I have found some answers . But at the same time I just don't understand it
Andrew Janke explianed it like this : While syms A may look like a static variable declaration, it isn't. It's just a regular function call. It's using Matlab's "command" invocation style to look like syntax, but it's really equivalent to syms('a', 'b', 'c').
on this page : Matlab: "Error using assignin: Attempt to add "c" to a static workspace"
what does static variable mean ?
I also search the HELP doc and it said :In functions and scripts, do not use syms to create symbolic variables with the same names as MATLAB® functions. For these names MATLAB does not create symbolic variables, but keeps the names assigned to the functions.
I only know syms x to create a symbolic variable in the workspace but why does the documentation say MATLAB does not create ?
'Static' means fixed, 'workspace' is what Matlab calls the places where all of its variables are stored. For non-nested functions the workspace starts off as empty when Matlab is at the beginning of the function; as Matlab continues through function's lines of code it continuously add more variables to the workspace.
For functions with a nested function, Matlab first parses the function to see what variable will be created (it specifically looks for x = type lines), then it creates all of these variables (with value as 'unassigned'), and then only does it start to run through the code; but while running through the code, it can never create a new variable.
This is why the code
function TestNestedFunction
syms x;
function Nested()
end
end
generates an error, there is no x = to tell it to pre-create the unassigned variable x at the start of the code. It fails at syms x;, as that line tries to create a new variable x, which fails as it may not.
This is also why the following code runs
function TestNestedFunction
syms x;
x = x;
function Nested()
end
end
it sees the x = and then pre-creates x. (This is why your example of adding [x, y, z] = deal([]); also works).
You can test this with a break point at the beginning of simple non-nested function and a simple nested function. Just run it step by step.
This code works:
function test
x=sym('x')
y=sym('y')
z=sym('z')
f=x
f1=f+1
f2=f1^2
function r=test2
r=f2^3;
end
f3=test2
end
I think the pages you found are quite clear.
You need to declare the variables one by one and use:
x = sym('x')
Otherwise syms will try to assign the values into a workspace where this is not allowed.

Calling local functions from command line

I have a local function defined in an m-file. For example:
% begining of public_function.m file
function fh = public_function( )
%
% do some computation...
fh = #local_function; % return function handle to local function defined below
function y = local_function( x )
%
% a local function inside public_function.m file
%
% some manipulation on x
y = x;
% end of public_function.m file NOTE THAT local_function is NOT nested
Now, I would like to call local_function from command line (and not from public_function). I was able to do so using the function handle returned from public_function:
>> fh = public_function(); % got handle to local_function
>> y = fh( x ); % calling the local function from command line :-)
My question:
Is there any other way (apart from explicitly pass the function handle) to call local function from command line (or other m-file/functions)?
More precisely, I want a method to access any local function in a file (provided that I know its name). So, if I have public_function.m file (and function) and I know that local_function is local to that file, is there a way to access local_function from command line ?
The official documentation says that:
... you cannot call a local function from the command line or from functions in other files.
According to this, you must pass its handle to the caller in order to allow invoking it indirectly outside its m-file. I believe that there is no documented sensible way to access local functions otherwise.
Oddly though, you can still do this with help:
help public_function>local_function

How to wrap an already existing function with a new function of the same name

Is it possible to create a wrapper around a function that has the exact same name as the original function?
This would be very useful in circumstances where the user wants to do some additional checks on input variables before they are passed on to the built in function How to interrupt MATLAB IDE when it hangs on displaying very large array?
Actually alternatively to slayton's answer you don't need to use openvar. If you define a function with the same name as a matlab function, it will shadow that function (i.e. be called instead).
To then avoid recursively calling your own function, you can call the original function from within the wrapper by using builtin.
e.g.
outputs = builtin(funcname, inputs..);
Simple example, named rand.m and in the matlab path:
function out = main(varargin)
disp('Test wrapping rand... calling rand now...');
out = builtin('rand', varargin{:});
Note that this only works for functions that are found by builtin. For those that are not, slayton's approach is likely necessary.
Yes this is possible but it requires a bit of hacking. It requires that you copy around some function handles.
Using the example provided in the question I will show how to wrap the function openvar in a user defined function that checks the size of the input variable and then allows the user to cancel any open operation for variables that are too large.
Additionally, this should work when the user double clicks a variable in the Workspace pane of the Matlab IDE.
We need to do three things.
Get a handle to the original openvar function
Define the wrapper function that calls openvar
Redirect the original openvar name to our new function.
Example Function
function openVarWrapper(x, vector)
maxVarSize = 10000;
%declare the global variable
persistent openVarHandle;
%if the variable is empty then make the link to the original openvar
if isempty(openVarHandle)
openVarHandle = #openvar;
end
%no variable name passed, call was to setup connection
if narargin==0
return;
end
%get a copy of the original variable to check its size
tmpVar = evalin('base', x);
%if the variable is big and the user doesn't click yes then return
if prod( size( tmpVar)) > maxVarSize
resp = questdlg(sprintf('Variable %s is very large, open anyway?', x));
if ~strcmp(resp, 'Yes')
return;
end
end
if ischar(x) && ~isempty(openVarHandle);
openVarHandle(x);
end
end
Once this function is defined then you simply need to execute a script that
Clears any variables named openvar
run the openVarWrapper script to setup the connection
point the original openVar to openVarWrapper
Example Script:
clear openvar;
openVarWrapper;
openvar = #openVarWrapper;
Finally when you want to clean everything up you can simply call:
clear openvar;
I prefer jmetz's approach using builtin() when it can be applied, because it is clean and to the point. Unfortunately, many many functions are not found by builtin().
I found that I was able to wrap a function using a combination of the which -all and cd commands. I suspect that this approach can be adapted to a wide variety of applications.
In my example case, I wanted to (temporarily) wrap the interp1 function so that I could check for NaN output values. (The interp1 function will, by default, return a NaN under some conditions, such as if a query point is larger than the largest sample point.) Here's what I came up with:
function Vq = interp1(varargin)
persistent interp1_builtin;
if (isempty(interp1_builtin)) % first call: handle not set
toolbox = 'polyfun';
% get a list of all known instances of the function, and then
% select the first such instance that contains the toolbox name
% in its path
which_list = which('interp1','-all');
for ii = 1:length(which_list)
if (strfind(which_list{ii}, [filesep, toolbox, filesep]))
base_path = fileparts(which_list{ii}); % path to the original function
current_path = pwd;
cd(base_path); % go to the original function's directory
interp1_builtin = #interp1; % create a function handle to the original function
cd(current_path); % go back to the working directory
break
end
end
end
Vq = interp1_builtin(varargin{:}); % call the original function
% test if the output was NaN, and print a message
if (any(isnan(Vq)))
dbstack;
disp('ERROR: interp1 returned a NaN');
keyboard
end
end
See also: How to use MATLAB toolbox function which has the same name of a user defined function