MATLAB Answers


To create cells with different names in a loop

Asked by Priya
on 25 May 2013

I have defined a cell array as follows:

names = {'joe', 'enna', 'fin'};

now in a loop I want to create empty cell arrays whose names should be 'joe' , 'enna', 'fin' respectively, instead of manually creating like follows:

joe = cell(zeros); enna = cell(zeros);fin = cell(zeros);

I have to make 50 such cell arrays in loop.

Please help how to do ?



No products are associated with this question.

2 Answers

Answer by Azzi Abdelmalek
on 25 May 2013

names = {'joe', 'enna', 'fin'};
for k=1:numel(names)
eval([names{k} '=cell(5)'])

Creating different names is not recommended. You can create one cell array containing other cell array



A={{1 2 3;4 5 6} {10 20 30} {5}}
A{1}  % your first cell array
A{2}  % your second cell array
% and so on
on 25 May 2013

@Image Analyst - Hi thanks. Actually I have around 790 datasets containing data as well as characters. Instead of working on 790 cells, would it not be better to make a single cells and sub cells in it

Priya, the best way would be to have a cell array, not a single cell, but a whole array of cells. The row in the array would represent the dataset (1 through 790) and the column would be the data for that data set (the various numbers and strings associated with that dataset). See the FAQ for a good description of cells:

Join the 15-year community celebration.

Play games and win prizes!

Learn more
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!