필터 지우기
필터 지우기

How to concatenate a row of variable names and a double?

조회 수: 8 (최근 30일)
new2matlab
new2matlab 2020년 8월 3일
편집: Adam Danz 2020년 8월 3일
I want to add my cell array of variable names to a double array of numbers. How would one do this properly?
  댓글 수: 1
James Tursa
James Tursa 2020년 8월 3일
Please provide a small example of inputs and desired output.

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

답변 (2개)

Adam Danz
Adam Danz 2020년 8월 3일
편집: Adam Danz 2020년 8월 3일
You can concatenate them within a cell array. Based on your question and title, I'm guessing you've got a row of variable names and a row of numbers of equal length to the variable name array.
VarNames = {'Sofia', 'Plovdiv', 'Burgas', 'Varna', 'Veliko Turnovo', 'Haskovo', 'Ruse'};
values = randi(100,size(VarNames));
y = [VarNames; num2cell(values)];
Result:
2×7 cell array
{'Sofia'} {'Plovdiv'} {'Burgas'} {'Varna'} {'Veliko Turnovo'} {'Haskovo'} {'Ruse'}
{[ 82]} {[ 91]} {[ 13]} {[ 92]} {[ 64]} {[ 10]} {[ 28]}
Or perhaps you want a table
T = array2table(values, 'VariableNames', VarNames)
Result: (different random values)
1×7 table
Sofia Plovdiv Burgas Varna Veliko Turnovo Haskovo Ruse
_____ _______ ______ _____ ______________ _______ ____
4 85 94 68 76 75 40

Rik
Rik 2020년 8월 3일
If you want to mix data types: that's not going to be possible. Each variable in Matlab can only be single type, so something like the code below will not work.
['SomeRandomString1' 5;...
'SomeOtherString' pi]
If you want to store an array like that you will need a cell array:
{'SomeRandomString1' 5;...
'SomeOtherString' pi}
Now each cell acts as a container for a variable. In some cells you have now stored char arrays, and in others scalar doubles.

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by