필터 지우기
필터 지우기

Merge text and numeric formats to double

조회 수: 2 (최근 30일)
AR
AR 2017년 1월 21일
댓글: Walter Roberson 2017년 1월 22일
Does anyone know how to combine text and numeric formats into double or matrix form?
I have two data sets:
A is a 1x500 table. A 1x5 example of data in the following format:
A={'aapl' 'abbv' 'abc' 'abt' 'ace'};
A=cell2table(A);
B is a 6322 x 500 table. A 3x5 example in following format:
B={12.4949 6.96103 NaN 7.03814 13.0399...
12.5195 7.02181 NaN 7.39418 12.7675...
12.7307 7.21876 NaN 7.75691 12.6901};
B=cell2table(B);
Would like to concatenate A to B, as C, in a double format for analysis and comparisons so C would look like
C=
aapl abbv abc abt ace
12.4949 6.96103 NaN 7.03814 13.0399
12.5195 7.02181 NaN 7.39418 12.7675
12.7307 7.21876 NaN 7.75691 12.6901
I've tried concantenating cells
A=table2cell(A);
B= table2cell(B);
C=[A;B];
and I get following error...
"All contents of the input cell array must be of the same data type."
Tried converting B to double, A to categorical then concantenating
A=categorical(A);
B= table2array(B);
C=[A;B];
and I get following error...
"Can not concatenate a double array and a categorical array."
Tried many other combos but nothing seems to work? Thank you for any suggestions.
AR

채택된 답변

Guillaume
Guillaume 2017년 1월 21일
If B is really declared as
B={12.4949 6.96103 NaN 7.03814 13.0399...
12.5195 7.02181 NaN 7.39418 12.7675...
12.7307 7.21876 NaN 7.75691 12.6901};
with the ... at the end of each line which makes it a row vector cell array, then
C = [A; reshape(B, [], size(A, 2))]
If B were declared
B={12.4949 6.96103 NaN 7.03814 13.0399
12.5195 7.02181 NaN 7.39418 12.7675
12.7307 7.21876 NaN 7.75691 12.6901};
then it would just be:
C = [A;B]
However, I would not recommend a mix of string and numbers in the cell array, it just makes it harder to use. Assuming that these strings are just column header:
C = cell2table(B, 'VariableNames', A)
would be a lot better.
  댓글 수: 2
AR
AR 2017년 1월 21일
편집: Walter Roberson 2017년 1월 22일
Guillaume,
Thank you. I've tested both solutions with my data and they work as you describe.
Unfortunately to do further analysis, I'd need to convert my data into double, then delete some rows with numerical operations not usable in table format yet preserve my column heading identification.
Do you have any suggestions to convert this into double?
Thank you.
--AR
Walter Roberson
Walter Roberson 2017년 1월 22일
You can extract a particular column as double by using dot notation,
MyTable.TheVariable
You can also extract portions of the table as double by using {} notation,
MyTable{3:21, :} %rows 3 to 21, all columns

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by