How do I plot different fields of a structure in a loop?

2 ビュー (過去 30 日間)
Mackenzie Maher
Mackenzie Maher 2021 年 10 月 15 日
コメント済み: Image Analyst 2022 年 12 月 5 日
Hi all.
I'm very new to Matlab and I'm trying to create a loop that plots everything between these 2 lines of code:
scatter(MCR_full.MIB035.Reaches.R32.kin(:,1),MCR_full.MIB035.Reaches.R32.kin(:,3))
scatter(MCR_full.MIB035.Reaches.R42.kin(:,1),MCR_full.MIB035.Reaches.R42.kin(:,3))
so it would plot the following
scatter(MCR_full.MIB035.Reaches.R32.kin(:,1),MCR_full.MIB035.Reaches.R32.kin(:,3))
scatter(MCR_full.MIB035.Reaches.R33.kin(:,1),MCR_full.MIB035.Reaches.R33.kin(:,3))
scatter(MCR_full.MIB035.Reaches.R34.kin(:,1),MCR_full.MIB035.Reaches.R34.kin(:,3))
and so on all the way to
scatter(MCR_full.MIB035.Reaches.R42.kin(:,1),MCR_full.MIB035.Reaches.R42.kin(:,3))
all in the same plot. The RNN field name changes with each call to scatter.
Any help with this would be fantastic thanks so much

採用された回答

Image Analyst
Image Analyst 2021 年 10 月 15 日
Use dynamic field names:
% Create sample data
MCR_full.MIB035.Reaches.R32.kin = rand(100, 3)
MCR_full.MIB035.Reaches.R33.kin = rand(100, 3)
MCR_full.MIB035.Reaches.R34.kin = rand(100, 3)
MCR_full.MIB035.Reaches.R41.kin = rand(100, 3)
MCR_full.MIB035.Reaches.R42.kin = rand(100, 3)
% Get all the fieldnames.
fieldNames = fieldnames(MCR_full.MIB035.Reaches)
% Get data from each field and plot it.
for k = 1 : numel(fieldNames)
thisFieldName = fieldNames{k};
fprintf('Plotting %s.\n', thisFieldName);
thisArray = MCR_full.MIB035.Reaches.(thisFieldName).kin;
x = thisArray(:, 1);
y = thisArray(:, 3);
% Unique, random color for each.
thisColor = rand(1, 3);
% Do the scatter plot
scatter(x, y, 30, thisColor, 'filled');
hold on; % Don't let subsequent plots blow away earlier ones.
end
grid on;
  3 件のコメント
Image Analyst
Image Analyst 2022 年 12 月 5 日
@Josh Tome, use ismember to get a list of those fieldnames which are in the list of ones you want to keep.
% Create sample data
MCR_full.MIB035.Reaches.R32.kin = rand(100, 3);
MCR_full.MIB035.Reaches.R33.kin = rand(100, 3);
MCR_full.MIB035.Reaches.R34.kin = rand(100, 3);
MCR_full.MIB035.Reaches.R41.kin = rand(100, 3);
MCR_full.MIB035.Reaches.R42.kin = rand(100, 3);
% Get all the fieldnames.
fieldNames = fieldnames(MCR_full.MIB035.Reaches);
fieldNamesToPlot = {'R33', 'R34', 'R42'};
indexesToKeep = ismember(fieldNames, fieldNamesToPlot)
indexesToKeep = 5×1 logical array
0 1 1 0 1
fieldNames = fieldNames(indexesToKeep)
fieldNames = 3×1 cell array
{'R33'} {'R34'} {'R42'}
% Get data from each field and plot it.
for k = 1 : numel(fieldNames)
thisFieldName = fieldNames{k};;
fprintf('Plotting %s.\n', thisFieldName);
thisArray = MCR_full.MIB035.Reaches.(thisFieldName).kin;
x = thisArray(:, 1);
y = thisArray(:, 3);
% Unique, random color for each.
thisColor = rand(1, 3);
% Do the scatter plot
scatter(x, y, 30, thisColor, 'filled');
hold on; % Don't let subsequent plots blow away earlier ones.
legendStrings{k} = thisFieldName;
end
Plotting R33. Plotting R34. Plotting R42.
grid on;
legend(legendStrings)
Image Analyst
Image Analyst 2022 年 12 月 5 日
@Josh Tome not sure what "I would also like to plot over the previous field, but I suspect that is a matter of not using the hold on function." means. My code plots what you wanted to plot. If you don't have hold on, it will blow away everything and plot everything from scratch. If you have hold on it will add those data markers to the existing plot. Did you try the code in my last comment? If not, why not?

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGraphics Performance についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by