a=0.222833 a=sprintf('%.6f',a) Answer from Azzi Abdelmalek on mathworks.com
Discussions

Converting an array of doubles to a string
Converting an array of doubles to a string. Learn more about doubles, string MATLAB More on mathworks.com
🌐 mathworks.com
1
0
January 27, 2021
Converting "double" to "string"
sprintf gives you control over the formatting: http://www.mathworks.com/help/matlab/ref/sprintf.html#input_argument_formatspec More on reddit.com
🌐 r/matlab
2
0
April 13, 2015
matlab - convert from double to strings - Stack Overflow
I have the matrix of double A A=[1 1 1 2 1; 2 1 1 2 1; 3 1 1 2 1; 4 1 1 2 1; 1 2 1 2 1; 2 2 1 2 1; 3 2 1 2 1; 4 2 1 2 1]; and I want to convert it in a matri... More on stackoverflow.com
🌐 stackoverflow.com
double to string in a cell
double to string in a cell. Learn more about double to string More on mathworks.com
🌐 mathworks.com
2
0
June 17, 2013
🌐
MathWorks
mathworks.com › matlabcentral › answers › 33607-converting-double-to-string
converting double to string - MATLAB Answers - MATLAB Central
March 27, 2012 - I am making a vigenere cipher function with a varying alphabet, with inputs (message,key,alphabet) e.g ('AVECAESAR','ROME','A':'Z') I presume I have to convert alphabet to string, and then wo...
🌐
MathWorks
mathworks.com › matlab › language fundamentals › data types › data type conversion
num2str - Convert numbers to character array - MATLAB
Update code that makes use of num2str to combine numeric scalars with text to use string instead. Numeric values can be combined with strings using the + operator. For example: ... The input precision must be compile time constant. For the syntax num2str(A,formatSpec), A and formatSpec must ...
🌐
Reddit
reddit.com › r/matlab › converting "double" to "string"
r/matlab on Reddit: Converting "double" to "string"
April 13, 2015 -

I'm working with some data that lists the duration (time) of an experiment as, "1.6420e+04". The type given by class(Duration) is double. I'd like to convert this to a string that reads "1.6420e+04" but can't find a good way to do it. sprintf('%f',Duration) returns 16420.479075, num2str also gives the incorrect answer (though there is apparently a precision option that might be able to help which I can't figure out) and there doesn't seem to be any double2str. Does anyone know a way to accomplish this or how to convert this double into a more readable float?

Top answer
1 of 4
6

One way to do this is to use sprintf to convert the array to a long string of digits. You can then reshape this string into the appropriate shape. Then you can use cellstr to convert each row of the reshaped string into a separate cell array element.

out = cellstr(reshape(sprintf('%d', A), [], size(A,2)));

Explanation

First convert the matrix into a long string of digits.

s = sprintf('%d', A)
%// 1234123411112222111111112222222211111111 

Then we want to reshape this so that each row of numbers in the original is a row of numbers in the output

s = reshape(s, [], size(A,2))
%// 11121
%// 21121
%// 31121
%// 41121
%// 12121
%// 22121
%// 32121
%// 42121

Then we can use cellstr to convert each row of this into it's own cell array

out = cellstr(s);
%// '11121'
%// '21121'
%// '31121'
%// '41121'
%// '12121'
%// '22121'
%// '32121'
%// '42121'

A different approach

