Automatically load a struct from a struct with a variable name
显示 更早的评论
Hi,
I am struggeling with hopefully only a small problem.
Maybe one of you could help me.
Problem:
I want to load automatically *.mat files with a variable name and get only a defined sub-struct out of it.
The loaded file is a struct and in this struct is a struct with a variable name according to the file-name.
And in this struct is another struct, which I need of course, with a fix fieldname.
Example:
- values = load (test1.mat) , result in workspace = values (struct)
- Content of values = struct with the name test1
- Content of test1 = 4 structs with fix field names (a, b, c, d)
Command for a file with a fix name is:
s = values.test1.a; % or .b, .c, .d
I am searching for a command to get only the contect of one sub-struct with the fix field name from a struct with a variable file-name.
And this should work without any further user input. (The path to the sub-struct should be generated automatically.)
Thank you.
2 个评论
Jan
2022-11-29
"content of one sub-struct with the fix field name from a struct with a variable file-name" - which of the mentioned field names is fixed und which is variable? Is "test1" variable? If so, does it equal the file name?
Martin Möhring
2022-11-29
采纳的回答
更多回答(1 个)
Jan
2022-11-29
file = 'test1.mat'
[~, name] = fileparts(file);
values = load(file);
subStruct = values.(name);
a = subStruct.a;
etc.
类别
在 帮助中心 和 File Exchange 中查找有关 Structures 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!