如何在单元格数组中显示n个小数位,这些位数也包含matlab中的字符串

时间:2013-12-04 09:45:39

标签: string matlab format cell-array

您好我有一个包含数字和字符串的单元格数组。我想打印单元格数组,这样我只会在小数点后看到5个数字,字符串需要相同

示例:

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

写完格式后,这就是我收到的内容

>> 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'

如果我写格式短,这就是我得到的:

>> 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]

但是我希望输出为:(没有[1x46字符]和小数点后的5个数字[也是4会好,但5会更好])

>> 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'

谢谢

1 个答案:

答案 0 :(得分:1)

您可以尝试 fprintf

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