How to call columns from table when heading names are variable dependent

7 views (last 30 days)
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.

Accepted Answer

Fangjun Jiang
Fangjun Jiang on 26 Feb 2025
Edited: Fangjun Jiang on 26 Feb 2025
use data.(MaxColumn). This is similar to dynamic field names like below.
a.FirstColumn=1;
ColumnName='FirstColumn'
ColumnName = 'FirstColumn'
a.(ColumnName)
ans = 1
  2 Comments
Peter Perkins
Peter Perkins on 3 Mar 2025
It's also possible to use variable indices in that syntax:
t = table([1;2;3],[4;5;6])
t = 3x2 table
Var1 Var2 ____ ____ 1 4 2 5 3 6
for i = 1:width(t)
t.(i)
end
ans = 3×1
1 2 3
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
ans = 3×1
4 5 6
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
But of course then you are not using your meaningful names.
Steven Lord
Steven Lord on 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])
t = 3x2 table
Var1 Var2 ____ ____ 1 4 2 5 3 6
N = t.Properties.VariableNames
N = 1x2 cell array
{'Var1'} {'Var2'}
for i = 1:width(t)
x = t{:, N{i}} % Use names
end
x = 3×1
1 2 3
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
x = 3×1
4 5 6
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
for i = 1:width(t)
x = t{:, i} % Use indices
end
x = 3×1
1 2 3
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
x = 3×1
4 5 6
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

Sign in to comment.

More Answers (0)

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!