how to convert arrays inside my cell array into cells?

1 Ansicht (letzte 30 Tage)
ilona
ilona am 29 Dez. 2013
Bearbeitet: Azzi Abdelmalek am 29 Dez. 2013
if I have a cell array:
% code
cities =
'city1'
'city2'
'city3'
'city4'
'city5'
I want my cell to include cells that include the strings inside like that
% code
cities =
{1x1 cell}
{1x1 cell}
{1x1 cell}
{1x1 cell}
{1x1 cell}
for example in the 3rd cell inside my cell array there is
% code
{1x1 cell}% inside of it --> 'city3'
without using any kind of loop! (cause I can do it without any real problam with a loop)
  2 Kommentare
Image Analyst
Image Analyst am 29 Dez. 2013
Cells are complicated - why make it even more complicated than it needs to be? If you want you could have cells inside of cells inside of cells inside of cells. But why?
ilona
ilona am 29 Dez. 2013
this is because I study Matlab in a University course and they want me to submit a structure that include cell arrays that include cell arrays... for no good reason other then showing us it is possible...

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Andrei Bobrov
Andrei Bobrov am 29 Dez. 2013
out = num2cell(cities)
  3 Kommentare
Image Analyst
Image Analyst am 29 Dez. 2013
I never would have thought something called num2cell would work on something that is not numbers, but surprisingly it does.
ilona
ilona am 29 Dez. 2013
yes! it is very surprising he is a genius!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

Azzi Abdelmalek
Azzi Abdelmalek am 29 Dez. 2013
Bearbeitet: Azzi Abdelmalek am 29 Dez. 2013
cities ={'city1'
'city2'
'city3'
'city4'
'city5'}
out=cellfun(@(x) {x},cities,'un',0)
out{3}
%or
out=cellfun(@cellstr,cities,'un',0)
  3 Kommentare
Azzi Abdelmalek
Azzi Abdelmalek am 29 Dez. 2013
I don't know how to explain, maybe you should look at
doc cellfun

Melden Sie sich an, um zu kommentieren.


Azzi Abdelmalek
Azzi Abdelmalek am 29 Dez. 2013
out=regexp(cities,'.+','match')

Kategorien

Mehr zu Cell Arrays finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by