How to call columns from table when heading names are variable dependent
34 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I am trying to loop through different columns in a data table in order to plot multiple different sensor readings into graphs. It seems I am unable to dot reference column names when said names are changing throughout the loop functions - is there any way to do this?
Here is what I have so far, which currently returns errors using tabular/dotParenReference that won't recognise my currentColumn, baseColumn or MaxColumn variable names as column headers:
% import WIM data and sensor names list to be read
data = readtable("Processed_WIM_09_2023_Data.xlsx");
sensornames = readtable('ESGSensorNames.xlsx');
% start separate plots for Gross and Axle weights
GrossWeightPlot = figure(1);
ax1 = axes('Parent', GrossWeightPlot);
ax1.Title.String = 'Gross Weight ESG Responses';
ax1.XLabel.String = 'Gross Vehicle Weight (kg)';
ax1.YLabel.String = 'ESG responses (microstrain)';
AxleWeightPlot = figure(2);
ax2 = axes('Parent', AxleWeightPlot);
ax2.Title.String = 'Axle Weight ESG Responses';
ax2.XLabel.String = 'Axle Weight (kg)';
ax2.YLabel.String = 'ESG responses (microstrain)';
hold on
% loop through WIM data entries
for i = 1:height(data)
GrossWeight = data.GrossWeight;
% find axle weight columns according to axle number
for j = 1:data.AxlesCount(i)
AxleColumn = sprintf('AxleWeight%s', j);
AxleWeight = data.currentColumn(i);
% identify sensor columns
for k = 1:height(sensornames)
currentSensor = sensornames(k);
BaseColumn = sprintf('Base_%s', currentSensor);
MaxColumn = sprinft('Max_%s', currentSensor);
% calculate strain response (max - base)
strainResponse = data.MaxColumn(i) - data.BaseColumn(i);
% set plot colour and legend label according to slab
if contains(currentSensor, '7')
plotColour = 'b';
legendLabel = 'Slab 7';
elseif contains(currentSensor, '8')
plotColour = 'r';
legendLabel = 'Slab 8';
elseif contains(currentSensor, '11')
plotColour = 'g';
legendLabel = 'Slab 11';
end
% plot strain responses against weights
GWscatter = scatter(ax1, GrossWeight, strainResponse, '.', 'Color', plotColour, 'DisplayName', legendLabel);
AWscatter = scatter(ax2, AxleWeight, strainResponse, '.', 'Color', plotColour, 'DisplayName', legendLabel);
end
end
end
hold off;
% set plot legends
legend(GWscatter);
legend(AWscatter);
I can attach screenshots of my data table if need be, although it is an extremely large file so doing this in a clear manner is proving difficult.
1 commentaire
Stephen23
le 26 Fév 2025
"Dot notation, as in T.varname or T.(expression), extracts an array from one table variable."
Réponse acceptée
Fangjun Jiang
le 26 Fév 2025
Modifié(e) : Fangjun Jiang
le 26 Fév 2025
use data.(MaxColumn). This is similar to dynamic field names like below.
a.FirstColumn=1;
ColumnName='FirstColumn'
a.(ColumnName)
2 commentaires
Peter Perkins
le 3 Mar 2025
It's also possible to use variable indices in that syntax:
t = table([1;2;3],[4;5;6])
for i = 1:width(t)
t.(i)
end
But of course then you are not using your meaningful names.
Steven Lord
le 3 Mar 2025
Or you could use curly brace indexing to extract the contents inside a table, either with names or with indices.
t = table([1;2;3],[4;5;6])
N = t.Properties.VariableNames
for i = 1:width(t)
x = t{:, N{i}} % Use names
end
for i = 1:width(t)
x = t{:, i} % Use indices
end
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Matrix Indexing dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!