Trying to add a variable RowName based off for loop.

19 views (last 30 days)
Jacob Lobao
Jacob Lobao on 25 Mar 2019
Edited: 埃博拉酱 on 28 Oct 2024 at 10:49
Hello, I am trying to create a table where each row name is the name of the file that I am manipulating. I was able to get it to work the first iteration, but after that im having trouble. I figured the way I had it would work, but am having trouble. Any help would be appreciated, thanks! Screenshot (2).png
textfiles = dir('*.txt');
library = cell(1,length(textfiles));
matrix = zeros(50,7);
avgvec = ones(1,7);
avgmatrix = zeros(length(textfiles),7);
for ii = 1:length(textfiles)
fid = fopen(textfiles(ii).name);
library{ii} = readtable(textfiles(ii).name, 'Delimiter', '\t', 'ReadVariableNames', false, 'HeaderLines', 9);
matrix = library{ii}(:,2:8).Variables;
avgvec = mean(matrix);
avgmatrix(ii,:) = avgvec;
avgtablevec = array2table(avgvec, 'RowNames', {textfiles(ii).name}, 'VariableNames', {'q', 'V_ref', 'alpha', 'NormalForce', 'AxialForce', 'Moment', 'P'});
averagetable(ii, :) = avgtablevec;
end

Answers (3)

Himanshu
Himanshu on 28 Oct 2024 at 9:34
Hey Jacob,
MATLAB tables don't directly support dynamic row names assignment in the way you're attempting. Instead, you should create a table with all the data first and then assign the row names separately.
You can refer to the following code:
textfiles = dir('*.txt');
numFiles = length(textfiles);
avgmatrix = zeros(numFiles, 7);
rowNames = cell(numFiles, 1);
for ii = 1:numFiles
% Read the file and extract the relevant data
library = readtable(textfiles(ii).name, 'Delimiter', '\t', 'ReadVariableNames', false, 'HeaderLines', 9);
matrix = library{:, 2:8};
% Calculate the mean for each column
avgvec = mean(matrix);
avgmatrix(ii, :) = avgvec;
% Store the filename for row naming
rowNames{ii} = textfiles(ii).name;
end
% Create the table with the calculated averages
averagetable = array2table(avgmatrix, 'VariableNames', {'q', 'V_ref', 'alpha', 'NormalForce', 'AxialForce', 'Moment', 'P'});
% Assign the row names
averagetable.Properties.RowNames = rowNames;
% Display the table
disp(averagetable);
You may also go through this Matlab answer thread, which is a discussion on the same issue:

Stephen23
Stephen23 on 28 Oct 2024 at 10:44
You can certainly add rownames dynamically in a loop, but not by indexing one table into an existing table.
Nrow = 5;
Ncol = 7;
T = table('Size',[Nrow,Ncol], 'VariableTypes',repmat("double",1,Ncol));
for k = 1:Nrow
T{k,:} = rand(1,Ncol); % row values
T.Properties.RowNames(k) = "MyRow"+k; % row name
end
display(T)
T = 5x7 table
Var1 Var2 Var3 Var4 Var5 Var6 Var7 _______ ________ ________ _______ _______ ________ _______ MyRow1 0.64754 0.034667 0.21142 0.64744 0.94951 0.044792 0.92204 MyRow2 0.92099 0.93698 0.17448 0.44149 0.34022 0.64313 0.66402 MyRow3 0.62632 0.66785 0.82359 0.51254 0.83964 0.89535 0.13099 MyRow4 0.97673 0.7379 0.35095 0.73779 0.13757 0.49799 0.17754 MyRow5 0.2926 0.22048 0.028628 0.92226 0.14278 0.78226 0.40858

埃博拉酱
埃博拉酱 on 28 Oct 2024 at 10:46
Edited: 埃博拉酱 on 28 Oct 2024 at 10:49
Table is what you really need to pre-allocate, and you don't need other variables at all.
textfiles = dir('*.txt');
library = cell(1,length(textfiles));
averagetable=table;%Tables are what you really need to pre-assign
for ii = 1:length(textfiles)
% fid = fopen(textfiles(ii).name);%This variable is not used at all, and you forgot to fclose the file you opened
library{ii} = readtable(textfiles(ii).name, 'Delimiter', '\t', 'ReadVariableNames', false, 'HeaderLines', 9);
%Tables should be indexed directly with row and column names
averagetable{textfiles(ii).name, {'q', 'V_ref', 'alpha', 'NormalForce', 'AxialForce', 'Moment', 'P'}} = mean(library{ii}{:,2:8},1);
end

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!