Search code examples
stringmatlabsystemprintf

Find line breaks from MATLAB system command


I'm trying to parse the result string from a system command to an external program.

[status,result] = system(cmd);

result prints out on my console with its lines correctly broken out, i.e.

line1

line2

...

But it's actually just a single long character array, and there are no newline characters anywhere. How does matlab know when to print out a new line? And how can I separate the char array into its separate lines for further parsing. Thanks!


Solution

  • Depending on operation system different characters represent end of line. It can be \n, \r, \f or their combination. Those characters have ASCII code less then 30. So you can look for them with, for example, find(results < 30) to display their position in the string , and int32(results(results < 30)) to see their code.

    int32(sprintf('\n\r\f'))
    
    ans =
    
          10          13          12
    

    Then you can use the code you get to split the string:

    regexp(results, char(13), 'split')
    

    If you are not interested on which characters are used as end-of-line, you can just try:

    regexp(results, '[\f\n\r]', 'split')