I would like to compile a text string for each one of the three index of two given arrays
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
SYML2nd
el 29 de Abr. de 2021
Comentada: Rik
el 29 de Abr. de 2021
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 comentarios
Respuesta aceptada
Más respuestas (1)
Rik
el 29 de Abr. de 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 comentarios
Rik
el 29 de Abr. de 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.
Ver también
Categorías
Más información sobre Characters and Strings en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!