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

34 次查看(过去 30 天)
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.

采纳的回答

Fangjun Jiang
Fangjun Jiang 2025-2-26
编辑:Fangjun Jiang 2025-2-26
use data.(MaxColumn). This is similar to dynamic field names like below.
a.FirstColumn=1;
ColumnName='FirstColumn'
ColumnName = 'FirstColumn'
a.(ColumnName)
ans = 1
  2 个评论
Peter Perkins
Peter Perkins 2025-3-3
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 2025-3-3
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>

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

产品


版本

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by