Can anybody tell me why could the shown warning appear and "It" is not a natural number without zeros after the decimal point?

2 views (last 30 days)
"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.

Accepted Answer

Image Analyst
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
Pavlin
Pavlin on 3 Feb 2013
Does anybody not know? If I make "it" an integer, that's fine, but I still get the same warning and the error that nothing is written / displayed in the end, when the loops are over and I type the written file's name.
Image Analyst
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?

Sign in to comment.

More Answers (1)

Jan
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
Pavlin
Pavlin on 4 Feb 2013
What astonishes me is that I get this 1.0000, 2.0000 and so on for the following (by skipping everything that does not concern "it"):
fid=fopen('Project_3_VCCT','w');
Text=([' It Applied Load a f_dof F1 u_dof'...
' u G_I K_I LOAD_displ']);
fprintf(fid,'% s\n',Text);
Text=([' # N mm # N #'...
' mm J/m MPa*m^0.5 mm']);
fprintf(fid,'% s\n',Text);
.................
.................
it=0;
for incr=1:incr_max
while Flag_G_IC==0 && it<itmax && Flag_last_el_x==0
it=it+1;
.............
.............
results(1,1) = it;
results(1,2) = load_total;
results(1,3) = a*1000;
results(1,4) = f_dof;
results(1,5) = F1;
results(1,6) = u_dof;
results(1,7) = u*1000;
results(1,8) = G_I;
results(1,9) = K_I*1e6;
results(1,10) = U(Initial_Crack_Tip_x*(elements_y+1)*2+2,1)*1000;
results(1,11) = el_L(Crack_Tip_x)*1000;
fprintf([fid,'%3.0f %14.3f %10.3f %7.0f %10.3f %7.0f %10.6f '...
'%12.3f %15.3f %14.6f \n',results]);
end
end
fclose(fid);
The warning is for the fprintf line and afterwards, no results are really written while the while loop runs. I see the numbers for it by taking away the semicolon of "results(1,1) = it". More interesting for me is that in the first few iterations "it" appears as an integer and then it has the decimal point. If necessary, I can provide the whole code, but only if anybody is interested in why these situations happen. I'd rather use the xlswrite function, which I've tried and it worked, so I can avoid the problem. Another interesting (for me, as I am not that an expert) is that the results are being written and shown in the end, if I type:
fprintf(fid, '%g \n' ,results);
However, that does not arrange them in a nice and usable way.
Thank you for your time! If anybody is interested, just e- mail me and I will provide the code and the supporting functions to run it.
Regards, Pavlin Todorov
Jan
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

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!