I have this code that looks something like the below (I cant upload the actual code). I get an error that says that: Error using fprintf Invalid File Identifier in fprintf(fid,'fgfdgfdgfd'). The weird thing is that this does not happen immediately after I run the code. The code runs smoothly and after 20 minutes of running or so I get this error. Any reasons why you would think this happen?
Edit: when I set ii, jj, kk at the ones that the code failed at and generated that error, it works just fine...I dont understand why at some point it threw that error. This is the third time I attempt running the code and it still does this.
x = 1:1:10
y = 0.1:0.01:0.2;
z = 1:0.25:3;
for ii = 1:length(x)
for jj = 1:length(y)
for kk = length(z)
fid = fopen('for1234.dat','w');
fprintf(fid,'fgfdgfdgfd')
fprintf(fid,'ASHGGEfc')
fprintf(fid,'fdWDG')
fclose(fid)
%AND THEN I RUN SOME PROCESSES using the system command
end
end
end

답변 (1개)

VBBV
VBBV 2023년 1월 25일
편집: VBBV 2023년 1월 25일

0 개 추천

fid = fopen('for1234.dat','w+');
fclose(fid)
Since you are writing the data repeatedly to the same file, place the above first line before all for loops and second line after all for loops.

댓글 수: 5

x = 1:1:10
y = 0.1:0.01:0.2;
z = 1:0.25:3;
fullFileName = fullfile(pwd, 'for1234.dat');
fid = fopen(fullFileName,'wt');
if fid == -1
% Throw error message if we can't open the output file.
errorMessage = sprintf('Error opening "%s".\n', fullFileName)
errordlg(errorMessage);
return; % Quit program.
end
for ii = 1:length(x)
for jj = 1:length(y)
for kk = length(z)
fprintf(fid,'fgfdgfdgfd')
fprintf(fid,'ASHGGEfc')
fprintf(fid,'fdWDG')
%AND THEN I RUN SOME PROCESSES using the system command
end
end
end
fclose(fid)
Ali Almakhmari
Ali Almakhmari 2023년 1월 25일
I tried @VBBV suggestion but I keep getting this message:
forrtl: The process cannot access the file because it is being used by another process.
forrtl: severe (30): open failure, unit 5,"The path to the folder I am editing"
Ali Almakhmari
Ali Almakhmari 2023년 1월 25일
편집: Ali Almakhmari 2023년 1월 25일
Its interesting. I get those lines of error only after I run the "system" command after finishing writing the file in the for loop. Should I prehaps close the file before the system command?
Voss
Voss 2023년 1월 26일
편집: Voss 2023년 1월 26일
"Should I prehaps close the file before the system command?"
Yes. If the .dat file you're writing is used by the program you're calling in the system command, and you want to create a new .dat file on each iteration of your loops (i.e., each time you call the external program), then you should open and close the .dat file like you were originally doing.
Ali Almakhmari
Ali Almakhmari 2023년 1월 26일
then if my original code that I posted was fine...why did the code fail?

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Programming Utilities에 대해 자세히 알아보기

제품

릴리스

R2021a

질문:

2023년 1월 25일

댓글:

2023년 1월 26일

Community Treasure Hunt

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

Start Hunting!

Translated by