How to add a matrix data to cell array?

I have var1 in cell array and var2 in matrix.
var1 = {'a' 'b' 'c'}
var2 = ([1 2 3])'.
I like add var2(1) to var1{2,2}, var2(2) to var1{3,2} and var2(3) to var1{4,2}. How can I do that smartly, not one by one? Thank you!

댓글 수: 4

Guillaume
Guillaume 2019년 11월 1일
What does it mean to you to add the character 'a' to the number 1?
Wes
Wes 2019년 11월 1일
Sorry for my poorly simplified description. var1{1,1:3} will have a header having 'a', 'b' and 'c' in first row. Under its 'b' column cell, I like to add var2 numerical data in next rows, ie, to var1{2:4,2}.
var1 = {'a' 'b' 'c'}
var2 = [1 2 3]';
What result you are expecting? Result=???
Wes
Wes 2019년 11월 1일
My expected result is to have var1 as below.
Capture.PNG

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

답변 (2개)

Guillaume
Guillaume 2019년 11월 1일

0 개 추천

Probably the best answer is don't use a cell array, use a table which is designed exactly for the purpose of having named columns:
>> var1 = {'a', 'b', 'c'}
>> var2 = magic(3);
>> t = array2table(var2, 'VariableNames', var1)
t =
3×3 table
a b c
_ _ _
8 1 6
3 5 7
4 9 2
>> t.a %access data in column 'a'
ans =
8
3
4

댓글 수: 1

Wes
Wes 2019년 11월 1일
Unfortunately I have an old version that doesn't support array2table, so couldn't test your proposal. Anyway thank you!

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

the cyclist
the cyclist 2019년 11월 1일
편집: the cyclist 2019년 11월 1일

0 개 추천

var1 = {'a' 'b' 'c'};
var2 = [1 2 3]';
var1(2:4,2) = num2cell(var2)

카테고리

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

태그

질문:

Wes
2019년 11월 1일

댓글:

Wes
2019년 11월 1일

Community Treasure Hunt

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

Start Hunting!

Translated by