MATLAB: How to rename a structure

structures

Hello all,
I'm using a software that exports recorded data as a Matlab structure, then analyzing the results with a Matlab script. The fields in each structure are named identically. (It is a 1×14 structure with 50 fields).
I want to write a loop to process each structure sequentially, reading the names from an array or .txt file. When I try the following, it creates a 1×1 structure named "filename" with my data structure inside it, then I have to still use the name of that file when accessing the data inside it.
filename = load('MeasureData_20160123_094305_3')
I would like to have "filename" be identical to the structure I loaded, not a 1×1 structure with the data inside it. That way I can do something like this:
plot(filename(11).x, filename(11).y);
Instead of this:
plot(filename.MeasureData_20160123_094305_3(11).x, filename.MeasureData_20160123_094305_3(11).y);
I have also tried this, which produces the 1×14 structure I want, but it still requires me to manually enter the file name:
load('MeasureData_20160123_094305_3')
a = MeasureData_20160123_094305_3
How can I automate this process so I don't have to manually change the filename in my script? Thanks for the help, Aaron

Best Answer

This is primarily due to the fact that the top level structure name of your data is varying and the name is long. You can use "dynamic field names" to solve the problem. Below is an example, hope you can get the point.
clear;
TopStruct.a=1;
TopStruct.b=2;
save;
filename=load;
StrName=fieldnames(filename);
StrName=StrName{1};
filename.(StrName).a
No matter what your file name or top level structure name, the above code should work.
Related Question