MATLAB Answers

How to write Continue Long Statements on Multiple Lines ?

27 ビュー (過去 30 日間)
Vuqar Samedov
Vuqar Samedov 2019 年 11 月 30 日
Commented: dpb 2019 年 11 月 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

  0 件のコメント

サインイン to comment.


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 2019 年 11 月 30 日
Ah! Nice refinement, Steven...hadn't thought of it.

サインイン to comment.

More Answers (1)

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 something like
fmt=['%f' repmat('%q',1,45) repmat('%f',1,2) repmat('%q',1,3) repmat('%f',1,2) ...
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... :)

  0 件のコメント

サインイン to comment.

サインイン してこの質問に回答します。


Translated by