Another way that you could accomplish this is to treat each column of A as a place value (i.e. 10000's place, 1000's, place, 100's place, etc.) and convert each row to an integer knowing that. This can easily be done by multiplying each row with an array of 10^(N-1:-1:0) and summing the elements. This will yield a digit for each row that combines all of the columns. We can then use num2str to convert this to a cell array of strings.

%// Then convert each number to a string in a cell array
out = arrayfun(@num2str, A * (10.^(size(A, 2)-1:-1:0)).', 'uni', 0);

Or to shorten this even more, we can borrow a page out of @rayryeng's book and use sprintfc to convert this array of integers into a cell array of strings:

out = sprintfc('%d', A * (10.^(size(A, 2)-1:-1:0)).');

Benchmark

I was curious about the performance of the methods presented here and in @rayryeng's answer and Dev-iL's answer when you increase the number of rows. I wrote up a quick test script.

function tests()
    % Test the number of rows between 100 and 10000
    nRows = round(linspace(100, 10000, 100));

    times1 = zeros(numel(nRows), 1);
    times2 = zeros(numel(nRows), 1);
    times3 = zeros(numel(nRows), 1);
    times4 = zeros(numel(nRows), 1);
    times5 = zeros(numel(nRows), 1);

    %// Generate a random matrix of N x 5
    getRandom = @(n)randi([0, 9], [n, 5]);

    for k = 1:numel(nRows)
        A = getRandom(nRows(k));
        times1(k) = timeit(@()string_reshape_method(A));
        A = getRandom(nRows(k));
        times2(k) = timeit(@()base10_method(A));
        A = getRandom(nRows(k));
        times3(k) = timeit(@()sprintfc_method(A));
        A = getRandom(nRows(k));
        times4(k) = timeit(@()addition_method(A));
    end

    %// Plot the results
    plot(nRows, cat(2, times1, times2, times3, times4)*1000);
    legend({'String Reshape', 'Base-10 Conversion', 'sprintfc', 'addition of "0"'})

    xlabel('Number of Rows in A')
    ylabel('Execution Time (ms)');
end

function out = string_reshape_method(A)
    out = cellstr(reshape(sprintf('%d', A), [], size(A,2)));
end

function out = base10_method(A)
    out = sprintfc('%d', A * (10.^(size(A, 2)-1:-1:0)).');
end

function B = sprintfc_method(A)
    B = sprintfc(repmat('%d', 1, size(A,2)), A);
end

function B = addition_method(A)
    B = cellstr(char(A + '0'));
end

Here are the results.

2 of 4
5

My suggestion is this:

out = cellstr(char(A + '0'));

Basically what we do is add the ASCII value of 0 to your matrix then convert it to characters. I didn't benchmark it, but it should be comparably fast :)

Find elsewhere
🌐
MathWorks
mathworks.com › matlabcentral › answers › 79327-double-to-string-in-a-cell
double to string in a cell - MATLAB Answers - MATLAB Central
June 17, 2013 - Hi, is there an easier way to convert from double to string in a cell? Thank you Left = {num2str(variable1) num2str(variable2) num2str(variable3) etc...} ...
🌐
MathWorks
mathworks.com › matlabcentral › fileexchange › 1777-from-double-to-string
From Double To String - File Exchange - MATLAB Central
June 3, 2002 - A conversion from a float to a binary string representation is provided. ... Two functions, num2bin and bin2num are provided. num2bin takes a double and converts it to a binary string representation. bin2num is its inverse operation.
🌐
MathWorks
mathworks.com › matlab › language fundamentals › data types › data type conversion
str2double - Convert strings to double precision values - MATLAB
Calling string and then double is recommended over str2double because it provides greater flexibility and allows vectorization. For additional information, see Alternative Functionality. X = str2double(str) converts the text in str to double precision values.
Top answer
1 of 2
4

Here are two simpler solutions to convert a single double value to a string and back without loss.

I want the string to be a human-readable representation of the number

Use num2str to obtain 17 decimal digits in string form, and str2double to convert back:

>> s = mat2str(x,17) 
s =
    '2.2204460492503131e-16'
>> y = str2double(s);
>> y==x
ans =
  logical
   1

Note that 17 digits are always enough to represent any IEEE double-precision floating-point number.

I want a more compact string representation of the number

Use matlab.net.base64encode to encode the 8 bytes of the number. Unfortunately you can only encode strings and integer arrays, so we type cast to some integer array (we use uint8 here, but uint64 would work too). We reverse the process to get the same double value back:

>> s = matlab.net.base64encode(typecast(x,'uint8'))
s =
    'AAAAAAAAsDw='
>> y = typecast(matlab.net.base64decode(s),'double');
>> x==y
ans =
  logical
   1

Base64 encodes every 3 bytes in 4 characters, this is the most compact representation you can easily create. A more complex algorithm could likely convert into a smaller UTF-8-encoded string (which uses more than 6 bytes per displayable character).

2 of 2
3

Function f: from double real-valued scalar x to char vector str

str = num2str(typecast(x, 'uint8'));

str is built as a string containing 8 numbers, which correspond to the bytes in the internal representation of x. The function typecast extracts the bytes as a numerical vector, and num2str converts to a char vector with numbers separated by spaces.

Function g: from char vector str to double real-valued scalar y

y = typecast(uint8(str2double(strsplit(str))), 'double');

The char vector is split at spaces using strsplit. The result is a cell array of char vectors, each of which is then interpreted as a number by str2double, which produces a numerical vector. The numbers are cast to uint8 and then typecast interprets them as the internal representation of a double real-valued scalar.

Note that str2double(strsplit(str)) is preferred over the simpler str2num(str), because str2num internally calls eval, which is considered evil bad practice.

Example

>> format long
>> x = sqrt(pi)
x =
   1.772453850905516
>> str = num2str(typecast(x, 'uint8'))
str =
    '106  239  180  145  248   91  252   63'
>> y = typecast(uint8(str2double(strsplit(str))), 'double')
y =
   1.772453850905516
>> x==y
ans =
  logical
   1
🌐
MathWorks
mathworks.com › requirements toolbox › author and validate requirements › model and validate requirements
str2double - Convert string to double-precision value in Requirements Table block - MATLAB
X = double(str) is an alternative way to execute str2double(str). ... In a Requirements Table block, convert the string "-12.345" to a double and output the value.
🌐
MathWorks
mathworks.com › matlabcentral › answers › 1959914-using-string-on-double-values-without-automatic-rounding
Using string() on double values without automatic rounding? - MATLAB Answers - MATLAB Central
May 8, 2023 - My question is wether it is possible to do this double->string conversion without this rounding. I hope i have made myself more clear, sorry if my initial formulation was off. ... https://www.mathworks.com/matlabcentral/answers/1959914-using-string-on-double-values-without-automatic-rounding#comment_2738349
🌐
MathWorks
mathworks.com › stateflow › simulation in simulink › data specification › string data
str2double - Convert string to double-precision value in Stateflow chart - MATLAB
X = double(str) is an alternative way to execute str2double(str) in charts that use MATLAB as the action language. ... Stateflow® charts that use C as the action language support calling double only with numeric arguments. ... Convert the string "-12.345" to a double-precision numeric value.