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 个评论
Arif Hoq
Arif Hoq 2021-12-17
attached a screenshot for your information. is this information enough for you? if not please dont hesitate to reply. thank you.
upper variable result for S, S1 in the lower right side. As both variable you wrote as S, thats why i wrote S and S1.
Voss
Voss 2021-12-17
S = load('Battery_Power_280.mat')
S = struct with fields:
Data_BatteryPower: [1×1 struct]
S = load('Load_Power_280.mat')
S = struct with fields:
Data_LoadPower: [1×1 struct]
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.

请先登录,再进行评论。

采纳的回答

Stephen23
Stephen23 2021-12-17
编辑:Stephen23 2021-12-17
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{:}]
S = 1×3 struct array with fields:
time signals blockName
  1 个评论
Arif Hoq
Arif Hoq 2021-12-17
编辑:Arif Hoq 2021-12-22
Great @Stephen. Thanks a lot. i have added few lines to export my expected variable(signal.values)
T = ["Battery_Power_280","Battery_Power_300","Battery_Power_320","Load_Power_280.mat"];
N = numel(T);
C = cell(1,N);
for k = 1:N
C(k) = struct2cell(load(T{k});
end
S = [C{:}];
% Export the signal value
num = numel(S);
D = cell(1,num);
for j=1:num
D{j}=S(j).signals.values(:,1);
end
Matrix=[D{:}];

请先登录,再进行评论。

更多回答(3 个)

Voss
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{:}]
Structure = 1×2 struct array with fields:
time signals blockName
  1 个评论
Arif Hoq
Arif Hoq 2021-12-17
this one also helpful in case of getting type. I appreciate your hard work.

请先登录,再进行评论。


KSSV
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 个评论
Arif Hoq
Arif Hoq 2021-12-17
all mat files have same types of data.please check the latest screenshot. Even you open the mat files from your end. I appreciate your hard work.
Stephen23
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
Arif Hoq 2021-12-20
trying something new. want to export all mat files as@KSSV suggested. applied this code:
files = dir('*.mat') ;
for k = 1:length(files)
load(files(k).name);
end
20 mat files exist in my folder. but after execution it shows only 4 mat files data(Actually got these 4 mat files from simulink. after simulation i have renmaed the files like "Battery_Power_280","Battery_Power_300","Battery_Power_320","Grid_Power_280","Grid_Power_300" so on...).
actual simulated mat files name: "Data_BatteryPower,Data_GridPower,Data_LoadPower,Data_PVPower"
Is it possible to export all data from these 20 mat files ? please check the screenshot for further information.
  10 个评论
Stephen23
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)
Arif Hoq
Arif Hoq 2021-12-22
its simpler and easier,i admit that. i will follow your hints.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Structures 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by