필터 지우기
필터 지우기

Name table columns with variable index

조회 수: 3 (최근 30일)
Pablo SERIS
Pablo SERIS 2022년 4월 27일
댓글: Pablo SERIS 2022년 4월 28일
I'll take to explain a 3 columns table but I actually have around 100 columns.
I have a 3 columns table, with each column named with a label Ux,Uy,Uz and filled with 18000 values
I want to calculate mean value and standard deviation for each column and put it in a 6 columns table with columns named like [Uxmean,Uxstd,Uymean,Uystd,...]
Here is my program.
file = uigetfile();
tab = readtable(file);
varnames = tab.Properties.VariableNames(1:end);
doub = table2array(tab(:,[1:end]));
for i = [1:width(doub)]
cell(2*i-1) = (mean(doub(:,i)));
cell(2*i) = (std(doub(:,i)));
end
tab =array2table(cell);
% for i = [1:width(doub)]
% tab.Properties.VariableNames(2*i-1)=varnames(i) "mean"
% tab.Properties.VariableNames(2*i)=varnames(i) "std"
% end
It works until the commented lines, when i try to add the index "mean" to odd columns and "std" to even columns
I'm very new on Matlab so i probably forgot some details and there is for sure a easier method so tell me.
Thank you!

채택된 답변

Chunru
Chunru 2022년 4월 27일
편집: Chunru 2022년 4월 27일
tab = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/980210/MATWORKS.csv');
head(tab)
ans = 8×3 table
Ux Uy Uz ________ ________ ________ -0.46271 0.18824 0.38583 -0.41488 0.55131 0.37074 -0.49457 0.18599 0.2409 -0.30067 0.3442 -0.20573 -0.54151 0.29827 -0.15212 -0.85707 -0.12284 -0.15933 -1.0658 -0.42837 -0.53856 -0.36889 -0.58331 -0.87129
varnames = tab.Properties.VariableNames(1:end)
varnames = 1×3 cell array
{'Ux'} {'Uy'} {'Uz'}
m_tab = varfun(@mean, tab)
m_tab = 1×3 table
mean_Ux mean_Uy mean_Uz _______ _______ ________ -1.0295 -2.4166 0.033029
s_tab =varfun(@std, tab)
s_tab = 1×3 table
std_Ux std_Uy std_Uz _______ ______ _______ 0.94946 2.1712 0.63944
output = [m_tab s_tab]
output = 1×6 table
mean_Ux mean_Uy mean_Uz std_Ux std_Uy std_Uz _______ _______ ________ _______ ______ _______ -1.0295 -2.4166 0.033029 0.94946 2.1712 0.63944
idx = [0; 3]+(1:3); idx=idx(:);
output=output(:, idx)
output = 1×6 table
mean_Ux std_Ux mean_Uy std_Uy mean_Uz std_Uz _______ _______ _______ ______ ________ _______ -1.0295 0.94946 -2.4166 2.1712 0.033029 0.63944
  댓글 수: 3
Chunru
Chunru 2022년 4월 27일
See the update above.
Pablo SERIS
Pablo SERIS 2022년 4월 28일
Thank you very much!!!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Low-Level File I/O에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by