lola
lola

Reputation: 5789

Matlab formatting : wrap line in sprintf

I get the value of output, which corresponds to the diff between 2 files :

output = 'a.x: low 0 -> low 1a.b : 3 -> Medium 6'

then I printf the content

sprintf('files are different \n%s', output);

but I would like to show it as follows (word wrap):

output :

a.x: low 0 -> low 1 
a.b : 3 -> Medium 6 

Upvotes: 2

Views: 1052

Answers (1)

zeFrenchy
zeFrenchy

Reputation: 6591

You could use a regular expression to chop your string

chopped = regexpi(output, '^(?<line1>[^>]*>[^\d]*[\d]*)(?<line2>.*)$', 'names')

does the trick here.

>> chopped = 

    line1: 'a.x: low 0 -> low 1'
    line2: 'a.b : 3 -> Medium 6'

The assumptions are:

  1. you always have one single -> in the first line
  2. the first line always ends with the first number after ->
  3. the numerical value ending first line in an integer

If you're not familiar with regex

  • (?<line1> ) and (?<line2> ) are here to capture the matches in the chopped variable.
  • [^>]* consumes until first >
  • [^\d]* consumes all non numerical characters (so until the following number starts)
  • [\d]* consumes all numerical characters
  • .* consumes rest of the string

Upvotes: 1

Related Questions