2

I have a code that i'm trying to run in matlab, it gives an error in textscan function as it can't split a string on a delimiter, although i'm sure the code works on other versions of matlab (on other computer)

>> a='ahmed;mohamed'

a =

ahmed;mohamed

>> b = textscan(a, '%s;%s', 'Delimiter', ';')

b = 

    {1x1 cell}    {0x1 cell}

>> b{1}

ans = 

    'ahmed'

>> b{2}

ans = 

   Empty cell array: 0-by-1

Can some one explain Why this is happening ? is there a recent change in textscan function ? i'm using matlab 2013

Ahmed Kotb
  • 6,269
  • 6
  • 33
  • 52

1 Answers1

4

This works:

str = 'ahmed;mohamed';
C = textscan(str, '%s', 'Delimiter',';', 'CollectOutput',true);
C = C{1};

with:

>> C
C = 
    'ahmed'
    'mohamed'
Amro
  • 123,847
  • 25
  • 243
  • 454
  • 2
    you should not place the delimiter inside the format string, so you could also use: `'%s %s'` (no semicolon). The previous one works because "formatSpec" is applied repeatedly (ie third input argument "N" is `Inf` if not specified) – Amro Jun 17 '13 at 22:40
  • 1
    As an addition to Amro's answer, the reason the original code did not work is that `textscan` does not require you to specify the delimiter in the `FORMAT` field. The function assumes that the delimiter(s) occur between the consecutive pattern matches. – cjh Jun 17 '13 at 22:41
  • Thanks a lot, apparently they have changed the internal implementation between versions and i had a slightly old code – Ahmed Kotb Jun 17 '13 at 22:43
  • the exception is `%c` specifier which reads a single character including delimiters – Amro Jun 17 '13 at 22:45