How do I rename a structure?
20 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Fangjun Jiang
2016-2-26
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Structures 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!