How to create a big mat.file including many variables generated in a GUI

Hi Guys,
i execute a function within a GUI and the calculated variables are not in the workspace but only available within the GUI. How can i save those variables to a mat.file. I know i could write all of them into the workspace and apply the command
save('path\myfile.mat');
BUt isnt that unnecessary and ineffective to write it into workspace first? How can i save all generated variables in my GUI as a mat.file without transferring them to workspace first? is there a possibility for that?
I am very glad for help!
Best regards, John;)

 Accepted Answer

Of course you can save those variable directly from the GUI workspace. And yes it is inefficient to transfer them to the base workspace, if they are not required there. Your code
save('path\myfile.mat');
will work just as well inside the GUI workspace as it does in the base workspace.
With a few minor differences basically every workspace (base or function) lets you do the same things: create variables, save them, call other functions, etc, etc. The difference are noted in the documentation (e.g. nargin), but functions like save can be called from any workspace.
You should read about the different workspaces:
and how to share data between them
Note that the most efficient way to share data is to pass them as variables, and the least efficient way is to use assignin or evalin (although beginners seem to love using these two functions they should be avoided).

5 Comments

thanks for your answer!!
it works well, so all variables are saved as you said and i can even choose what variables to save! perfect.
Now i would like to give the opportunity to the user to choose the name of the file that is saved and to choose the location folder as well. If i have a mat.file in matlab then it works like this. This is acutally the callback of the save button with an example mat.file called 'myMatrix'
handles.myMatrix=[1,2,3;4,5,6]; % Fuege Matrix zu Testzwecken hinzu
myMatrix=handles.myMatrix
startingFolder = 'C:\Program Files\MATLAB'
if ~exist(startingFolder, 'dir')
% If that folder doesn't exist, just start in the current folder.
startingFolder = pwd;
end
% Put in the name of the mat file that the user wants to save.
[file,path] = uiputfile('myMatrix.mat','Save file name');
if file == 0
% User clicked the Cancel button.
return;
end
fullFileName = fullfile(path, file)
% optional, the variables to be saved can be chosen
save(fullFileName, 'myMatrix')
But i dont want to save the mat.file 'myMatrix' but specific variables created after having pressed a 'run' button so that my simulation is finished then.
save('C:\Users\john\Desktop\GUI_Experiments\Status Quo\finalresults.mat', 'SeqVoltage_res','SeqVoltage_res')
How can make this possible for the 'save' callback? so that the variables 'SeqVoltage_res' and 'SeqVoltage_res' are saved in a location defined by the user and with a name given by the user. I dont know how to do it because i dont have a corresponding mat.file like 'myMatrix' above:(
Best regards, John
The variables that save can save must be available in that workspace where save is called from. You basically have a choice of two syntaxes:
save(filename) % save ALL variables in that workspace
save(filename,va1,var2,...) % save the named variables
I would not recommend the first syntax. You should use the second syntax and simply list all of the variables that you wish to save. This is much more robust coding.
You don't say where those "specific variables" are: if they are in the same workspace then list them in the save call. If they are in the workspace of the calling function, then using nested functions is an efficient way to let save be able to access them.
Hi Stephen, thx for your answer! yes those specific variables are in the same workspace. My only question now is, how i can make the user chose the folder to save and define a name for the mat.file so combining the save command with the uiputfile...i would like to integrate this into my first example of code. The 'myMatrix.mat' file was just a placeholder for all the variables that now have to be stored...but my problem is that the variables to be stored are not defined as mat.file. so i cannot just use the first code sample!
I hope i expressed myself better now.
Id be glad for help!
Best regards, John
This is exactly what uiputfile does: lets the user designate a folder and a filename. You already use this in your code, so just change the filter spec to '*.mat' and they will be able to select the name as well:
[myfile,mypath] = uiputfile('*.mat');
Note that path is a very bad name for a variable, because this is the name of an important inbuilt function path, which will not work when you define a variable with the same name. You can use which to check if variable names are already defined.
thank you very much!!! i got it now! :)

Sign in to comment.

More Answers (0)

Asked:

on 21 Sep 2015

Edited:

on 21 Sep 2015

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!