Storing output from each iteration of for loop of a structured data

1 visualizzazione (ultimi 30 giorni)
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

Risposte (2)

KL
KL il 1 Nov 2017
Modificato: KL il 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 Commenti
Fida Rafi
Fida Rafi il 2 Nov 2017
Thanks KL for your answer. I tried the D(k) = structured.(chk(k)); command but it doesn't work.
I actually have more than 600 device information for a whole year with 1 min resolution. The 10 devices were just an example. Sorry for the confusion.
My target is to get each Device data from the single structured mat file and apply corresponding formulas for all the Devices.
Thats why I thought using for loop would be easier.
KL
KL il 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?

Accedi per commentare.


Fida Rafi
Fida Rafi il 9 Nov 2017
Eval command solved the problem. Cheers
  3 Commenti
Jan
Jan il 9 Nov 2017
@Fida Rafi: EVAL is a really bad idea. Search for a solution, which does not cause more troubles than it solves.
Fida Rafi
Fida Rafi il 10 Nov 2017
I couldn't find alternative way unfortunately. Would really appreciate if you could share your suggestions.
I need to access data from different columns from each Devices to calculate powers which I need to use in Simulink model, probably using an S function.
Here is my current code:
load ('structured_data.mat')
c = struct2cell(structured_data);
[row,col]= size(c);
for s=1:c
try
chk = sprintf('Device%d',s);
%D = sprintf('D%d',s)
D = structured_data.(chk);
eval(['Device' num2str(s) '= h']);
catch
% will skip the iteration if device info can't be found
end
end

Accedi per commentare.

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by