Find more time-efficient way to extract data from a .txt file
1 次查看(过去 30 天)
显示 更早的评论
I have a code that takes over 4minutes to converge to a solution, and I need to get convergence about 30 times for synthesis purposes, resulting in over 2hrs of run time. I found that what is slowing my code down so much is the fopen function.


The files with the function name "intermediate_GW" and "intermediate_GW2" read a single data point with the format #####.# from a text file outputted from my gross weight solver (not listed above), and then, they output this value to the functions FF, DISK, and SW_TOTAL. "intermediate_GW2" stays the same per iteration of this WEIGHT_FUEL program while "intermediate_GW" changes per call. Is there a way for me to have the values from these two programs in a better format for this purpose? Below is a rough flowchart of how my programs are going to run. I can't have the TOTAL_WEIGHT program directly output to my other programs because this causes an infinite recursion, and I had to bypass that by using txt files.
If it could be done such that the below loop of intermediate_GW2, DISK, and SW_TOTAL can be called only once instead of 40000 times, this alone would drastically improve the runtime of my code.

9 个评论
dpb
2020-3-8
"...with one text file changing and the other remaining constant."
That doesn't seem to be so...
...
i=i+1;
fileID = fopen('intermediate_GW.txt','w');
fprintf(fileID,'%.1f\n',GW);
fclose(fileID);
fileID = fopen('intermediate_GW2.txt','w');
fprintf(fileID,'%.1f\n',GW);
fclose(fileID);
end
writes the same value to both files every iteration.
Presuming the
GW = intermediate_GW2;
function is reading the file of that name, the new GW value in SW_TOTAL(y) is the same one just written.
In which case, just pass it (new GW) in the argument list and bypass the file.
When you get to the bottom and GW=sum(W), THEN you do have a difference again that you can test for convergence.
I can't see any reason for the file at all if you just pass the information as an additional argument to the functions that need it.
采纳的回答
dpb
2020-3-8
While I'm convinced you really don't need the files at all and probably with only a minor amount of rearranging if it works as is, one really expedient workaround would be to not open/close the files every time...
function [GW,W] = WEIGHT_SIZING(H,IT,x,y)
...
i = 1; %Used for iterations
fID1 = fopen('intermediate_GW.txt','w');
fID2 = fopen('intermediate_GW2.txt','w');
...
while epsilon > 1
...
i=i+1;
frewind(fID1)
fprintf(fID1,'%.1f\n',GW);
frewind(fID1)
frewind(fID2)
fprintf(fID2,'%.1f\n',GW);
frewind(fID2)
end
fID1=fclose(fID1); % release file handles only when completed iteration loop
fID2=fclose(fID2);
end
You then either add fID1, fID2 to the argument list to the functions that need them, or, (ugly, but expedient) make them GLOBAL variables.
5 个评论
dpb
2020-3-8
Your call, of course. If it's fast enough this way, then perhaps there's no real justification for spending the time now to refactor it.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Parallel and Cloud 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!