How to create more cells within a cell in a cell array?

2 views (last 30 days)
I have a cell array which only consists of numeric values. Hence, there are only numeric values in one cell. However, I need to change some of this values to strings. I tried to do so but I ended up with "NaN"s instead of my string value. I'm guessing the only way to approach this is for all values in a cell to be stored in their own 1 x 1 cell array. So a cell array within a cell array? This is the code used to create the cell array.
lat = coordinates(:,2); % x coordinates
lon = coordinates(:,3); % y coordinates
% create nested for loop
% create nested for loop
i_vector=103.6:(1/75):104;
j_vector=1.25:(1/120):1.5;
stns=cell(length(i_vector),length(j_vector)); % 31 x 31 cell array
for i = 1:(length(i_vector)-1)
% you can't access element end+1
for j = 1:(length(j_vector)-1)
% index = logical indexing vector
index = (lat > i_vector(i)) ...
& (lat < i_vector(i+1)) ...
& (lon > j_vector(j)) ...
& (lon < j_vector(j+1)) ;
stns{i,j} = coordinates(index,1); % stn number
end
end
So for example, all the values in stns{21,8} and all other cells should be stored in a 1 x 1 cell array right. How do I achieve this?

Answers (1)

Guillaume
Guillaume on 13 Jun 2017
Edited: Guillaume on 13 Jun 2017
Notwithstanding that I need to change some of these values to strings sounds very iffy (an arbitrary mix of numbers and strings is not a good idea), you can simply convert a matrix into a cell array with num2cell, so change your code to:
stns{i,j} = num2cell(coordinates(index,1)); % stn number
Note that it will not create a 1x1 cell array but an mxn cell array (same size as the matrix) where each cell contains a 1x1 matrix (a scalar). Individual cells can then be changed to strings if desired.
But again, mixing numbers and strings arbitrarily does not sound like a good idea. What exactly are you trying to achieve?

Community Treasure Hunt

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

Start Hunting!