Load command don´t work the way I expect!
Show older comments
Hi!
I am trying to use the load command to open a file from a different location and use this script:
[stmfile, stmpath] = uigetfile('*.mat','Pick data file');
Measurefile = fullfile(stmpath, stmfile)
Then I use
disp (Measurefile)
and gets "C:\Matlab\Template\matdata.mat", which is what I expect.
But then I use
load(Measurefile);
and it doesn´t work!
No errors, it simply wont load.
I am quite new to this so please don´t slam me, I just need help.
Thanks in advance!
Ola
Answers (1)
Walter Roberson
on 4 Apr 2016
Use
datastruct = load(Measurefile);
Now there will be one field in datastruct for every variable that is stored in the file. For example if there were variables voltage and Power in the file, there would be datastruct.voltage and datastruct.Power fields.
You can cross-check with
size(datastruct)
fieldnames(datastruct)
to see what you loaded.
Note: if you are doing this inside a function, remember that the variables will disappear as soon as the function returns.
7 Comments
Oronnert
on 4 Apr 2016
Walter Roberson
on 4 Apr 2016
If you try
datastruct = load(Measurefile);
then what does size(datastruct) and fieldnames(datastruct) report? What does
whos('-file', Measurefile)
report?
Oronnert
on 4 Apr 2016
Walter Roberson
on 4 Apr 2016
Then the
datastruct = load(Measurefile);
did work, and you can go ahead and access the fields you need.
Oronnert
on 4 Apr 2016
Walter Roberson
on 4 Apr 2016
datastruct.Post_NO would be the content of the Post_NO variable of that particular Measurefile. Do you see datastruct in the workspace?
One problem you have is that you have a variable named Measurefile saved in your .mat file. When you load() that .mat file without specifying any output, then the Measurefile from the .mat file would overwrite the Measurefile that you had created . That is likely to cause problems. Using the output form of load() and accessing the resulting structure is firmly recommended.
Oronnert
on 5 Apr 2016
Categories
Find more on Whos in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!