Suppose I have a cell
v = 'v' [576.5818] [3.0286] [576.9270]
'v' [576.5953] [3.1180] [576.8716]
'f' [ 56] [ 58] [ 52]
'f' [ 56] [ 58] [ 52]
and I want to convert this to a cell array using a format string for each element:' %.5f'
How can I do this? I tried the following approach, but I get an error:
f1 = @(x) sprintf(' %.5f',x);
cellfun(f1, num2cell(v),'UniformOutput', false)
I am getting an error as ???
Error using ==> sprintf
Function is not defined for 'cell' inputs.
Error in ==> @(x)sprintf(' %.5f',x)
Can any one help me thanks in advance
String is a cell array
Well, not really.. It is a matrix, but continue reading.
I guess cell array is the most mystic data type in MATLAB. So let's demystify it a bit ;-)
Assume
First of all integer indexing is not needed for small arrays. It is much better to use foreach-like constructions. Indeed,
is equivalent to
right?
Well, not quite. First loop produces strings, while the second one gives cells. You can check it with
, i.e. [1 0] and [0 1].
If you have spot the difference, then you must know what to do with the next example (in this one is really relate to your question (!) I promise!). Say you try to do horizontal concatenation in a loop:
You will get
and so on. Why? Apparently this code tries to concatenate a nested cell array to a string (a cell array containing a matrix of chars which constitute the string like 'banana'). So, correct answer is
Use {:}
Magically this already produces the expected 'banana is a fruit', 'apple is a fruit', etc.
Hints
A few hints:
for fruit = [fieldnames][1](fruits)'
{:}
is equivalent tocell2mat
PS
a solution to your question may look like this:
Given
covert index-wise only numeric types to strings
Above code outputs
vcell =
which can be concatenated.
Suppose we have a cell as follows:
We can get the string out of it simply by using the
{:}
operator on it directly.Note that we can use the expression
mycell{:}
anywhere we would use a normal string.By looking in the strjoin.m file i found this:
Try this:
(Works according to some Google results.)