How to write Continue Long Statements on Multiple Lines ?

6 visualizzazioni (ultimi 30 giorni)
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

Risposta accettata

Steven Lord
Steven Lord il 30 Nov 2019
Modificato: Steven Lord il 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]"], "")

Più risposte (1)

dpb
dpb il 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... :)

Categorie

Scopri di più su Creating and Concatenating Matrices in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by