Can anybody tell me why could the shown warning appear and "It" is not a natural number without zeros after the decimal point?
9 views (last 30 days)
Show older comments
"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.
0 Comments
Accepted Answer
Image Analyst
on 2 Feb 2013
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.
5 Comments
Image Analyst
on 3 Feb 2013
We don't know the exact line of code that generates the error "Warning: Out of range or non-integer values truncated during conversion to character." Since you converted "it" to integers, we don't know if it's referring to a different non-integer variable, or if "it" is out of range. What does the help on that warning say?
More Answers (1)
Jan
on 3 Feb 2013
Edited: Jan
on 3 Feb 2013
You wrote:
I meet the kind of error at other places, too. The division 4/2 is sometimes 2 and other times 2.0000.
No, dividing 4 by 2 gives exactly 2 in every case. Results like "2.0000" come from calculations like 4.00000000000002/2, which you use the display format "short":
format short
4.00000000000002/2
format long g
4.00000000000002/2
Here you do not observe an error or bug, but just the correct calculations with numbers, which are represented with a limited precision.
Please show us the line, which causes the warning.
2 Comments
Jan
on 5 Feb 2013
Edited: Jan
on 5 Feb 2013
The square brackets in this line should cause problems:
fprintf([fid,'%3.0f %14.3f %10.3f %7.0f %10.3f %7.0f %10.6f '...
'%12.3f %15.3f %14.6f \n',results])
You do not want to create a joined vector of these variable and deliver one vector to fprintf(). So simply omit these brackets as Image Analyst has suggested already.
When I understand your problem correctly, and I do not have the impression that I do, check if your "it" is integer by inserting this at several locations:
if it ~= floor(it), keyboard, end
See Also
Categories
Find more on Entering Commands in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!