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
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
12345678 123.45 123.456789 %# Output