Storing output from each iteration of for loop of a structured data
1 回表示 (過去 30 日間)
古いコメントを表示
Hi all,
I have structured.mat file for 10 device and each device has 4 columns and 131040 rows. The filednames are Device1,2,3...
The four column represents time, voltage, current and angle. I want to calculate power for all 10 devices, hence want to get access to each device data.
However, I can't save the output of all the 10 devices if I run the following code:
load ('structured.mat');
for k = 1:10
chk = sprintf('Device%d', k);
D = sprintf('D%d', k)
D = structured.(chk);
end
The output shows only the Device 10 data, but I need all 10 devices data. If it were some numbers, I could have used a vector to store all outputs.
But I want the for loop to give me Device1, Device2,... Device10 output instead of just Device10.mat output.
Your help would be highly appreciated.
Cheers
0 件のコメント
回答 (2 件)
KL
2017 年 11 月 1 日
編集済み: KL
2017 年 11 月 1 日
Use indexing,
I haven't paid much attention to your code but something like the following should work,
chk(k) = sprintf('Device%d', k);
D(k) = structured.(chk(k));
Anyway, I'd suggest revamp your data and use a table for your purpose. Since you have 10 devices, use cell array of tables. Imagine something like,
deviceData{1,1} = table(structured.Device1,'v',{'time','volage','current','angle'};
you can do the same for all devices.
And then you can also easily calculate power and add it to the same table, for example,#
device1.Power = device1.current*device1.voltage %something like that
2 件のコメント
KL
2017 年 11 月 2 日
Yes, you probably would need to use a loop. Could you create a dummy data with a similar structure (say 3 devices and just 5 measurements on each) and attach it here as a mat file?
参考
カテゴリ
Help Center および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!