for loop doesn't increment beyond 127

I'm trying to write a file with numbers and their respective multiplication with 2. But the printed file does not have values beyond 127.
fileID = fopen('exp.txt','w');
k=[0:3800];
for t = k(1:1:end)
A=[t,t*2];
fprintf(fileID,'%d %d\r\n',A);
end
fclose(fileID);
Help me with this issue. Thanks in advance

回答 (3 件)

Steven Lord
Steven Lord 2015 年 7 月 28 日

1 投票

Check the CLASS of the variable that you're printing to the file. I'd bet you that it is an int8 array and also that the code you posted is not your actual code. The INT8 data type can store values between -128 and 127 inclusive; values greater than 127 stored in int8 saturate.
x = int8(12345678) % x will be 127
Andreas Goser
Andreas Goser 2015 年 7 月 28 日

0 投票

On my machine - MATLAB R2015a and Win7 64Bit - this produces all expected 3801 lines. In order to find out what is going wrong within you installation, you need to debug. Does the loop exit earlier than expected? Maybe "end" is variable on your workpace? Or is the loop complete and just the printing does not work?
Image Analyst
Image Analyst 2015 年 7 月 29 日

0 投票

No idea, but Steve is probably right. Anyway, that's kind of a weird for loop anyway, just try it this way instead:
fileID = fopen('exp.txt','wt');
if fileID ~= -1
for t = 0 : 3800
fprintf(fileID,'%4d %4d\n', t, 2*t);
end
fclose(fileID);
else
message = sprintf('Error opening file exp.txt');
uiwait(warndlg(message));
end

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

質問済み:

2015 年 7 月 28 日

回答済み:

2015 年 7 月 29 日

Community Treasure Hunt

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

Start Hunting!

Translated by