How to write Continue Long Statements on Multiple Lines ?

4 views (last 30 days)
formatSpec = '%f%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%f%f%q%q%q%f%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%f%q%q%f%q%q%f%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%C%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%f%q%q%q%q%q%q%q%q%q%q%f%q%q%q%q%q%q%q%q%q%q%q%q%q%q%[^\n\r]';matlab.png

Accepted Answer

Steven Lord
Steven Lord on 30 Nov 2019
Edited: Steven Lord on 30 Nov 2019
Another way to do this, with a sufficiently recent release of MATLAB, is to join elements of a string array together with the repeated elements of the format being duplicated with repelem. The second input to join says to concatenate the string array elements with nothing between them.
join(["%f", repelem(["%q", "%f", "%q", "%f"], [45 2 3 2]), "%[^\n\r]"], "")

More Answers (1)

dpb
dpb on 30 Nov 2019
The ugliness of the C-style formatting string in all its glory exhibited!!! Why TMW didn't go to the trouble to keep Fortran FORMAT instead. :(
Anyways, don't do this...do something like
fmt=['%f' repmat('%q',1,45) repmat('%f',1,2) repmat('%q',1,3) repmat('%f',1,2) ...
...
'%[^\n\r]'];
where continue to build the counted substrings as needed in place of "..." placeholders above.
The answer to the actual Q? asked is the line continuation syntax in Matlab is "...". For string continuation, must terminate the pieces on each line and let the input parser string them together; the above syntax encloses the actual strings excepting the first and last unique entries as the argument to the repmat function.
PS. Thanks to SL, TMW for illustrating this idiom years ago... :)

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!