Info

此问题已关闭。 请重新打开它进行编辑或回答。

Indexing Values from a structure array

2 次查看(过去 30 天)
Tyrell Warrick
Tyrell Warrick 2019-3-6
关闭: MATLAB Answer Bot 2021-8-20
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 个评论
darova
darova 2019-3-6
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

回答(1 个)

Agnish Dutta
Agnish Dutta 2019-3-18
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:

此问题已关闭。

产品


版本

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by