Trying to add a variable RowName based off for loop.
19 views (last 30 days)
Show older comments
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!
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
0 Comments
Answers (3)
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:
0 Comments
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)
0 Comments
埃博拉酱
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
0 Comments
See Also
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!