How to use 'for' loop to string variable ?
124 次查看(过去 30 天)
显示 更早的评论
I have severals mat files. I have defined these files as a string variable in m file. i can execute the files seperately with a loop.
1st mat file: Battery_Power_280.mat
2nd mat file: Battery_Power_300.mat
3rd mat file: Battery_Power_320.mat
4th mat file: Load_Power_280.mat
If i execute first 3 files its executed successfully. but whenever i add the 4th file(Load_Power_280) with the 'Name' variable its showing this error.
Error using horzcat
Names of fields in structure arrays being concatenated do not match. Concatenation of structure arrays requires that these arrays have the same set of fields.
character length of first 3 files is 17 indibidually. and for the last file is 14. is it the reason ?
My CODE with first 3 files:
Name=["Battery_Power_280","Battery_Power_300","Battery_Power_320"];
N = numel(Name);
C = cell(1,N);
for i = 1:N
C{i} = load(Name{i});
end
Structure = [C{:}]; % its executed successfully.
8 个评论
Voss
2021-12-17
S = load('Battery_Power_280.mat')
S = load('Load_Power_280.mat')
These are what get stored as elements of C in the code. One is a struct with one field called 'Data_BatteryPower', and one is a struct with one field called 'Data_LoadPower'. The set of field names is not the same between the two, so they cannot be concatenated in a struct array. This is why the error happens.
One solution is to reference the field 'Data_BatteryPower' for the mat files that have 'Data_BatteryPower' and reference the field 'Data_LoadPower' for the mat files that have 'Data_LoadPower', and concatenate those structs. But the real question is whether these things really need to be concatenated in a struct array at all, or would a cell array of structs - which does allow each element to have a different set of field names - be sufficient? The answer to that question depends on how you use the variable Structure further down in your code.
采纳的回答
更多回答(3 个)
Voss
2021-12-17
Here's one way to make a struct array, as intended in the code you posted in your question (note that this just uses two of the mat files and explicitly states the field to use (in the variable type)):
Name = ["Battery_Power_280","Load_Power_280"];
type = ["Data_BatteryPower","Data_LoadPower"];
N = numel(Name);
C = cell(1,N);
for i = 1:N
C{i} = subsref(load(Name{i}),substruct('.',type{i}));
end
Structure = [C{:}]
KSSV
2021-12-17
Why you want to make names manually? You can get the files present in the folder using:
matFiles = dir('*.mat') ;
for i = 1:length(matFiles)
load(matFiles(i).name)
end
9 个评论
Stephen23
2021-12-17
编辑:Stephen23
2021-12-17
"all mat files have same types of data"
No, the variable names are different.
When you LOAD into a structure those variable names are given as different fieldnames (of the scalar structures inside the cell array). Scalar structures of different fieldnames cannot be concatenated together.
Arif Hoq
2021-12-20
10 个评论
Stephen23
2021-12-22
"using actual indices is lengthy process i guess. "
No, using actual indices is the simplest approach. It makes it trivial and very efficient to write a loop to process your data, something that would be complex and very inefficient using your approach.
Why do you think that it would be "lengthy"? (when the reality is that it would be simpler and easier for you)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Structures 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!