MATLAB: Can anybody tell me why could the shown warning appear and “It” is not a natural number without zeros after the decimal point

truncation error

"Warning: Out of range or non-integer values truncated during conversion to character." ———————————————
Parts of the Code defining and showing "it":
…..
fid=fopen('Project_3_VCCT','w');
Text=([' it ']);
fprintf(fid,'% s\n',Text);
it=0;
for incr=1:incr_max
load_total=load_total+load_increment;
Flag_G_IC=0;
while Flag_G_IC==0 && it<itmax && Flag_last_el_x==0
it=it+1;
.....
.....
results(1) = it
.....
fprintf([fid,'%3.0f \n',results]);
.....
end
end
fclose(fid);
type Project_3_VCCT
---------------------------------------------------------------------------
In the end I do not see the results (after "type"), but I by removing the semicolon I saw the it is:
It=1.000
It=2.000
.....
This should be the source of warning / error, but I do not understand why is "It" not just 1,2,3,4… at the first place.
Thank you in advance!
Regards,
Pavlin Todorov
P.S. I meet the kind of error at other places, too. The division 4/2 is sometimes 2 and other times 2.0000.

Best Answer

If you want to ensure "it" is an integer, make it an integer:
it = int32(it + 1);
then results should also be an integer, but you need to print it as an integer, using %d instead of %f:
fprintf(fid, '%d\n', results);
The brackets are unnecessary so I removed them.