load structure array with a function and extract the variables
Mostrar comentarios más antiguos
Hi,
I am trying to write a function that will load a generic file that is a structure array. Then I want to extract the variables (and perform some operations on them).
The problem is that i cannot extract them, because, I need to call the function with 'filename.mat', but when I then want to extract the variables, it does of course not work, because the name of the variable contains .mat, instead of only 'filename' - which i could use as the structure.
The array does have a variable called 'name', its value being the file name w/o the .mat extension.
My idea was to extract this value and turn it into a variable, but I cannot access it:
- with load(filename,'name') i get 'filename.mat' - and not 'filename'...
- and with filename = load(filename,'name') i get an empty structure array??
I hope I expressed myself clearly enough and that somebody can tell me what is wrong??
see the example:
- function [cube] = img(filename)
- load(filename)
- filename=load(filename, 'name')
- cube = filename.data;
- end
The warnings and error i get are:
_________________________________________________________________
Warning: Error occurred while trying to call loadobj on a dataset object: Reference to non-existent field 'props'. > In img at 7 Warning: Class 'dataset' is an unknown object class or does not have a valid 'loadobj' method. Object 'Norway_spruce_not_extracted_RA_ray' of this class has been converted to a structure. > In img at 7 Warning: Variable 'name' not found. > In img at 9 Reference to non-existent field 'data'.
Error in img (line 11) cube = filename.data;
___________________________________________________________
I really do not understand what is going on...??
Thank you in advance! Sophie
3 comentarios
Stephen23
el 18 de Mayo de 2017
Hello Sophie, you might like to read this (if you want to write better code, then it can help you):
Sophie Füchtner
el 22 de Mayo de 2017
@Sophie Füchtner: the section "Alternative: load into a Structure, not into the Workspace" is relevant to your situation.
Also you should note that it explained that it is much simpler to save and load data in a loop when the variable name does not change. This includes the variable names inside .mat files. Most likely your code could be significantly simplified by not saving lots of different variable names, but by simply doing something like this:
test_case = 'spruce'
test_data = [...]
save('test1.mat','test_case','test_data')
and then simply
S = load('test1.mat');
S.test_data
S.test_case
etc
You will note that this is then trivial to do in a loop.
Respuesta aceptada
Más respuestas (2)
Sophie Füchtner
el 24 de Mayo de 2017
0 votos
I had a similar problem. Here is a solution (maybe not the best one):
filename='*'; % you get it from somewhere
[path,name,ext] = fileparts(filename);
a=load(filename);
my_good_structure=a.(name);
and here you go.
2 comentarios
Stephen Cobeldick : Not exactly. He suggested :
matcontent = load('somefile.mat');
name = 'varname'; %varname is the name of a variable in the matfile
varcontent = matcontent.(name); %get the content of the variable.
If you compare, it is not exactly the same.
matcontent.(name)
would not exist.
matcontent.(somefile).name
might exist if any.
My solution is getting the whole structure and you should not know in advance - before loading the file - any 'varname'.
Categorías
Más información sobre Workspace Variables and MAT Files en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!