How to call columns from table when heading names are variable dependent
26 ビュー (過去 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.
1 件のコメント
Stephen23
2025 年 2 月 26 日 14:05
"Dot notation, as in T.varname or T.(expression), extracts an array from one table variable."
採用された回答
Fangjun Jiang
2025 年 2 月 26 日 13:21
編集済み: Fangjun Jiang
2025 年 2 月 26 日 13:25
use data.(MaxColumn). This is similar to dynamic field names like below.
a.FirstColumn=1;
ColumnName='FirstColumn'
a.(ColumnName)
2 件のコメント
Peter Perkins
2025 年 3 月 3 日 18:22
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
2025 年 3 月 3 日 19:08
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
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Matrix Indexing についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!