Error using cell2mat (line 52) CELL2MAT does not support cell arrays containing cell arrays or objects.

HI I have a cell array {4000x1 cell, 5000x1 cell} containing strings 'nameA', 'nameB' - I want to concatenate them into 1 long character array - cell2mat does the trick if the cells are numbers - but what do you use if you have strings as in my case? thanks

2 Commenti

strings or char array?
Your notation would imply that you have 1x2 cell array, where each cell is itself a cell array (of size 4000x1 and 5000x1 respectively). Presumably each cell of these subcells are strings or char arrays?
Should each of the main two cell result in one char array (i.e one char array is the concatenation of the 4000 char array, and the other is the concatenation of 5000 char array), or should everything be concatenated together?
please see my comment below to bird man
everything should be concatenated together
thanks

Accedi per commentare.

 Risposta accettata

vertcat(A{:})

8 Commenti

OK this works thanks
before I accept the answer . . .
could you tell me how I would concatenate n character arrays in a for loop?
at the moment I am storing each one in a structure array then turning this into a cell array and then turning it onto 1 long character array (with the method you suggested above) - but this is insane there must ba an easier way
thanks
Why would you want to use a for loop for this case, where you can do what you want with only one line of code?
because I have 24 character arrays and I want to concatenate them (I am hacking it at the moment in the way I explained above - but it's very inelegant and there must be a better way)
vertcat() on character arrays doesn't work
Oh, you have directly character array, not cell array that contain characters? Am I right? Can you give an example?
"vertcat() on character arrays doesn't work"
Actually it does:
>> vertcat('ab','XY','-3')
ans =
ab
XY
-3
Maybe it horzcat is what you want? However this is just a guess, because none of us actually know what your data really is. Rather than wasting time describing what you have you should simply upload your variable in a .mat file so that we have something to work with.
yes i directly have character arrays
a good example is 'load cities ' and the 'categories' character array
don't worry if matlab is not set up to do this -i've hacked it to work now - maybe i'll look into creating a proper 'dataset' as in the example 'load spectra' (this is sort of what i am doing)
thanks
@cgenes: In your question you stated that you have "I have a cell array {4000x1 cell, 5000x1 cell} containing strings 'nameA', 'nameB'", later you wrote "I have 24 character arrays", and now you write that you "have character arrays. a good example is 'load cities ' and the 'categories' character array". We still have no idea what you actually have, because your description changes every five minutes.
Note that the categories variable is actually a character array (not a cell of strings as you wrote in you question) and that this can easily be converted into one long character row vector, quite efficiently without any loop. Is that what you are trying to do?
Hi all thanks for all your help -
in fact I had character arrays and I was using vertcat() - but it was somehow scrambling the arrays - and turning them all into numbers
anyway i've got a good hack now so works fine
thanks again

Accedi per commentare.

Più risposte (0)

Categorie

Richiesto:

il 21 Mar 2018

Commentato:

il 21 Mar 2018

Community Treasure Hunt

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

Start Hunting!

Translated by