How to use 'for' loop to string variable ?
123 views (last 30 days)
Show older comments
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 Comments
Voss
on 17 Dec 2021
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.
Accepted Answer
Stephen23
on 17 Dec 2021
Edited: Stephen23
on 17 Dec 2021
Assuming that each file contains exactly one variable (itself a structure with the same field names):
T = ["Battery_Power_280","Battery_Power_300","Load_Power_280.mat"];
N = numel(T);
C = cell(1,N);
for k = 1:N
C(k) = struct2cell(load(T{k}));
end
S = [C{:}]
More Answers (3)
Voss
on 17 Dec 2021
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
on 17 Dec 2021
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 Comments
Stephen23
on 17 Dec 2021
Edited: Stephen23
on 17 Dec 2021
"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
on 20 Dec 2021
10 Comments
Stephen23
on 22 Dec 2021
"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)
See Also
Categories
Find more on Structures in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!