Can anyone tell me where I went wrong below that I get Invalid file identifier error? This code attempts to write data in different output files, where file name includes a number and strings.

ST = 14;
NR = 2
for r = 1:NR
file = fopen(['LadPos' int2str(ST+r) '.txt','w']);
fprintf([file,'Competition Ladder for Round' char(ST+r)]);
for i = 1:NT
fprintf(file,'%d %s %d\n',i,Classiclad(i,:),classic(i));
end
fclose(file);
....

Respuestas (2)

You wrote
fprintf([file,'Competition Ladder for Round' char(ST+r)])
the [ ] are misplaced, it should be :
fprintf(file,['Competition Ladder for Round' char(ST+r)])
Try this:
ST = 14;
NR = 2
NT = 3
for r = 1:NR
baseFileName = sprintf('LadPos%d.txt', ST+r);
fullFileName = fullfile(pwd, baseFileName)
fileID = fopen(fullFileName, 'wt');
fprintf('Competition Ladder for Round %d', (ST+r));
fprintf(fileID, 'Competition Ladder for Round %d' (ST+r));
for i = 1 : NT
fprintf(1,'%d %s %d\n',i,Classiclad(i,:),classic(i));
fprintf(fileID,'%d %s %d\n',i,Classiclad(i,:),classic(i));
end
fclose(fileID);
end

2 comentarios

You're welcome. Can you officially mark it as "Accepted" then? Thanks.

Iniciar sesión para comentar.

Categorías

Más información sobre Software Development Tools en Centro de ayuda y File Exchange.

Preguntada:

el 27 de Nov. de 2014

Comentada:

el 27 de Nov. de 2014

Community Treasure Hunt

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

Start Hunting!

Translated by