Matlab sprintf formatting

前端 未结 2 1324
既然无缘
既然无缘 2021-01-22 10:50

EDIT: I\'ve reworded the question to be clearer.

Does anyone know a clever way to get sprintf to print \"%.6f with trailing zeros elminated\"? This is

2条回答
  •  Happy的楠姐
    2021-01-22 11:13

    EDIT: Updated to address the edited question...

    I don't think there's any way to do it with a particular format string for SPRINTF, but you could instead try this non-loop approach using the functions NUM2STR and REGEXPREP:

    >> mat = [12345678 123.45 123.456789012345];       %# Sample data
    >> str = num2str(mat,'%.6f');             %# Create the string
    >> str = regexprep(str,{'\.?0+<','\s'},{'<',''});  %# Remove trailing zeroes
                                                       %#   and whitespace
    >> fprintf(str);                                   %# Output the string
    
    12345678123.45123.456789  %# Output
    

提交回复
热议问题