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

2 visualizzazioni (ultimi 30 giorni)
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

Risposta accettata

Image Analyst
Image Analyst il 15 Ott 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 Commenti
Image Analyst
Image Analyst il 5 Dic 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)
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 il 5 Dic 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?

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Graphics Performance in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by