names = {'A' 'B' 'C' 'D' 'E'};
val = [0;1.5;0.5;0.5;1.5]
I want my output as folows
A 0
B 1.5
C 0.5
D 0.5
E 1.5

 채택된 답변

Star Strider
Star Strider 2021년 4월 7일

1 개 추천

Try this:
names = {'A' 'B' 'C' 'D' 'E'};
val = [0;1.5;0.5;0.5;1.5];
C = {[names{:}].' val}
C1 = C{:,1}
C2 = C{:,2}
TC = table(C1,C2)
.

댓글 수: 6

Souarv De
Souarv De 2021년 4월 7일
Thanks @Star Strider. Its solved my purpose.
As always, my pleasure!
@Star Strider I found an a auxiliary problem with your code is that if names is replaced by as follows
names = {'A1' 'B1' 'C1' 'D1' 'E1'}
Then, the code encounters some errors. Please help.
names = {'A1';'B1';'C1';'D1';'E1'}; % note the orientation!
val = [0;1.5;0.5;0.5;1.5];
T = table(names,val)
T = 5×2 table
names val ______ ___ {'A1'} 0 {'B1'} 1.5 {'C1'} 0.5 {'D1'} 0.5 {'E1'} 1.5
Stephen — Thank you!
Another option is to replace them with string variables:
names = {"A1" "B1" "C1" "D1" "E1"};
.
Souarv De
Souarv De 2021년 4월 8일
편집: Souarv De 2021년 4월 8일

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

추가 답변 (1개)

David Hill
David Hill 2021년 4월 7일

0 개 추천

for k=1:length(names)
names{k}=[names{k},' ',num2str(val(k))];
end

댓글 수: 1

Souarv De
Souarv De 2021년 4월 7일
@David Hill It is ok but in your code the output becomes again a cell type and both values are merged together as individual enitity of cell.

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

카테고리

도움말 센터File Exchange에서 Data Types에 대해 자세히 알아보기

제품

릴리스

R2018a

태그

질문:

2021년 4월 7일

편집:

2021년 4월 8일

Community Treasure Hunt

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

Start Hunting!

Translated by