fopen just works inside of a loop

조회 수: 19 (최근 30일)
Chris Topher
Chris Topher 2020년 11월 9일
댓글: Image Analyst 2020년 11월 10일
Hello,
I currently have a performance problem which should be easy to solve i think. The code here is a minimalistic example of the problem. It´s in 2 seperate .m-files. The "main" function (which is an optimizer) calls the "loop" function with the given values. The "loop" function calls the other m.-file "dothat" in every while-loop.
function main
a = 2;
b = 3;
loop(a, b)
end
function value = loop(a, b)
while a <= 20
dothat
a = a + b;
end
end
The "dothat" file needs additional data from a .txt - file in every loop to interpolate a value (in the example just shows a text).
b = b+1;
fid = fopen('example.txt' , 'r');
tline = fgetl(fid)
That works fine, but in the real code the while loop "loops" a few thousand times so i also have to add an fclose command in the "dothat" file to prevent the "too many files open" error. So the code opens and closes the files thousands of times, and the simulation needs minutes instead of seconds. Is there a way to get the fopen-command out of the loop? It just works when fopen is in the "dothat" file or in the "loop" function, but just inside of the while-loop. Otherwise MATLAB cant open the file or doesn´t know "fid".
Thanks for any help, files are in the attachments!
  댓글 수: 4
Chris Topher
Chris Topher 2020년 11월 10일
It seems like it only remembers the file for one loop.
Chris Topher
Chris Topher 2020년 11월 10일
I uploaded everything in the original question.

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

채택된 답변

Image Analyst
Image Analyst 2020년 11월 10일
Your dothat.m has a call to fopen() in it. But nowhere do you have a call to fclose(). Why not? You should. If you're opening the same file, you should open it just once, before the loop, and close it after the loop:
function value = loop(a, b)
fid = fopen('example.txt' , 'rt'); % Open text file as 'rt' just once.
while a <= 20
dothat % Must not have a call to fopen() in this function.
a = a + b;
end
fclose(fid)
end
and do NOT have a call to fopen in dothat.m.
If you need to do sequential access on fid using fgetl(), then it's possible you might want to rewind the pointer, depending on if you need to check the file from the beginning or not.
frewind(fid);
  댓글 수: 2
Chris Topher
Chris Topher 2020년 11월 10일
Thank you! It now works perfectly with just putting a frewind(fid) at the end of every loop. That´s what I´ve needed and it makes perfectly sense.
Image Analyst
Image Analyst 2020년 11월 10일
OK, but you still need to use fclose() at some point. Don't forget that!!!

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

추가 답변 (1개)

Fangjun Jiang
Fangjun Jiang 2020년 11월 9일
Whatever data you need from the text file, you could read it once, store it in a variable and use it in the loop. You wouldn't drive your car to a store 5 times in a roll back and forth to buy 5 items

카테고리

Help CenterFile Exchange에서 Low-Level File I/O에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by