Matlab – Save or Load Data from/to GUIDE/Workspace


I'm new in GUIDE in MATLAB. I have two different problems:

  1. I would like to save in a .mat file all variables (about 1000) from workspace using a button in GUI in MATLAB. How can I do?

  2. I have a button where after press on it I'm able to load a specific .mat file from my path, always using GUI, but I would like that the variables contained in this file, became presents in the base workspace.

In other words, I have a button, "LOAD", in GUIDE in MATLAB where I can load a .mat file, and the variables contained in the .mat file must be loaded into the 'base' workspace when the button is clicked.

Please help me.

Best Solution

For your first question, I would suggest just putting a command like save('filename.mat'); in the "Save" button's callback. But what variables? If they are in the base workspace, see my answer to your second question below.

To load data into the base workspace, you might try the evalin command:


The 'base' argument tells it to run the command in the base workspace.

If your file name is in a variable:

fname = 'filename.mat';
evalin('base',['load(''' fname ''')']);

Alternatively, you could use sprintf:

loadCmd = sprintf('load(''%s'')',fname);
Related Question