필터 지우기
필터 지우기

How to write Continue Long Statements on Multiple Lines ?

조회 수: 6 (최근 30일)
Vuqar Samedov
Vuqar Samedov 2019년 11월 30일
댓글: 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

채택된 답변

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일
Ah! Nice refinement, Steven...hadn't thought of it.

댓글을 달려면 로그인하십시오.

추가 답변 (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