I have a function in MATLAB, say [o1, o2]=MyFunction(i1,i2), and I have a main which is also in a function called main. Is this even make sense?
I did it in MATLAB as follow:
function main
i1=1;
i2=2;
[o1, o2]=MyFunction(i1, i2);
end
function [o1, o2]=MyFunction(i1, i2)
%Code goes here.
end
I cannot run this script anyway. Please any suggestions?
There is no "main" function in MATLAB. You should move its contents to a separate script, like the following:
Script 1:
i1=1;
i2=2;
[o1, o2]=MyFunction(i1, i2);
Script 2 (called "MyFunction.m"):
function [o1, o2]=MyFunction(i1, i2)
%Code goes here.
end
Then run Script 1.
as it has been commented above it works fine... also if all you want the function to do is define simple variables and call another function #ClydeW's answer is a sensible way to do that. For more complicated variables mat-files created with save or matfile and recoverd with load or matfile are available.
In Matlab terminology what you have there is a local function
Local Functions are extra functions defined within a function m-file, appearing after the end of the "main" function. Local functions have a separate workspace i.e. to use variables from the main function they will need to be inputs to the local function
Other alternatives for having "sub-functions" which are stored in the same m-file and used by the "main" function within Matlab are Nested Functions and anonymous functions
Nested functions are similar to a local function but appear within the "main" function definition i.e. before the end. The major difference being that a nested function has acces to the main functions workspace i.e. can use & modify variables from the main function without having them explicity as inputs or outputs
Anonymous functions are quite different in that they need defining with different syntax again within the "main" function, but prior to use (appearing earlier in file than the call to them). The inputs to an anonymous function come from the main function however other values used within an anonymous function use values from the main function at the time the anonymous function was defined.
Related
I have a GUI callback that includes a for loop but compared to running the same code in a script, the data/variable being retrieved within the loop after the loop ends is not being recognized and instead MATLAB says that the variable is undefined. The script runs fine with the same inputs. I am well aware that scripts and functions have different workspaces, but does this mean that the recent data in a function after ending a loop is forgotten?
The callback belongs to a push button while the data I am handling in that callback is symbolic.
I would like to know what is causing this problem
There is difference between functions and scripts.
functions have their local variables. These variables are defined within the scope of that function only. Inside of a function is isolated from the caller scope. The only way to exchange data is input and output parameters of the function.
When the flow of program reaches to a function, all variables are stored in stack, and the execution of function starts without any variables except the input parameter. On the exit from function, all local variable defined in the function are deleted, and the stored variable on the stack will be returned back to the scope.
To circumvent this mechanism, you can use
global variables
assignin and evalin functions
But this is not considered as a procedural programming.
I have a script that calls a function, which is written in separate file and contains sub-functions that are inner to main function only. In one of my sub-functions, I have persistent variable that I would like to clear every time I run the main script. How can I do so? In addition, I have breakpoints through my code, and I would prefer to keep them while I clear the persistent variable - how that can be done?
MainScript.m script:
clear variables;
for iterNum=1:5
dataOut = MyMainFunction(iterNum);
end
disp(dataOut);
MyMainFunction code:
function dataOut = MyMainFunction(iterNum)
if (iterNum==1)
clear MySubFunction;
end
dataOut = MySubFunction();
end
function dataOut = MySubFunction()
persistent idx;
if isempty(idx)
idx=1;
end
dataOut=idx;
idx=idx+1;
end
I would like to clear "idx" persistent variable every time that I run MainScript.m, but of course to keep that variable as long as the script is running.
Thanks, John
The easiest way I see is to call clear followed the function name:
clear MySubFunction
instead of
clear variables;
This should clear all the persistent variables in that particular function. This will probably have the side effect of removing the stored JIT'd copy of it, causing it to be reparsed the next time it is invoked.
You can use munlock if you previously mlock'ed your function.
OR
You can define a special set of parameters in your function that are designed to solely clear the persistent variable, and you call the function with this syntax at the beginning of your main file.
Unfortunately, the other answer is partially incorrect - it is NOT possible to clear persistent variables in a sub-function using clear MySubFunction.
To quote an answer by a MathWorks staff member,
Only top level or main functions (with the same name as the file) may be cleared. To clear any local or nested function the main function must be cleared and that can't be done while the main function (or any other function in the file) is running.
and
Only whole m files can be cleared from memory. The entire file is managed as a unit so sub functions can't be cleared without clearing the main function.
As such, your options are to
separate the sub-function into its own m-file, or
clear the entire MyMainFunction from within MainScript.m, or
follow Ratbert's second suggestion, i.e. instead of using clear, give MySubFunction an additional argument that tells it to reset the persistent variables by itself
I came accross this sentence in MATLAB doc:
The body of a parfor-loop cannot make reference to a nested function. However, it can call a nested function by means of a function handle.
Can someone please explain what this means?
A parfor loop is different from a normal loop, in that the body of the loop has its independent workspace for every iteration. In fact, when you are running the parfor loop on a parallel pool, the variables that need to be transmitted to the loop body are saved and reloaded (that's, by the way, the reason for the "variable x cannot be sliced which may lead to communication overhead" warning: Having to save and reload huge variables may add quite a bit to your processing time).
Consequently, calls to nested functions won't work - the nested function in the parent function no longer shares its workspace with the loop body. Furthermore, nested function calls may alter workspace variables across iterations of a loop, which won't mesh with parallel execution.
In contrast, passing a function handle, or calling a separate function, works fine. The function defined in the function handle, as well as the separate function, have their own workspaces, nothing gets shared across iterations of the parfor body, and thus the iterations can run completely independently.
/aside: Creating a function handle to a nested function may still be able to cause you problems: a live function (as opposed to a function handle stored as string which you "activate" with str2func) handle can carry quite a bit of the existing workspace, including handle objects. Both the size of the workspace and the not-being-passed-by-reference (because of save&reload) may lead to unhappiness.
When I try to access the data member of the handle structure inside a non-callback function, it gives me "Reference to non-existent field ..." error. Or is it the situation that you can ONLY use the handle structure inside a callback function in matlab gui programming?
If you want to use handles inside a random function you have to use the following:
handles = guidata(hObject);
This allows you to "load" the handle structure and where hOject is a handle, be sure to pass this variable in the function you are writing with an argument:
function [var_out] = my_function(var_in,hObject,handles)
handles=guidata(hObject);
%some code
guidata(hObject,handles);
end;
The last line is useful if you want to "save" everything you've done with the handle structure.
This way, you'll be able to use the handle structure in a non-callback function:
function my_callback(hObject,eventdata,handles)
%some code
[var_out] = my_function(var_in,hObject,handles);
end
All this works if you call my_function in a callback.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Declaring a global variable in MATLAB
Currently my matlab code is in one big script file with no internal functions. I would like to have functions declared within my script, to make my code more readable and reuse code etc. This in itself is not difficult in matlab, e.g.
Example 1:
function main()
myfunc('goat');
end
function myfunc(x)
fprintf(x);
end
My problem is that I have a biig datafile, which I want to load only once, such that I can re-run my code during development without spending time on re-loading the data. This is not a problem in my current framework with one big script with no internal functions. One solution is to have two matlab script files. One for loading data and then calling the functions in another script.
However in the above example 1 a true global variable declaration will not work, and I am forced load the biig file every time I run the script. What I would like to do can be shown in two examples:
Example 2:
% Global variable
if ~exist('data',var)
data = load biigdatafile.mat; %FAILS, outside function.
end
function main()
myfunc('goat');
end
function myfunc(x)
fprintf(x);
end
Example 3:
function main()
% Global variable
if ~exist('data',var)
global data; % Is not really global after whole script is completed.
data = load biigdatafile.mat;
end
myfunc('goat');
end
function myfunc(x)
fprintf(x);
end
So my question is how to declare a true global variable as in example 2, where I load my data once to stay in workspace, while calling my functions inside one script?
Use input arguments, that's what they're made for
You could just use arguments to the main function, load the dataset once into the base workspace and call your function with that dataset as an argument. If any subfunctions also use the dataset, pass it along
function main(data)
if nargin<1
disp('hey, you forgot to supply the dataset!');
end
% do your stuff
showData(data);
end
and then in the base workspace:
Data = load('biigdatafile.mat');
main(Data);
Use persistent variables
persistent X Y Z defines X, Y, and Z as variables that are local to the function in which they are declared; yet their values are retained in memory between calls to the function. Persistent variables are similar to global variables because the MATLAB software creates permanent storage for both. They differ from global variables in that persistent variables are known only to the function in which they are declared.
So you could easily use:
function main()
persistent data;
if isempty(data)
disp('loading dataset');
data=load('biigdatafile.mat');
end
% do your stuff
showData(data);
end
First time you call this function on a cleared base workspace or cleared function*, the dataset will be loaded. Every next time (when the function hasn't been edited), the dataset will already/still be in memory.
I usually do this when I'm just using one dataset; it's tedious to always load the dataset and when testing a function, it's also easier to just press F5.
* when is a function cleared you might ask?
Whenever you clear or modify a function that is in memory, MATLAB also clears all persistent variables declared by that function. To keep a function in memory until MATLAB quits, use mlock.
Global variables have to be declared everywhere they are used. Just add the global data to whereever you need it, and it will work.
It is not a duplicate of declaring global variables in matlab. A global var is only accesible to all functions, but when the script is complete, the global variables are gone and I need to read them into memory again.
What I need is to move the local variable into the base workspace and then access it again during other function calls. I actually solved it myself, just now with this code
function main()
try
Data = evalin('base','Data'); % Move back from base workspace
fprintf('Already loaded...\n');
catch
fprintf('Loading data...');
data = load biigdatafile.mat;
assignin('base', 'Data', Data); % Move to base workspace
fprintf('Ok\n');
end
showDocID(Data{1});
end
function showData(Data)
fprintf(['Data: "' Data '"\n']);
end