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 CenterFile Exchange에서 Graphics Performance에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by