0

In Matlab,

1. strsplit('a,b,c,', ',')
2. strsplit('a,b,c,,,', ',')

both results of 1 and 2 are same,

{{'a'}, {'b'}, {'c'}, {0×0 char}}

However I want to take

{{'a'}, {'b'}, {'c'}, {0×0 char}, {0×0 char}, {0×0 char}} 

from a string

'a,b,c,,,'.

I tried 'CollapseDelimiters' option in strsplit function. But It does not work for tails.

J. Kim
  • 95
  • 7
  • 3
    `strsplit('a,b,c,,,', ',','CollapseDelimiters',0)` is giving the correct output on my machine... ? – Paolo Aug 18 '18 at 09:15
  • @UnbearableLightness ah. You're right. The data has wrong format. Appreciated for your kind answer to silly question. – J. Kim Aug 20 '18 at 03:57

1 Answers1

1

As UnbearableLighness suggested, CollapseDelimiters works but you could also use split

>> strsplit('a,b,c,,,', ',','CollapseDelimiters',false)

ans =

  1×6 cell array

    {'a'}    {'b'}    {'c'}    {0×0 char}    {0×0 char}    {0×0 char}


>> split('a,b,c,,,', ',')'

ans =

  1×6 cell array

    {'a'}    {'b'}    {'c'}    {0×0 char}    {0×0 char}    {0×0 char}

I would suggest split because of the performance boost

function profFunc

    n = 1e5;

    tic
    for i = 1:n
        x = strsplit('a,b,c,,,', ',','CollapseDelimiters',false);
    end
    toc

    tic
    for i = 1:n
        x = split('a,b,c,,,', ',');
    end
    toc
end

>> profFunc
Elapsed time is 31.044903 seconds.
Elapsed time is 1.761662 seconds.
matlabbit
  • 696
  • 3
  • 4