3

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.

chappjc
  • 30,359
  • 6
  • 75
  • 132
user3043636
  • 559
  • 6
  • 23

1 Answers1

4

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:

evalin('base','load(''filename.mat'')');

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);
evalin('base',loadCmd);
chappjc
  • 30,359
  • 6
  • 75
  • 132
  • thanks, but if I want take a filename from a edit text? for example filename=get(handles.file_name,'String'); evalin('base','load(??????)'); – user3043636 Nov 27 '13 at 22:12
  • @user3043636 Answer updated. :) You can either use horizontal concatenation (`[]`), or you can form the string with `sprintf` like `sprintf('load(%s)',fname)`. – chappjc Nov 27 '13 at 22:15
  • @user3043636 Of the filename variable? I added two examples to the end of the answer. – chappjc Nov 27 '13 at 22:19
  • GUIDE gives me an error. I set : fname=get(handles.file_name,'String'); loadCmd = sprintf('load(%s)',fname); evalin('base',loadCmd); it is correct? – user3043636 Nov 27 '13 at 22:23
  • @user3043636 Looks OK. Take off each terminating `;` to see the contents of the variables. What is the error? The file may not be in the current working directory. – chappjc Nov 27 '13 at 22:27
  • the error is : Undefined variable "file_name" or class "file_name.mat". Error in Frame>load_param_Callback (line 659) evalin('base',loadCmd); where file_name is the name set as input – user3043636 Nov 27 '13 at 22:32
  • @user3043636 Oops, my mistake, answer updated. Just make sure file_name.mat exists. – chappjc Nov 27 '13 at 22:34