How do I rename a structure?
20 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Aaron
el 26 de Feb. de 2016
Comentada: Aaron
el 26 de Feb. de 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
0 comentarios
Respuesta aceptada
Fangjun Jiang
el 26 de Feb. de 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.
Más respuestas (0)
Ver también
Categorías
Más información sobre Structures en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!