MATLAB - get current workspace - matlab

Inside of an function I want to do assignin(ws, 'var', val), but at this point I don't know the workspace ws, which is currently displayed in the workspace window inside the matlab window. I am looking for a statement which replaces my ws in the statement above.
Probably a simple and a frequently asked question, but i didn't find an answer.

What you seem to want is do an assignin in the current workspace. But this is trivial in MATLAB, because is the basic assignment:
var = val;
The other two accessible workspaces via the ws parameter are:
'base', which is the base workspace (all the scripts will create their variables in it);
'caller', which is the workspace of the calling function (this workspace is created every time you enter a function, and destroyed when exiting the function), or the global workspace if called from a script.

I used the following workaround:
function main()
...
assignhere(name,value)
...
return
function assignhere(varname,varvalue)
assignin('caller',varname,varvalue);
return
although I would prefer to have access to the current workspace directly

Related

running different matlab scripts from different folders by using one script

I want to run multiple scripts from different folder by using one script
for example I have the following code:
Original_AddRun1 = 'F:\UPT\Root\Run\S1.m';
Original_AddRun2 = 'F:\UPT\Root\Run2\S2.m';
Original_AddRun3 = 'F:\UPT\Root\Run3\S3.m';
Original_AddRun4 = 'F:\UPT\Root\Run4\Subfolder\S4.m';
run(Original_AddRun1);
run(Original_AddRun2);
run(Original_AddRun3);
run(Original_AddRun4);
there are four scripts that I want to run (S1.m, S2.m, S3.m and S4.m) which are located in the folders (Run, Run2, Run3 and Subfolder inside Run4)
the above Matlab is created inside the "Root" folder that have all the folders
however when i run the code the following error happens
Undefined function or variable 'Original_AddRun2'.
Error in AllRun (line 7)
run(Original_AddRun2);
the first script runs and I get the figure plot I want but the it stops when it tries to run the second script, each individual script works perfectly and were tested before
keep in mind that S2.m needs the variables generated by S1.m to work and the same with S3.m that needs S2.m's generated variables and S4.m needs S3.m's generated variables... this is why those script needs to run in order and Function cannot be used because it uses it's own workspace and not the general workspace
I used save() to save the general workspace at the end of each script then clear the general workspace to be able to run the next script then used load()
in that script to load the variables I need to the general workspace again to use them
I feel there is a much simpler way to do it that the roundabout way save() and load()
I think it is easier to avoid clear as much as possible. It should only be used at the top of a main script. The solution is to use a function workspace (by creating a function) instead of the global workspace. In this way we avoid cluttering the global workspace.
main.m:
clear all
Original_AddRun1 = 'F:\UPT\Root\Run\S1.m';
Original_AddRun2 = 'F:\UPT\Root\Run2\S2.m';
Original_AddRun3 = 'F:\UPT\Root\Run3\S3.m';
Original_AddRun4 = 'F:\UPT\Root\Run4\Subfolder\S4.m';
run(Original_AddRun1);
run(Original_AddRun2);
run(Original_AddRun3);
run(Original_AddRun4);
F:\UPT\Root\Run\S1.m:
function S1()
... % global variables (ex. Original_AddRun1) are not defined here
... % newly defined variables are not added to global scope
end
I found that when you run a .m script from another .m script you have to clear the workspace before running another .m script in the same process.
Since each script depends on the variables of the previous script in the question that is not directly possible. To avoid this issue and still achieve the objective it is possible to use the save() function to save the workspace, then clear it to run the next script, and then load it again with load().
A bit roundabout way of doing it but it works.

MATLAB Figures and Global Scope

I am using functions from a MATLAB package ("EEGLAB"). One of the functions I'm using ("pop_selectcomp") creates a GUI. However, when I try to interact with the GUI, an error results: an expected variable (In this case "EEG", a data structure) is not defined. That's odd, because pop_selectcomp has EEG as an input. I discovered that declaring global EEG anywhere in the function stack above my call to pop_selectcomp makes EEG available to it again. This is the structure of my function stack.
Main Script
data import function loads variable "EEG"
function to process data
function call to pop_selectcomp
So declaring EEG as a global in the main script or the data processing function fixes the problem.
My interpretation is that when pop_selectcomp creates its GUI window, it's being created outside of my function stack in the main workspace or something like that. So, the EEG variable is only available to it if it's been declared global above the function call. I'm not very familiar with Matlab figures and GUIs, but I guess that normally pop_selectcomps doesn't have this problem because it's not called as a sub-function.
Is there any better way to make this work? Can I somehow point pop_selectcomps' GUI at the correct sub-workspace where it will find the variables it needs? I can modify pop_selectcomps if I have to, although that would be messier. The function can be found here:
https://sccn.ucsd.edu/svn/software/eeglab/functions/popfunc/pop_selectcomps.m

How to use MATLAB toolbox function which has the same name of a user defined function

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.

Matlab function working only in debug mode

I have a function in a separate file
function [ ] = loadModel( model , version )
cd(model.path);
loadPath = strcat(model.name(1,:) , model.versions(version,:), '_results' ) ;
load(loadPath,'-mat');
end
which using model structure and version number as inputs changes directory to the one of the model and loads its results. When I call the function from a m-file there is no error but its seems like none of the lines of the function have been executed e.g. a variable loadPath doesn't even exist. When I put a break point before load(), I see loadPath generated correctly and if I type the last line manually it works fine. Any clues why is this happening?
P.S. I am used to C++, Java and I find the matlab language absolute nighmare
This is because you're only loading the file within the scope of the loadModel function. In MATLAB, variables declared within a function (this includes through file loading) are only defined within that function (barring global variables, assignin, etc). You need to return the results of the file load in order to use the loaded data. For instance:
function data = loadModel( model , version )
cd(model.path);
loadPath = strcat(model.name(1,:) , model.versions(version,:), '_results' ) ;
data = load(loadPath,'-mat');
end
This will load the file into a struct, which is returned to the calling scope. You can then access the loaded data from this struct.
You need to understand the concept of workspace: the function workspace is different from the base workspace (the one you can access from the command line). Your function executes normally and the variable loadPath is created, but in the function workspace, not the base workspace. As your function doesn't return anything you don't have access to it. In debug mode, you get access to the function workspace, so you can see the variable. You need the function to return the variable of interest if you want to access it from the base workspace. I suggest you have a look at the documentation, it's very thorough.

MatLab global function issue

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