How to write repeating string with variables in for loop

6 ビュー (過去 30 日間)
Piet Jonker
Piet Jonker 2019 年 4 月 5 日
コメント済み: Piet Jonker 2019 年 4 月 9 日
I'm trying to write a script for another program using MATLAB. I want to repeat these 7 lines 75 times, but after VideoA in line2 and after VideoA in line 4 I want number 1 to 75. I've tried the sprintf function, but can't get it to work. I hope this is clear, if not please ask! Can anybody help me?
line1 = ' new ScreenItem(ScreenVideoComponent,';
line2 = ' ''VideoA'',';
line3 = ' {';
line4 = ' video: ''assets/VideoA.mp4'',';
line5 = ' mime: ''video/mp4'',';
line6 = ' }';
line7 = ' ),';
  1 件のコメント
Stephen23
Stephen23 2019 年 4 月 5 日
"I've tried the sprintf function, but can't get it to work."
Please show us what you tried.

サインインしてコメントする。

採用された回答

Dennis
Dennis 2019 年 4 月 5 日
編集済み: Dennis 2019 年 4 月 8 日
Check if this works for you.
for i=1:75
line1 = ' new ScreenItem(ScreenVideoComponent,';
line2 = sprintf(' VideoA%02d,',i);
line3 = ' {';
line4 = sprintf(' video: assets/VideoA%02d.mp4,',i);
line5 = ' mime: ','video/mp4'','; %i think this should be line5 = [' mime: ','video/mp4'','];
line6 = ' }';
line7 = ' ),';
end
Please notice that the above code overwrites all variables in each loop iteration. I also don't think that you need 7 different variables for your strings, you might want to have a look at Stephen's tutorial about dynamically named variables. Here is the slightly changed version of your code:
line=cell(7,75);
for i=1:75
line{1,i} = ' new ScreenItem(ScreenVideoComponent,';
line{2,i} = sprintf(' VideoA%02d,',i);
line{3,i} = ' {';
line{4,i} = sprintf(' video: assets/VideoA%02d.mp4,',i);
line{5,i} = [' mime: ','video/mp4'','] ;
line{6,i} = ' }';
line{7,i} = ' ),';
end
  3 件のコメント
Guillaume
Guillaume 2019 年 4 月 5 日
@Dennis, I assume you meant to use %02d as a format (to generate 01, 02, ... 10, ... 75). %01d is the same as %d and generates 1,2, ...10, ... 75.
Dennis
Dennis 2019 年 4 月 8 日
You are right, i corrected it.

サインインしてコメントする。

その他の回答 (1 件)

Guillaume
Guillaume 2019 年 4 月 5 日
編集済み: Guillaume 2019 年 4 月 5 日
Well, if you really want uncluttered:
line = cell(7, 75);
line([1 3 5 6 7], :) = repmat({' new ScreenItem(ScreenVideoComponent,';
' {';
' mime: ''video/mp4'',';
' }';
' ),'}, 1, 75);
line(2, :) = compose(' VideoA%02d,',1:75);
line(4, :) = compose(' video: assets/VideoA%02d.mp4,', 1:75);
No need for a loop.
If all you want to do is just create a long string where the above is repeated 75 times, then:
lines = compose(strjoin({' new ScreenItem(ScreenVideoComponent,';
' ''VideoA%02d'',';
' {';
' video: ''assets/VideoA%02d.mp4'',';
' mime: ''video/mp4'',';
' }';
' ),'}, '\n'), ...
repelem(1:75, 2));
lines = strjoin(lines, '\n');
  1 件のコメント
Piet Jonker
Piet Jonker 2019 年 4 月 9 日
Thanks a lot. I don't really have problems with processing so the loop works pretty well and is easy for me to edit and to use for other strings. The formatting is important (with line breaks), so the long string isn't useful for me. Thanks a lot for the additions and showing me the even more efficient ways of coding the string, it gives me a lot of insight.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

タグ

製品


リリース

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by