필터 지우기
필터 지우기

How to extract desired variables column data from a table?

조회 수: 13 (최근 30일)
Devendra
Devendra 2024년 3월 7일
댓글: Mathieu NOE 2024년 3월 7일
I have the data in excel csv file with variable names as follows
Var1 POSValue MAXSum AVGSum SOS POS EOS Base Duration FirstHalf SecondHalf GrowthRate SenescenceRate Peaks total_cane act_y_ha act_per_ha field_yield total_FS variety
However, I want to get only required variables by using following matlab code
features = df.Properties.VariableNames(~ismember(df.Properties.VariableNames, ...
{'POS value', 'MAX sum', 'AVG sum', 'SOS', 'POS', 'EOS', ...
'base', 'duration', 'first half', 'second half', 'growth rate', ...
'senescence rate', 'peaks'}));
Instead it is extracting all the variables.
Please suggest me how to get the required variables.
Dev

답변 (1개)

Mathieu NOE
Mathieu NOE 2024년 3월 7일
hello
this is one solution
NB that the variables names and the names your are using in the selection are not exactly the same,
like POSValue MAXSum vs POS value , MAX sum and alike....
I corrected this minor bug
T =readtable("data.xlsx");
v = {'POSValue', 'MAXSum', 'AVGSum', 'SOS', 'POS', 'EOS', ...
'Base', 'Duration', 'FirstHalf', 'SecondHalf', 'GrowthRate', ...
'SenescenceRate', 'Peaks'};
v_to_use = v(ismember(v,T.Properties.VariableNames));
new_T = T(:,v_to_use)
  댓글 수: 4
Devendra
Devendra 2024년 3월 7일
Thank you very much for helping me. One more little thing is how to resolve following error
X = T(:, new_T);
A table variable subscript must be a numeric array containing real positive integers, a logical array, a character vector, a string array, a cell array of character vectors, or a pattern scalar used to match variable names.
Dev
Mathieu NOE
Mathieu NOE 2024년 3월 7일
this is not my code : X = T(:, new_T);
new_T is the table extracted from T , so T(:, new_T) is non sense (you cannot use a table as a table subscript
use the code I provided above (it works !!)

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

카테고리

Help CenterFile Exchange에서 Tables에 대해 자세히 알아보기

제품


릴리스

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by