Search code examples
matlabstring-concatenationcell-array

Matlab: convert cell to readable format


Thanks in advance for the help.

I have an array of values that looks like this

[[1x5 double]; [1x2 double]; ....]

I would like to convert this to this

['12345'; '12'; ....]

cell2mat sort of does what I want, but I end up getting this

[['1' '2' '3' '4' '5']; ['1' '2'];...]

I have been all over the matlab documentation and haven't found a way to do this. Really all I want is to convert [1x5 double] to a string (I can't convert to a num because I don't want to drop insignificant zeros). Is there an simple easy way to do this besides doing this by hand with for loops?


Solution

  • If input_array is your input array, try this cellfun + num2str based approach -

    cellfun(@(x) num2str(x,'%1d'), input_array,'uni',0)
    

    Example -

    %// Input array
    input_array = {randi(9,1,5)-1;randi(9,1,2)-1}
    
    %// Display the cell array values for verification of results later on
    celldisp(input_array)
    
    %// Output
    out = cellfun(@(x) num2str(x,'%1d'), input_array,'uni',0)
    

    Output (on run) -

    input_array = 
        [1x5 double]
        [1x2 double]
    
    input_array{1} =
         3     6     0     5     3
    input_array{2} =
         6     2
    
    out = 
        '36053'
        '62'