Search code examples
stringmatlabformatcell-array

How to display with n decimal places from cell array that also contain strings in matlab


Hello I have a cell array that contains numbers, and also strings. I want to print the cell array so that I will see only 5 number after the decimal point, the strings need to be the same

Example:

for i=1:3
    c{1,i*2-1} = pi
    c{2,i*2-1} = pi/2
    c{3,i*2-1} = pi/4
    c{4,i*2-1} = 2^0.5

    c{1,i*2} = 'emperiment_number_7_result_21_total_5_and_ext'
    c{2,i*2} = 'emperiment_number_4_result_21_total_5_and_ext'
    c{3,i*2} = 'emperiment_number_6_result_21_total_5_and_ext'
    c{4,i*2} = 'emperiment_number_12_result_11_total_5_and_ext'
end

After writing format long this is what I receive

>> c
c = 
    [3.141592653589793]    'emperiment_number_7_result_21_total_5_and_ext'     [3.141592653589793]    'emperiment_number_7_result_21_total_5_and_ext'     [3.141592653589793]    'emperiment_number_7_result_21_total_5_and_ext' 
    [1.570796326794897]    'emperiment_number_4_result_21_total_5_and_ext'     [1.570796326794897]    'emperiment_number_4_result_21_total_5_and_ext'     [1.570796326794897]    'emperiment_number_4_result_21_total_5_and_ext' 
    [0.785398163397448]    'emperiment_number_6_result_21_total_5_and_ext'     [0.785398163397448]    'emperiment_number_6_result_21_total_5_and_ext'     [0.785398163397448]    'emperiment_number_6_result_21_total_5_and_ext' 
    [1.414213562373095]    'emperiment_number_12_result_11_total_5_and_ext'    [1.414213562373095]    'emperiment_number_12_result_11_total_5_and_ext'    [1.414213562373095]    'emperiment_number_12_result_11_total_5_and_ext'

If I write format short this is what I get:

>> c
c = 
    [3.1416]    [1x45 char]    [3.1416]    [1x45 char]    [3.1416]    [1x45 char]
    [1.5708]    [1x45 char]    [1.5708]    [1x45 char]    [1.5708]    [1x45 char]
    [0.7854]    [1x45 char]    [0.7854]    [1x45 char]    [0.7854]    [1x45 char]
    [1.4142]    [1x46 char]    [1.4142]    [1x46 char]    [1.4142]    [1x46 char]

But I want that the output will be: (no [1x46 char] and 5 numbers after the decimal [ also 4 will be good but 5 is better])

>> c
c = 
    [3.14159]    'emperiment_number_7_result_21_total_5_and_ext'     [3.14159]    'emperiment_number_7_result_21_total_5_and_ext'     [3.14159]    'emperiment_number_7_result_21_total_5_and_ext' 
    [1.57079]    'emperiment_number_4_result_21_total_5_and_ext'     [1.57079]    'emperiment_number_4_result_21_total_5_and_ext'     [1.57079]    'emperiment_number_4_result_21_total_5_and_ext' 
    [0.78539]    'emperiment_number_6_result_21_total_5_and_ext'     [0.78539]    'emperiment_number_6_result_21_total_5_and_ext'     [0.78539]    'emperiment_number_6_result_21_total_5_and_ext' 
    [1.41421]    'emperiment_number_12_result_11_total_5_and_ext'    [1.41421]    'emperiment_number_12_result_11_total_5_and_ext'    [1.41421]    'emperiment_number_12_result_11_total_5_and_ext'

Thank you


Solution

  • You can try fprintf

        for i=1:3
           fprintf('%1.5f %s %3.5f %s %5.5f %6s \n',c{i,:})
        end