How can I fix my function?

20 ビュー (過去 30 日間)
michael story
michael story 2018 年 10 月 29 日
編集済み: michael story 2018 年 10 月 29 日
function numReplaced=replaceMultiples(fname,num)
clc
num=0
fname='example.txt';
ifn=fname;
ofn=['updated%d %s',num,fname];
ifh=fopen(ifn,'r'); % open the file for reading
ofh=fopen(ofn,'wt'); %open the output file for writing
ln=fgetl(ifh); % try to get the first line of the file
while str2num(fname)
while ~isnan(num)
[tk,ln]=strtok(ln);
if mod(fname,num)==0
fprintf(ofh,'%s ',tk); %write to output file
end
end
fprintf(ofh,'\n'); %after the complete line has
ln=fgetl(ifh);
end
fclose(ifh); % close the file
fclose(ofh);

採用された回答

Walter Roberson
Walter Roberson 2018 年 10 月 29 日
Do not assign to fname or num in your code.
str2double(fname) is attempting to interpret the file name itself as a number.
Your code is converting file contents into tokens but it then tries to take modulo on the file name itself instead of examining the content it tokenized.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeFilename Construction についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by