How to write Continue Long Statements on Multiple Lines ?

6 次查看(过去 30 天)
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

采纳的回答

Steven Lord
Steven Lord 2019-11-30
编辑:Steven Lord 2019-11-30
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]"], "")

更多回答(1 个)

dpb
dpb 2019-11-30
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... :)

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by