Trying to add a variable RowName based off for loop.
조회 수: 7 (최근 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일
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일
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일
편집: 埃博拉酱
2024년 10월 28일
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!