How to call columns from table when heading names are variable dependent
Mostrar comentarios más antiguos
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 comentario
Stephen23
el 26 de Feb. de 2025
"Dot notation, as in T.varname or T.(expression), extracts an array from one table variable."
Respuesta aceptada
Más respuestas (0)
Categorías
Más información sobre Operators and Elementary Operations en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!