How do I rename a structure?

20 vues (au cours des 30 derniers jours)
Aaron
Aaron le 26 Fév 2016
Commenté : Aaron le 26 Fév 2016
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 1x14 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 1x1 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 1x1 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 1x14 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

Réponse acceptée

Fangjun Jiang
Fangjun Jiang le 26 Fév 2016
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.
  1 commentaire
Aaron
Aaron le 26 Fév 2016
So just to confirm that I get what's happening here, this reads the name of the structure I loaded into "filename", and saves it to a cell:
StrName=fieldnames(filename);
Then this converts the cell into a string:
StrName=StrName{1};
And this calls the structure I loaded into "filename" using that string:
filename.(StrName).a
This is just what I was trying to do. Thanks so much for the help!
Aaron

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Structures dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by