MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn moreOpportunities for recent engineering grads.

Apply Today**New to MATLAB?**

Asked by Stephen
on 24 Apr 2012

Is there a way to print multiple lines to the command window (like fprintf does for single lines) without using fprintf at the start of each line?

At the moment my code looks similar (the below was quickly made up as an example) to;

fprintf ('The results of test %d are such that %d ', t1, cats) fprintf ('of the cats are older than %d years old.\n', age) fprintf ('The results of test %d are such that %d ', t2, dogs) fprintf ('of the dogs are older than %d years old.\n', age) fprintf ('The results of test %d are such that %d ', t3, fish) fprintf ('of the fish are older than %d years old.\n', age)

Is there a way to only state fprintf once at the start then have the text followed by the variables at the end? Maybe a bit like;

fprintf ('The results of test %d are such that %d of the ... cats are older than %d years old.\nThe results of test ... %d are such that %d of the dogs are older than %d years ... old.\nThe results of test %d are such that %d of the ... fish are older than %d years old.\n', t1, cats, age, ... t2, dogs, age, t3, fish, age)

I've searched the help files and looked into sprintf but I couldn't see anything that does this so thought I'd see if I'm wasting my time searching!

The help is much appreciated, and apologise if I've missed something staring me in the face in the help files!

*No products are associated with this question.*

Answer by Walter Roberson
on 24 Apr 2012

Accepted answer

Yes, that second syntax for fprintf() itself is fine. You are being caught on the fact that string constants cannot be continued by using ...

fprintf( ['The results of test %d are such that %d of the ', ... 'cats are older than %d years old.\nThe results of test ', ... '%d are such that %d of the dogs are older than %d years ', ... 'old.\nThe results of test %d are such that %d of the ', ... 'fish are older than %d years old.\n'], t1, cats, age, ... t2, dogs, age, t3, fish, age);

Answer by Image Analyst
on 24 Apr 2012

You can do it with one line but you can't split up the fprintf inside the quotes, you have to at least wait until the closing quote

fprintf('blah blah blah %d snafu fubar %s lorem ipsum',... % Format specifier string MUST be on one line. myInteger, myString); % List of variables can be on the next line.

## 0 Comments