MATLAB Answers

Dealing with Data Structures

조회 수: 4(최근 30일)
Abdulrrahman AlGadi
Abdulrrahman AlGadi 2021년 9월 14일
댓글: Stephen 2021년 9월 15일
Hello all!
I am importing data from a .TDMS file and converting into CSV format. So, after converting it I get the data as struct type.
My issue is that I want to assign different arrays in the struct to different variables and would like to use for loop to it for code simplicity.
For examle:
Data is the struct variable.
Assigning variables code:
impact_data0 = Data.Configuration0_Impact00.cDAQ1Mod1_ai0.data'; ---> 1
accel_data0(:,1) = Data.Configuration0_Impact00.cDAQ1Mod1_ai1.data'; --->2
accel_data0(:,2) = Data.Configuration0_Impact00.cDAQ1Mod1_ai2.data'; ---> 3
accel_data0(:,3) = Data.Configuration0_Impact00.cDAQ1Mod1_ai3.data'; ---> 4
impact_data1 = Data.Configuration0_Impact01.cDAQ1Mod1_ai0.data';
accel_data1(:,1) = Data.Configuration0_Impact01.cDAQ1Mod1_ai1.data';
accel_data1(:,2) = Data.Configuration0_Impact01.cDAQ1Mod1_ai2.data';
accel_data1(:,3) = Data.Configuration0_Impact01.cDAQ1Mod1_ai3.data';
What I would like to do is to repeat the same first four lines and change the impact number only [in bold]
Thanks !
  댓글 수: 1
Stephen
Stephen 2021년 9월 15일
"My issue is that I want to assign different arrays in the struct to different variables and would like to use for loop to it for code simplicity."
Simple and efficient code would not change the variable name, it would just use indexing.

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

답변(2개)

Dave B
Dave B 2021년 9월 14일
편집: Dave B 2021년 9월 14일
I think in general it's better if your struct is an array rather than using names of fields in the struct as an array, but if you're stuck with this from the import, it's actually not too difficult to dynamically specify a field name, you just need to put it in parentheses:
s = struct('Var01',pi,'Var02',2*pi,'Var03',3*pi);
vals = nan(1,3);
for i = 1:3
varname = sprintf('Var%02d',i);
vals(i) = s.(varname);
end
disp(vals)
3.1416 6.2832 9.4248
  댓글 수: 1
Dave B
Dave B 2021년 9월 14일
after seeing @Steven Lord's post I realized that you were also trying to name your outputs, I'd avoid this (as Steven explains). If differences in sizes make it difficult to use his method, use a cell array:
s = struct('Var01', sin(linspace(0,2*pi,50)), 'Var02', cos(linspace(0,2*pi,100)));
vals = cell(1,2);
for i = 1:2
varname = sprintf('Var%02d',i);
vals{i} = s.(varname);
end
disp(vals)
{1×50 double} {1×100 double}

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


Steven Lord
Steven Lord 2021년 9월 14일
Can you create variables named impact_data0, impact_data1, impact_data2, etc.? Yes.
Should you do this? The general consensus is no.
If you wanted to access the data from each struct array in turn and operate on it (like in a for loop) I'd use a temporary variable (with a fixed name) to store the data to be processed. You can use the technique Dave B posted to get the data into that temporary variable. Once you've completed processing then store the finished results in a section of a (fixed named) variable.
Note that the code below has no temp1, temp2, temp3, etc. variables. I'm using randi rather than indexing into a struct array because it's quicker to write as an example, but the general technique will still work with struct data.
finalResults = zeros(3, 5);
for k = 1:5
temp = randi(10, 3, 3)
s = sum(temp, 2)
finalResults(:, k) = s;
end
temp = 3×3
4 5 4 4 6 6 6 4 10
s = 3×1
13 16 20
temp = 3×3
5 2 7 8 9 2 8 3 6
s = 3×1
14 19 17
temp = 3×3
6 5 2 10 7 9 10 3 10
s = 3×1
13 26 23
temp = 3×3
4 10 7 7 4 8 2 9 6
s = 3×1
21 19 17
temp = 3×3
1 6 6 9 8 7 10 3 2
s = 3×1
13 24 15
finalResults
finalResults = 3×5
13 14 13 21 13 16 19 26 19 24 20 17 23 17 15

Community Treasure Hunt

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

Start Hunting!

Translated by