change numbers 9.95 and 11.32 to strings '09.95' and '11.32'

조회 수: 3 (최근 30일)
Rob
Rob 2025년 3월 28일
댓글: Walter Roberson 2025년 3월 29일
I tried, among some other things
num2str([9.95 11.32]', '%02.2f')
and
sprintf('%02.2f', [9.95 11.32])
  댓글 수: 1
Stephen23
Stephen23 2025년 3월 28일
In the context of your other question and comment:
this is highly relevant:
If you are actually working with dates/times then ask about that: tell us what data you have, what you need to do with it.

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2025년 3월 28일
sprintfc('%05.2f', [9.95 11.32])
ans = 1x2 cell array
{'09.95'} {'11.32'}
compose('%05.2f', [9.95 11.32])
ans = 1x2 cell array
{'09.95'} {'11.32'}
compose("%05.2f", [9.95 11.32])
ans = 1x2 string array
"09.95" "11.32"
  댓글 수: 4
Walter Roberson
Walter Roberson 2025년 3월 28일
이동: Walter Roberson 2025년 3월 29일
fid = 1;
m = 2; s = 3.54;
fprintf(fid, '%d:%05.2f\n', m, s)
2:03.54
Rob
Rob 2025년 3월 28일
이동: Walter Roberson 2025년 3월 29일
This is great, and so much simpler than some other suggestions. Thank you!!

댓글을 달려면 로그인하십시오.

추가 답변 (4개)

Steven Lord
Steven Lord 2025년 3월 28일
m = 2;
s = 3.54;
d1 = duration(0, m, s) % or
d1 = duration
00:02:03
d2 = minutes(m)+seconds(s)
d2 = duration
2.059 min
str1 = string(d1) % or
str1 = "00:02:03"
str2a = string(d2) % or
str2a = "2.059 min"
str2b = string(d2, 'hh:mm:ss.SS')
str2b = "00:02:03.54"

Engenuity
Engenuity 2025년 3월 28일
Would string(num2str([9.95 11.32]')) do it?
  댓글 수: 1
Engenuity
Engenuity 2025년 3월 28일
or
num2str([09.95 11.32]')
ans = 2x5 char array
' 9.95' '11.32'
ans(1,:) = strrep(ans(1, :), ' ', '0')
ans = 2x5 char array
'09.95' '11.32'
string(ans)
ans = 2x1 string array
"09.95" "11.32"

댓글을 달려면 로그인하십시오.


Umar
Umar 2025년 3월 28일

Hi @Rob,

You mentioned in your post, ”change numbers 9.95 and 11.32 to strings '09.95' and '11.32'”

Please see my response to your comments below. To convert the numbers 9.95 and 11.32 into the desired string format '09.95' and '11.32', you can utilize the sprintf function effectively. The format specifier %02.2f ensures that the numbers are formatted with two decimal places and padded with a leading zero if necessary. Here’s how you can achieve this:

% Define the numeric values
numbers = [9.95, 11.32];
% Convert to strings with the desired format
stringValues = arrayfun(@(x) sprintf('%05.2f', x), numbers, 'UniformOutput', 
false);
% Display the result
disp(stringValues);

Please see attached.

In this code, arrayfun applies the sprintf function to each element of the numbers array, resulting in a cell array of strings. This approach ensures that both numbers are correctly formatted as strings with leading zeros where applicable.

Hope this helps.


Rob
Rob 2025년 3월 28일
Ok, I guess I am not very good at asking questions yet.
I have two numbers, m and s. Let's say m = 2 and s= 3.54
I want to write a line to a file that contains '2:03.54'
I get the character string(?) '2' from num2str(m). I can get a variety of things that contain '03.54' using the suggestions above, but none of them can be used in fprintf(fid, [num2str(m),':',MagicFunction(s)])
  댓글 수: 4
Rob
Rob 2025년 3월 28일
Thank you for the suggestion, Umar. I have looked for a way to vote or "Accept this answer" for Walter Roberson's code, but apparently all I can do is vote for or 'Accept' at the level of my own "Ok, I guess I am not very good at asking questions yet." post, which feels both odd and not useful. Hence, I wrote my thank you for Walter's suggestion.
Walter Roberson
Walter Roberson 2025년 3월 29일
I moved my comments over to my Answer.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

제품


릴리스

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by