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

I tried, among some other things
num2str([9.95 11.32]', '%02.2f')
and
sprintf('%02.2f', [9.95 11.32])

1 Kommentar

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.

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

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 Kommentare

m = 2;
s = 3.03;
minutes = floor(s);
seconds = mod(s, 1) * 60; % Convert fractional part to seconds
formatted_time = sprintf('%02d:%.2f', minutes, seconds)
formatted_time = '03:1.80'
Besides the issue that the user wants 3.54 to print out is "03:54" rather than as "03:40" (40 = 0.54 * 60), we see that your code does not print out the leading zeros if the seconds are in the range 0 to 9.
fid = 1;
m = 2; s = 3.54;
s = compose('%d:%05.2f', m, s)
s = 1x1 cell array
{'2:03.54'}
fprintf(fid, '%s\n', s{1})
2:03.54
fid = 1;
m = 2; s = 3.54;
fprintf(fid, '%d:%05.2f\n', m, s)
2:03.54
Rob
Rob am 28 Mär. 2025
Verschoben: Walter Roberson am 29 Mär. 2025
This is great, and so much simpler than some other suggestions. Thank you!!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (4)

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 am 28 Mär. 2025

0 Stimmen

Would string(num2str([9.95 11.32]')) do it?

1 Kommentar

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"

Melden Sie sich an, um zu kommentieren.

Umar
Umar am 28 Mär. 2025

0 Stimmen

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 am 28 Mär. 2025

0 Stimmen

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 Kommentare

Hi @Rob,

Thanks for clarifying. So, your goal is to convert these numbers into a string formatted as `'2:03.54'`, where: - The minutes (`m`) should be displayed directly. - The seconds (`s`) should be formatted to always show two digits before the decimal point, even if it's a single digit (i.e., `3.54` should become `03.54`). Here's how you can achieve this in MATLAB:

% Define the numbers
m = 2;
s = 3.54;
% Open a file for writing
fid = fopen('output.txt', 'w');
% Convert m to string
m_str = num2str(m);
% Convert s to string with leading zero for minutes
minutes = floor(s);
seconds = mod(s, 1) * 60; % Convert fractional part to seconds
formatted_time = sprintf('%02d:%.2f', minutes, seconds);
% Write to file
fprintf(fid, '%s\n', [m_str, ':', formatted_time]);
% Close the file
fclose(fid);

Please see attached.

Feel free to adapt this code snippet further based on any additional requirements or contexts you might have!

Hi @Rob,

Please don’t forget to vote and accept answer for @Walter Robertson. He did provide insightful feedback on the MATLAB code snippet. by identifiying two key issues that need to be addressed:

1. Incorrect Calculation of Seconds: The conversion from the fractional part of `s` to seconds is indeed miscalculated. The line `seconds = mod(s, 1) * 60;` is meant to convert the decimal portion of `s` into seconds, but it should instead take the entire value of `s`, not just its fractional part.

2. Leading Zero Formatting: The formatting of seconds using `sprintf('%02d:%.2f', minutes, seconds);` does not enforce two digits for the whole seconds portion when it is less than 10. This can lead to outputs like '03:1.80' instead of '03:01.80'.

Feel free to reach out if you still have further questions or need additional modifications!

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.
I moved my comments over to my Answer.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Characters and Strings finden Sie in Hilfe-Center und File Exchange

Produkte

Version

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by