Storing output from each iteration of for loop of a structured data
1 view (last 30 days)
Show older comments
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 Comments
Answers (2)
KL
on 1 Nov 2017
Edited: KL
on 1 Nov 2017
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 Comments
KL
on 2 Nov 2017
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?
See Also
Categories
Find more on Loops and Conditional Statements 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!