I want to export a numerical array as a .csv file. So, the simplified term looks like this:
fid = fopen('output.csv','wt')
toprint = [1.0, 1.1, 1.2, 1.3];
fprintf(fid, '%f, %f, %f, %f\n', toprint);
fclose(fid)
In this case there is no problem. I use %f
in string format to maintain precision. However, sometimes, or rather usually, there are zeros in the array like this:
toprint = [1.0, 0, 0, 1.1];
In such situation, I want to adjust the string format to:
'%f, %d, %d, %f\n' % where "%f" were replaced by "%d" at the positions of the zeros
to reduce output file size since I do not need the precision of zero numbers. The original solution I applied was to detect data types through the array. If zero was detected, then concatenate '%d' onto string format. But it seems to be very inefficient.
What I am looking for is a efficient method to adjust string format depending on input data. Is there any way to achieve this?