From: Pr B on
i'm trying to concatenate strings together in a cell array with the following code:

r = [];

for i = 1:length(unique_m)

for j = 1:length(m{i})

r{i} = [r ; table(strmatch(m{i}(j),table(:,1)),3)];

end

end

basically, for each element in cell 'm', i am obtaining corresponding values from a 'table' and storing them in a cell 'r'. however, the concatenation only works for the first iteration. the strings are stored in 'r' for the first iteration but for the following iterations, 'r' contains cells within cells in the ith position. how do i fix this so that 'r' contains the individual strings concatenated in one cell instead of cells within cells?
From: Walter Roberson on
Pr B wrote:
> i'm trying to concatenate strings together in a cell array with the
> following code:
>
> r = [];
>
> for i = 1:length(unique_m)
> for j = 1:length(m{i})
> r{i} = [r ; table(strmatch(m{i}(j),table(:,1)),3)];
> end
> end
>
> basically, for each element in cell 'm', i am obtaining corresponding
> values from a 'table' and storing them in a cell 'r'. however, the
> concatenation only works for the first iteration. the strings are
> stored in 'r' for the first iteration but for the following iterations,
> 'r' contains cells within cells in the ith position. how do i fix this
> so that 'r' contains the individual strings concatenated in one cell
> instead of cells within cells?

Are you sure about your code?

m{i}(j) would have to result in a character string in order to be usable
in that argument position in strmatch, but the only way for that to be
possible with that sequence of operations is if m{i}(j) is an individual
character (a string of length 1). As strmatch() looks for rows starting
with the given string, if the string is only 1 character long, you might
as well use "==" if "table" is a character array (in which case
table(:,1) would also be a single character).

Using strmatch() makes more sense if "table" is a cell array, but in
that case, you would be better off initializing

firstchar = cellfun(@(c) c(1), table(:,1));

and then in the loop using "==" against firstchar .


Anyhow, your most immediate problem is that in the loop you are setting
the content of r{i} to include _all_ of r so far, not just building up
r{i} or r .

I cannot advise you as to the correct code as I am unclear as to what
the data types of m and m{i} and table are.
 | 
Pages: 1
Prev: face masking of an image
Next: point cloud to STL