running different matlab scripts from different folders by using one script - matlab

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.

Related

MATLAB - Saving all base workspace variables into a mat file using uisave

I am trying to allow the user of my software to save all the base workspace variables into a mat* file, however I'm not sure how to accomplish this using 'uisave'.
Is there a better way to do this than calling every single workspace variable manually using evalin('base', 'var') and creating a list of strings to input as an argument into uisave?
The function save will do it:
save('filename.mat')
You also may save one or several variables with the same function:
save('filename.mat', 'variable1', 'variable2')

MATLAB - get current workspace

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

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

Matlab gui files in a package?

I am trying to put some .fig and their corresponding .m files in package.
I have:
+ui/mainWindow.fig
+ui/mainWindow.m
But when I try to run mainWindow.fig Matlab prints an error from GUIDE:
Error using feval
Undefined function or variable mainWindow
The funny thing is that if i call with its fully qualified name:
ui.mainWindow
the window appears normally (but all callbacks don't work anyway).
I have tried to import ui.* before running it.
Please note that I want to do this as sort of namespace. I don't want to have my entire application in the global scope of Matlab.
Explanation
The fundamental problem is that MATLAB GUIDE is unaware of packages. Normally, it manages the callback names automatically, keeping the .m and .fig files synchronized, and everyone is happy. When the figure is within a package, it fails to properly update the callbacks in the .fig properties - these still point to the unqualified name mainWindow rather than the correct ui.mainWindow. Subsequently, all callbacks fail.
Workarounds
Two ways around this one:
Export your figure: Guide -> File -> Export. Place this file within your +ui folder. Open the file, and do a find-replace replacing all instances of #(hObject,eventdata)mainWindow with #(hObject,eventdata)ui.mainWindow.
Alternatively, you can manually update the references directly within GUIDE itself, without exporting. For each button and element, Right Click -> Property Inspector then edit the 'Callback' field, replacing mainWindow with ui.mainWindow.
Personally, I prefer the first solution because you can replace all occurrences with a single find-replace command.