Add '_max' to odd variable names and '_min' to even ones.

조회 수: 2 (최근 30일)
Usune Elizondo
Usune Elizondo 2021년 9월 22일
댓글: Usune Elizondo 2021년 9월 22일
I have a table where just even variables have a name, the rest (odd) have automatic name set by Matlab.
Something like this:
I want to have Var1 = t_max and t = t_min, then Var3 = ang_azi_max and ang_azi = ang_azi_min.
Hope someone can help ;)

채택된 답변

Stephen23
Stephen23 2021년 9월 22일
T = array2table(rand(5,8),'VariableNames',{'Var1','t','Var3','ang_azi','Var5','vel_azi','Var7','acc_azi'})
T = 5×8 table
Var1 t Var3 ang_azi Var5 vel_azi Var7 acc_azi _______ _______ _________ _______ _______ _______ _______ _______ 0.64569 0.92867 0.0076932 0.65038 0.92731 0.53037 0.45407 0.55135 0.94421 0.22754 0.50774 0.82564 0.97646 0.69373 0.55652 0.26508 0.83117 0.69494 0.93597 0.35117 0.89839 0.7037 0.9437 0.52426 0.8657 0.26734 0.017575 0.80891 0.79093 0.22237 0.93816 0.57082 0.79332 0.75431 0.40472 0.97708 0.26331 0.52194 0.94611 0.63532
T.Properties.VariableNames(1:2:end) = strcat(T.Properties.VariableNames(2:2:end),'_max');
T.Properties.VariableNames(2:2:end) = strcat(T.Properties.VariableNames(2:2:end),'_min')
T = 5×8 table
t_max t_min ang_azi_max ang_azi_min vel_azi_max vel_azi_min acc_azi_max acc_azi_min _______ _______ ___________ ___________ ___________ ___________ ___________ ___________ 0.64569 0.92867 0.0076932 0.65038 0.92731 0.53037 0.45407 0.55135 0.94421 0.22754 0.50774 0.82564 0.97646 0.69373 0.55652 0.26508 0.83117 0.69494 0.93597 0.35117 0.89839 0.7037 0.9437 0.52426 0.8657 0.26734 0.017575 0.80891 0.79093 0.22237 0.93816 0.57082 0.79332 0.75431 0.40472 0.97708 0.26331 0.52194 0.94611 0.63532

추가 답변 (1개)

Simon Chan
Simon Chan 2021년 9월 22일
Suppose the table is called T, then try the following:
T.Properties.VariableNames(1:4) = {'t_max','t_min','ang_azi_max','ang_azi_min'}
  댓글 수: 3
Simon Chan
Simon Chan 2021년 9월 22일
Try this:
temp = T.Properties.VariableNames(2:2:end);
temp_min = cellfun(@(x) strcat(x,'_min'),temp,'UniformOutput',false);
temp_max = cellfun(@(x) strcat(x,'_max'),temp,'UniformOutput',false);
temp_combine = vertcat(temp_max, temp_min);
T.Properties.VariableNames = temp_combine(:);
Stephen23
Stephen23 2021년 9월 22일
@Simon Chan: STRCAT also operates on cell arrays of char vectors, so CELLFUN is entirely superfluous.

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

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by