load structure array with a function and extract the variables

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 个评论

I did read this your link, and it was interesting, but I seem to either not get your answer or the other way round. in any case i did not find a solution to my problem.
@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.

请先登录,再进行评论。

 采纳的回答

I did not understand exactly what you're trying to do. It sounds like you are trying to name your variables depending on the file that you load, which is never a good idea.
However, I can certainly explain the error message. The mat file that you are trying to load a file that contains a variable of type dataset. The version of matlab that you are using does not know what a dataset is. It's because either you're missing the required toolbox (e.g. there's a dataset type in the statistics and machine learning toolbox) or because it's a custom type for which you're missing the m file (in which case you need to ask whoever saved that mat file for the required m files).
In any case, without the required toolbox or necessary m file, you will not be able to load that mat file properly. You may be able to load part of the file using matfile but you'll always be missing the dataset.

9 个评论

hmm...but i can see the structure array in my workspace, and all of the variables I want to extract are there...
@Sophie: just access them from the structure. There is no point in copying the data to new variables.
@Guillaume: I have the matlab version 2014b; the dataset is exported from another program where I have no influence on - neither will I get any .m file. i do have the statistics toolbox - is it wrong for me to work with structure instead of a dataset? (i do not really understand the difference in this context)
@Stephen: I am lost now. My problem is, that the structure has a different name every time, and i don't know how to write a general code in this case...
PS: the name of the structure is stored in the a character string called name...
Sophie, to clarify on the error message. It is saying that in the mat file you have an object named Norway_spruce_not_extracted_RA_ray. That object is of a class called dataset (which may or may not be the dataset of the stats toolbox). Matlab is telling you it doesn't know how to load that object so instead of loading it as an object, it loads it as a structure (the object properties are converted to fields of the structure)
This means that you've still got access to all the object properties, which is good for you, but you no longer can use all the methods of the object, which may or may not be a problem for you.
With regards to your later comment. The name of the structure should always be the same, since it's the name you write in your code:
matcontent = load('somefile.mat');
With the line above, the name of the structure is always matcontent.
What may change is the names of the fields within the structure. If you know that a variable in the mat file has a name held in the char array name, you can easily access it:
name = 'varname'; %varname is the name of a variable in the matfile
varcontent = matcontent.(name); %get the content of the variable.
Guillaume, thank you for your time :)
unfortunately using
matcontent = load('somefile.mat')
creates a structure, containing the structure that i want to load...so it adds one more level to my data, and i don t get rid of the problem of not being able to access the data...see img... and i really don t undestand why it packs it in a superstructure instead of just giving it the name i assign to it?
@Sophie Füchtner: you get a structure in a structure because a .mat file can have multiple variables saved in it, and yet there is just one output. The most convenient way to achieve this is to use a structure, as then every variable can be accessed simply using the name of the variable as the fieldname, exactly as Guillaume already showed.
The fact that your variable is also a structure is irrelevant, as there is no special case to treat this any differently to as if this was any other kind/s of array in a .mat file. Think about the implications of having multiple variables in one .mat file, and how these would need to be loaded into one variable (hint: every variable gets its own field). That is what you have to access.
Stephen has already explained why it packs your variable in what you call a 'superstructure'. That's the standard behaviour of load.
Note that according to the error message, that Norway_spruce_not_extracted_RA_ray should be a 1x1 dataset. But because matlab does not know how to load that properly, it loads it as a 1x1 struct instead. That's a problem that you probably ought to fix but not related to the discussion here.
I'm going to guess that your problem is that Norway_spruce_not_extracted_RA_ray variable changes name from file to file. The proper fix would be to modify whatever creates these files in the first place to use the same name all the time. Assuming you want to give it a fix name for using in your code, and assuming it is the only variable in the file:
fn = fieldnames(matcontent);
agoodvarname = matcontent.(fn{1}); %give fixed name agoodvarname to whichever variable is in the file.
I will try again and let you know if I succeed (since - as you might guess - I am new to matlab, this is quite complicated for me^^)
for now, thank you alot Stephen and Guillaume :)

请先登录,再进行评论。

更多回答(2 个)

Hi,
just to let you know, I decided to just load the file and rename the structure by hand into a generic name, after which my scripts can be applied to that. It is not as "perfect" as I wanted it to be, but it works and I can move on :)
Thank you for your time. Best, Sophie
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 个评论

It is a good solution, which was already suggested by Guillaume in this comment above.
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'.

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Workspace Variables and MAT Files 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by