How do I plot different fields of a structure in a loop?
2 views (last 30 days)
Show older comments
Mackenzie Maher
on 15 Oct 2021
Commented: Image Analyst
on 5 Dec 2022
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
0 Comments
Accepted Answer
Image Analyst
on 15 Oct 2021
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 Comments
Image Analyst
on 5 Dec 2022
@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)
fieldNames = fieldNames(indexesToKeep)
% 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
grid on;
legend(legendStrings)
Image Analyst
on 5 Dec 2022
@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?
More Answers (0)
See Also
Categories
Find more on Graphics Performance in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!