concatenate string with array

14 visualizaciones (últimos 30 días)
Zee
Zee el 31 de Mayo de 2022
Comentada: dpb el 1 de Jun. de 2022
Hello,
I have the following data which I need to bring to a certain format.
A= [23.5567, 5.34567, 23.1]
B= [-13.5357, 6.254, 2101.2]
C= [4,8,15]
I want to add some string value and certain characters to the above vectors and combine it to get the following format of single column vector:
4-sample1-sample2-(23.56,-13.54)
8-sample1-sample2-(5.35, 6.25)
15-sample1-sample2-(23.10, 2101.20)
Also to get the above format I want to round of A and B vector values to two decimal places but the the earlier Matlab version doesn't have rounding to certain places function.
  2 comentarios
SALAH ALRABEEI
SALAH ALRABEEI el 31 de Mayo de 2022
For the rounding part,
format short
A= [23.5567, 5.34567, 23.1];
B= [-13.5357, 6.254, 2101.2];
C= [4,8,15];
A=round(A,2)
A = 1×3
23.5600 5.3500 23.1000
B=round(B,2)
B = 1×3
1.0e+03 * -0.0135 0.0063 2.1012
C=round(C,2)
C = 1×3
4 8 15
dpb
dpb el 31 de Mayo de 2022
" the earlier Matlab version doesn't have rounding to certain places function."
As the other responders have shown, you can use the i/o library functions with formatting strings to write to desired precision. If the object is rounding, one can then convert the result back to numeric w/ str2double
num2str has had a precision optional argument "since forever" but it is the total number of significant digits so not quite so helpful here.
I see that the optional argument to round wasn't added until R2014b in the Compatibility notes -- that surprises me to be reminded it's that recent an enhancement.
But, it's easy-peasy to do yourself --
Round=@(x,n)round(x*10^n)/10^n;
>> Round(pi,2)
>> ans =
3.1400
>>

Iniciar sesión para comentar.

Respuesta aceptada

Voss
Voss el 31 de Mayo de 2022
Editada: Voss el 31 de Mayo de 2022
A= [23.5567, 5.34567, 23.1];
B= [-13.5357, 6.254, 2101.2];
C= [4,8,15];
One way:
sprintfc('%d-sample1-sample2-(%0.2f,%0.2f)',[C; A; B].')
ans = 3×1 cell array
{'4-sample1-sample2-(23.56,-13.54)' } {'8-sample1-sample2-(5.35,6.25)' } {'15-sample1-sample2-(23.10,2101.20)'}
Another way:
arrayfun(@(a,b,c)sprintf('%d-sample1-sample2-(%0.2f,%0.2f)',c,a,b),A,B,C,'Uniform',false).'
ans = 3×1 cell array
{'4-sample1-sample2-(23.56,-13.54)' } {'8-sample1-sample2-(5.35,6.25)' } {'15-sample1-sample2-(23.10,2101.20)'}
  4 comentarios
Zee
Zee el 1 de Jun. de 2022
Thank you for explaining. I am just trying to explore different functions and learning from mistakes and troubleshooting at the moment. The second option works and I will read more about this function.
dpb
dpb el 1 de Jun. de 2022
The two return identically the same output; in fact the second is the equivalent of what the internal definition of sprintfc is -- a wrapper that turns the char() array output of sprintf that creates only a single character array for each invocation into a cell array of cellstr() for each element of the arguments passed to sprintf
To see what the difference is, try
sprintf('%d-sample1-sample2-(%0.2f,%0.2f)',C,A,B)
at the command line and see what it returns -- and why the cell array was useful.
Then try
sprintf('%d-sample1-sample2-(%0.2f,%0.2f)\n',C,A,B)
and
whos ans
after each and observe...

Iniciar sesión para comentar.

Más respuestas (1)

Matt J
Matt J el 31 de Mayo de 2022
A= [23.5567, 5.34567, 23.1];
B= [-13.5357, 6.254, 2101.2];
C= [4,8,15];
formatSpec="%d-sample1-sample2-(%.2f, %.2f)";
str = compose(formatSpec,C',A',B')
str = 3×1 string array
"4-sample1-sample2-(23.56, -13.54)" "8-sample1-sample2-(5.35, 6.25)" "15-sample1-sample2-(23.10, 2101.20)"
  1 comentario
Walter Roberson
Walter Roberson el 31 de Mayo de 2022
User is R2011b, string objects and compose() did not exist back then...

Iniciar sesión para comentar.

Categorías

Más información sobre Data Type Conversion en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2011b

Community Treasure Hunt

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

Start Hunting!

Translated by