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

조회 수: 1 (최근 30일)
Fida Rafi
Fida Rafi 2017년 11월 1일
댓글: Fida Rafi 2017년 11월 10일
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

답변 (2개)

KL
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
Fida Rafi
Fida Rafi 2017년 11월 2일
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 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?

댓글을 달려면 로그인하십시오.


Fida Rafi
Fida Rafi 2017년 11월 9일
Eval command solved the problem. Cheers
  댓글 수: 3
Jan
Jan 2017년 11월 9일
@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 2017년 11월 10일
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

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by