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!