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!