Load command don´t work the way I expect!

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)

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

No, the problem is that I don´t see anything at all in the workspace. It loads nothing!
If you try
datastruct = load(Measurefile);
then what does size(datastruct) and fieldnames(datastruct) report? What does
whos('-file', Measurefile)
report?
ans =
1 1
ans =
'time'
'sVcEc_Z_NoxSensUsNoxRaw'
'sVcEc_Z_NoxSensDsNoxRaw'
'Scm_VSpd'
'Spt_GaDPF'
'Post_NO'
'Post_NO2'
'Pre_NO'
'Pre_NO2'
'Measurefile'
Name Size Bytes Class Attributes
Measurefile 1x7 14 char
Post_NO 17901x1 143208 double
Post_NO2 17901x1 143208 double
Pre_NO 17901x1 143208 double
Pre_NO2 17901x1 143208 double
Scm_VSpd 17901x1 143208 double
Spt_GaDPF 17901x1 143208 double
sVcEc_Z_NoxSensDsNoxRaw 17901x1 143208 double
sVcEc_Z_NoxSensUsNoxRaw 17901x1 143208 double
time 17901x1 143208 double
Is this not what one would expect?
Then the
datastruct = load(Measurefile);
did work, and you can go ahead and access the fields you need.
But I cannot access the files, and I cannot see them in the workspace.
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.
Wow! It worked now! accessing it via datastruct.xxx did it! That you very much for your help. I appreciate it a lot. Have a good day!

Sign in to comment.

Asked:

on 4 Apr 2016

Commented:

on 5 Apr 2016

Community Treasure Hunt

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

Start Hunting!