Why the load() function load nothing of .MAT file when used in GUI callbacks or other self-definition functions?

107 Views Asked by At

I have met with a problem when trying loading .MAT file in Matlab GUI with the button callback. The code as follows:

function button1_callback()
[filename,pathname,filterindex]=uigetfile('*.mat','open data');
s1=load (strcat([pathname filename]));

Then, nothing was loaded in the workspace. What can I do to solve this problem? Thanks!

1

There are 1 best solutions below

2
On

In this case, your statement s1 = load(...) requests the result of load to be returned as a struct in the variable s1, see this section of the doc. The variable s1 is defined inside the function button1_callback, and therefore gets destroyed when the execution of that function completes.

So, you need to do something to put the data from s1 where you need it next. If you're programming a UI, you might modify some state of that UI. Or, you could simply push the data into MATLAB's base workspace by using assignin

assignin('base', 's1', s1)