graph multiple fields in a struct

45 ビュー (過去 30 日間)
Boris Chan
Boris Chan 2019 年 2 月 18 日
編集済み: Kevin Phung 2019 年 2 月 18 日
I have a struct with multiple fields named data1, data2, data3 etc up to 9 I want to graph the first two colums of each field
I used this
position = variable.data1(:,2);
velocity = variable.data1(:,3);
%then i did
plot(position,velocity)
This worked fine but is there anyway to do this in a loop to make it faster? In the future I may have data up to 100s of different arrays

採用された回答

Kevin Phung
Kevin Phung 2019 年 2 月 18 日
編集済み: Kevin Phung 2019 年 2 月 18 日
fld = fieldnames(variable); %list of all field names.. data1, data2,data3...
for i = 1:numel(fld) % for each field name
m = variable.(fld{i}); %this will be the data1,data2,data3 matrix
pos = m(:,2);%index matrix for position
vel = m(:,3); %velocity
plot(pos,vel); %plot results
hold on; % keep plot for next plot
end

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLine Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by