concatenate string with array

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 件のコメント

SALAH ALRABEEI
SALAH ALRABEEI 2022 年 5 月 31 日
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 2022 年 5 月 31 日
" 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
>>

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

 採用された回答

Voss
Voss 2022 年 5 月 31 日
編集済み: Voss 2022 年 5 月 31 日

0 投票

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 件のコメント

Zee
Zee 2022 年 6 月 1 日
Thank you, the first option works. However when I try to read the inputs for A, B and C from an excel file as I large set of input data, instead of getting the answer in C-sample1-sample2-(A,B) format I am getting as C-sample1-sample2-(C,C) format although right inputs have been assigned to the vectors A, B and C. Could you please let me know how can I correct this issue. Also may I know where can I learn more about functions like sprintfc which are not documented. Sorry, newbie here.
dpb
dpb 2022 年 6 月 1 日
sprintfc is just sprintf with the output cast to the cellstr (hence the "c" suffix). @_ has trotted it out here recently quite a bit; I've used MATLAB for 30 years and was unaware of it before, myself. Why TMW doesn't document its existence is a mystery/disservice to users; it is handy to have a builtin function that does the conversion; clearly obvious to be so since the developers found it useful enough to have created it as a builtin. I intended to post an enhancement request to the documentation but hadn't as yet...
You'll have to show us specifically what you did to create the spreadsheet and then how read it -- and then explain what it is that you expect. Writing out data in such string format combined to text is generally not a useful way to save things you want to read back in and retrieve the data from -- it may look good for some purpose, but it's a pain to have to parse going back the other way.
Explain the end purpose of all of this for context, not just the single line of code in isolation and someone may have better ways to solve the underlying problem.
Zee
Zee 2022 年 6 月 1 日
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 2022 年 6 月 1 日
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...

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

その他の回答 (1 件)

Matt J
Matt J 2022 年 5 月 31 日

0 投票

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 件のコメント

Walter Roberson
Walter Roberson 2022 年 5 月 31 日
User is R2011b, string objects and compose() did not exist back then...

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

カテゴリ

ヘルプ センター および File ExchangeData Type Conversion についてさらに検索

製品

リリース

R2011b

質問済み:

Zee
2022 年 5 月 31 日

コメント済み:

dpb
2022 年 6 月 1 日

Community Treasure Hunt

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

Start Hunting!

Translated by