Trying to add a variable RowName based off for loop.
19 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(3 个)
Himanshu
2024-10-28,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 个评论
Stephen23
2024-10-28,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 个评论
埃博拉酱
2024-10-28,10:46
编辑:埃博拉酱
2024-10-28,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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrices and Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!