Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to convert a cell to string in matlab

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

like image 373
user1804229 Avatar asked Nov 06 '12 20:11

user1804229


People also ask

How do you convert a cell to an array in Matlab?

A = cell2mat( C ) converts a cell array into an ordinary array. The elements of the cell array must all contain the same data type, and the resulting array is of that data type. The contents of C must support concatenation into an N-dimensional rectangle. Otherwise, the results are undefined.

How do you make a cell array of strings?

Convert Cell Arrays to String Arrays You can convert cell arrays of character vectors to string arrays. To convert a cell array of character vectors, use the string function. In fact, the string function converts any cell array, so long as all of the contents can be converted to strings.

How do I convert an array to a string in Matlab?

str = string( A ) converts the input array to a string array. For instance, if A is numeric vector [1 20 300] , str is a string array of the same size, ["1" "20" "300"] . str = string( A , dateFmt ) , where A is a datetime or duration array, applies the specified format, such as "HH:mm:ss" .


1 Answers

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

fruits = {...
    'banana',...
    'apple',...
    'orange'...
}

First of all integer indexing is not needed for small arrays. It is much better to use foreach-like constructions. Indeed,

for index = 1:numel(fruits)
    fruits{index}
end

is equivalent to

for fruit = fruits
    fruit
end

right?

Well, not quite. First loop produces strings, while the second one gives cells. You can check it with

for index = 1:numel(fruits)
    [isstr(fruits{index}) iscell(fruits{index})]
end

for fruit = fruits
    [isstr(fruit) iscell(fruit)]
end

, 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:

for fruit = fruits
    [fruit 'is a fruit']
end

You will get

ans = 

    'banana'    'is a fruit'

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 {:}

for fruit = fruits
    [fruit{:} 'is a fruit']
end

Magically this already produces the expected 'banana is a fruit', 'apple is a fruit', etc.

Hints

A few hints:

  • Index-free looping works nicely with structs as in for fruit = [fieldnames][1](fruits)'
  • The above is true for open source octave
  • banana is not just fruit, taxonomically it is also a herb ;-) just like 'banana' in MATLAB is both a string and a matrix, i.e. assert(isstr('banana') && ismat('banana')) passes, but assert(iscell('banana')) fails.
  • {:} is equivalent to cell2mat

PS

a solution to your question may look like this:

Given

vcell = {...
    'v'    576.5818    3.0286  576.9270;
    'v'    576.5818    3.0286  576.9270    
}

covert index-wise only numeric types to strings

 vcell(cellfun(@isnumeric, vcell)) =  cellfun(@(x) sprintf('%.5f', x), vcell(cellfun(@isnumeric, vcell)), 'UniformOutput', false)

Above code outputs

vcell =

'v'    '576.58180'    '3.02860'    '576.92700'
'v'    '576.58180'    '3.02860'    '576.92700'

which can be concatenated.

like image 109
Yauhen Yakimovich Avatar answered Sep 20 '22 10:09

Yauhen Yakimovich