How to write repeating string with variables in for loop

6 vues (au cours des 30 derniers jours)
Piet Jonker
Piet Jonker le 5 Avr 2019
Commenté : Piet Jonker le 9 Avr 2019
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 commentaire
Stephen23
Stephen23 le 5 Avr 2019
"I've tried the sprintf function, but can't get it to work."
Please show us what you tried.

Connectez-vous pour commenter.

Réponse acceptée

Dennis
Dennis le 5 Avr 2019
Modifié(e) : Dennis le 8 Avr 2019
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 commentaires
Guillaume
Guillaume le 5 Avr 2019
@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 le 8 Avr 2019
You are right, i corrected it.

Connectez-vous pour commenter.

Plus de réponses (1)

Guillaume
Guillaume le 5 Avr 2019
Modifié(e) : Guillaume le 5 Avr 2019
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 commentaire
Piet Jonker
Piet Jonker le 9 Avr 2019
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.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Data Type Conversion dans Help Center et File Exchange

Produits


Version

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by