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 what I'm looking for:
sprintf('%somemagic ', [12345678 123.45])
ans = 1234578 123.45
where %somemagic is some magical specifier. None of the formats seem to work.
% no trailing zeros, but scientific for big nums
sprintf('%g ', [12345678 123.45])
ans = 1.23457e+007 123.45
% not approp for floats
sprintf('%d ', [12345678 123.45])
ans = 12345678 1.234500e+002
% trailing zeros
sprintf('%f ', [12345678 123.45])
ans = 12345678.000000 123.450000
% cannot specify sig figs after decimal (combo of gnovice's approaches)
mat = [12345678 123.45 123.456789012345];
for j = 1:length(mat)
fprintf('%s ', strrep(num2str(mat(j),20), ' ', ''));
end
I don't think there is a way to do it other than looping through each element and changing the specifier based off of mod(x,1)==0 or using regexp to remove trailing zeros. But you never know, the crowd is more clever than I.
My actual application is to print out the array elements in an html table. This is my current clunky solution:
for j = 1:length(mat)
if mod(mat(j),1) == 0
fprintf('<td>%d</td>', mat(j));
else
fprintf('<td>%g</td>', mat(j));
end
end