creating cell array

1 view (last 30 days)
Tor Fredrik Hove
Tor Fredrik Hove on 23 Oct 2011
I would like to create a cell array with 1 row and two columns and in each column it is a columnvector of 5 elements in the first 5 numbers in the second 5 characters. This was my attempt:
>> cellarray2too1={5.3; 2.2; 3.3; 4.4; 1.1, 'a'; 'b'; 'a'; 'a'; 'b'} ??? Error using ==> vertcat CAT arguments dimensions are not consistent.

Accepted Answer

Image Analyst
Image Analyst on 23 Oct 2011
Look at your statement. After each semicolon it tries to make a new row in your cell array. So the first row has 5.3. The next row has 2.2. The next row has 3.3. The next row has 4.4. Each of those rows has one thing that goes into one cell. Now look at the next row. You have 1.1, 'a' and this is two things. It's trying to make that row be two cells when each of your prior rows was only one cell. It can't concatenate a two cell row to a columnar array of one-cell rows. That's why you got the error. You could either fix it like andrei suggested, or use this alternative:
cellarray2too1={5.3; 2.2; 3.3; 4.4; 1.1; 'a'; 'b'; 'a'; 'a'; 'b'}
depending on exactly what you want to have. Note I put "1.1; 'a'" so that it is now two rows of one cell each instead of one row of two cells. Think of a cell array as a grid of buckets on the floor. You can arrange the buckets into any rectangular array shape you want. And you can toss almost anything you want into each bucket. But your syntax tried to create a row of two buckets when all the other buckets were in a single line. You were trying to do this:
5.3
2.2
3.3
4.4
1.1, 'a'
'b'
'a'
'a'
'b'
and that is not allowed.

More Answers (1)

Andrei Bobrov
Andrei Bobrov on 23 Oct 2011
cellarray2too1={[5.3; 2.2; 3.3; 4.4; 1.1],[ 'a'; 'b'; 'a'; 'a'; 'b']}

Categories

Find more on Mathematics in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!