Clear Filters
Clear Filters

Info

This question is closed. Reopen it to edit or answer.

Indexing Values from a structure array

1 view (last 30 days)
Tyrell Warrick
Tyrell Warrick on 6 Mar 2019
Closed: MATLAB Answer Bot on 20 Aug 2021
Positive_Torque_Values = double.empty;
Speed_Values = double.empty;
index = 1;
index2 = 1;
for i = HL_simout.Motor_Torque.Data(index)
if HL_simout.Motor_Torque.Data(index) >= 0
Positive_Torque_Values(index)= HL_simout.Motor_Torque.Data(index) ;
Speed_Values(index2)= HL_simout.Motor_Speed.Data(index);
index2 = index2 + 1;
end
index = index + 1;
end
a = size(Speed_Values);
b = size(Positive_Torque_Values);
disp(a)
disp(b)
title('Motor Torque Vs Motor Speed')
scatter(Speed_Values,Positive_Torque_Values)
xlabel('Motor Speed [RPM]')
ylabel('Motor Torque [Nm]')
I'm trying to take values from a column of data stored in a structure created by MATLAB simulink and take all the values grearer than zero(0) and store it in a seprate array. However, im only getting one value in the new array hence its not going through the entire structure list
  1 Comment
darova
darova on 6 Mar 2019
index2 = 1;
n = length(HL_simout.MOtor_Torque.Data); % number of rows
for i = 1:n
if HL_simout.Motor_Torque.Data(i) >= 0
Positive_Torque_Values(index2)= HL_simout.Motor_Torque.Data(i) ;
Speed_Values(index2)= HL_simout.Motor_Speed.Data(i);
index2 = index2 + 1;
end
end

Answers (1)

Agnish Dutta
Agnish Dutta on 18 Mar 2019
You can use logical indexing to obatin the non zero values from the structure arrays as per the code below:
Positive_Torque_Values = HL_simout.Motor_Torque.Data(HL_simout.Motor_Torque.Data > 0);
Speed_Values = HL_simout.Motor_Speed.Data(HL_simout.Motor_Torque.Data > 0);
From the code that you've provided, I'm assuming that you want the values of Speed and Torque at all those points where the Torque is greater than zero.
Refer to the logical indexing section of the following document for more information:

This question is closed.

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!