I would like to compile a text string for each one of the three index of two given arrays
    2 Ansichten (letzte 30 Tage)
  
       Ältere Kommentare anzeigen
    
Hi,
Given two array XX=[1,2,3]  YY=[3,2,1]
and a typical string:
&DEVC ID='vel_XX', QUANTITY='VELOCITY', ABC=YY,0.0525,9.74/
where the bold letters represent the given array. I would like to write a string for each one of the three index of the arrays:
&DEVC ID='vel_1', QUANTITY='VELOCITY', ABC=3,0.0525,9.74/
&DEVC ID='vel_2', QUANTITY='VELOCITY', ABC=2,0.0525,9.74/
&DEVC ID='vel_3', QUANTITY='VELOCITY', ABC=1,0.0525,9.74/
Can You Help me?
0 Kommentare
Akzeptierte Antwort
Weitere Antworten (1)
  Rik
      
      
 am 29 Apr. 2021
        Easy with sprintf:
XX=[1,2,3];
YY=[3,2,1];
z=repmat("",size(XX));
FormatSpec='&DEVC ID=''vel_%d'', QUANTITY=''VELOCITY'', ABC=%d,0.0525,9.74/';
for n=1:numel(XX)
    z(n)=string(sprintf(FormatSpec,XX(n),YY(n)));
end
disp(z.')
2 Kommentare
  Rik
      
      
 am 29 Apr. 2021
				For completeness' sake: you should read the documentation for sprintf (or fprintf), which will show you the full list of options, including the %g flag that Stephen used in his answer.
Siehe auch
Kategorien
				Mehr zu Characters and Strings finden Sie in Help Center und File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


