.mat files not loading correctly

Hi all,
I am very new to MATLAB, and the first program i wrote which loaded a file loaded a file named data_vector. Now, whenever I load any file it gets automatically stored in a variable of name data_vector. This means that the following code will not compile on my copy of MATLAB R2016b.
clear
load test_data;
plot(test_data(1,:)) %test_data is a 1000x500 2D array
Please help!
Edit: the specific error message is
Reference to a cleared variable test_data.
Error in phase_detector (line 4)
plot(test_data(1,:))

7 comentarios

Matt J
Matt J el 13 de Abr. de 2017
Editada: Matt J el 13 de Abr. de 2017
It's a little too hard to diagnose from the code shown. Modify it as follows and report all output to us.
clear
disp 'Contents of file:',
whos -file test_data
load test_data;
disp 'Contents of workspace:',
whos
plot(test_data(1,:)) %test_data is a 1000x500 2D array
dpb
dpb el 13 de Abr. de 2017
load will retrieve the variable(s) stored in the .mat file; they may or may not be named the same as the filename used to store them.
Show us the result of
whos
whos -file test_data.mat
in the workspace...the first will show the variables in the workspace and their size and class; the second the content of the file on disk.
It looks like you may have inadvertently re-SAVEd the file after having cleared the variable so that it's empty; the second above will show you what is actually in the file.
after replacing the entire code with what @Matt J provided, the output is:
>> phase_detector
Contents of file:
Name Size Bytes Class Attributes
data_vector 1000x500 4000000 double
Contents of workspace:
Name Size Bytes Class Attributes
data_vector 1000x500 4000000 double
Reference to a cleared variable test_data.
Error in phase_detector (line 7)
plot(test_data(1,:)) %test_data is a 1000x500 2D array
I just realised my OP is incorrect, data_vector isn't the name of the first file i loaded - sorry! that was data_matrix.
I generated the test_data file using another script which stores the data in a file called data_vector, but at the end of the script I do:
save test_data data_vector
Sorry for the mistake, hopefully this makes the problem a lot less odd
dpb
dpb el 13 de Abr. de 2017
Editada: dpb el 13 de Abr. de 2017
The only problem appears to be your expectation...the variable is named data_vector, the file you wrote it to is test_data.mat.
Naming a file doesn't change the variable name inside it; the
whos -file test_data
line clearly reveals the content of the file is, as you asked it to be, the array data_vector. When you load the file, the variable(s) in the file are restored to the workplace with their original name.
This is all in
doc save
doc load
There simply is no variable test_data in sight here...if you want that as a variable name you'll have to assign something to it to make it such.
Daniel Guilbert
Daniel Guilbert el 14 de Abr. de 2017
So what's the point of being able to save them under different names if you can only ever reference them using the initial name? Is there anyway to change the name of a variable besides cloning? Seems a bit weak.
dpb
dpb el 14 de Abr. de 2017
Editada: dpb el 16 de Abr. de 2017
This doesn't save them under different variable names, only as a different file; that's the point. SAVE/LOAD is primarily a shorthand for precisely that purpose; save/retrieve a given variable or set thereof for quickly being able to reproduce a snapshot of a previous session or the like without having to worry about extraneous code.
If you really want to save a set of data but read it back in a more general sense, use something other than SAVE (like fwrite/fread for unformatted) that doesn't include such additional information with it.
Or, as Steven notes, if you want to force rename a variable from a .mat file, there's always the structure route albeit it is a fair amount of overhead often compared to using simple arrays.

Iniciar sesión para comentar.

 Respuesta aceptada

Steven Lord
Steven Lord el 14 de Abr. de 2017

0 votos

  1. Calling clear to clear all variables is rarely necessary in a function.
  2. If you call load with an output argument, the output argument will be a struct array and the variables that were in the MAT-file will be read into fields in that output argument. This allows you to avoid conflicts with variables that already existed in the workspace. I recommend you use this approach; that way the name of the variable you have to access to use your data is predictable. You can use dynamic field names in conjunction with fieldnames if you don't know the names of the variables in your MAT-file but need to iterate over them.

Más respuestas (0)

Productos

Etiquetas

Preguntada:

el 13 de Abr. de 2017

Editada:

dpb
el 16 de Abr. de 2017

Community Treasure Hunt

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

Start Hunting!

Translated by