fprintf in a for-loop

Hi,
I'm looking for a way to make my code faster. I'm facing the following problem:
I have a for-loop of 1:length(events). If the event == 1 the event and venue of the event is written to a text file. However, as the length of events is quite large, it takes some time to write the entire text file. I was wondering if there is a way to first store the array first and write it to the text file once the entire for-loop has been processed?
This is my code:
for f=events
fprintf(fileid, ' Event%03d: ' , f);
counter=1;
for v= venue_name
if event(f)==1
if counter==1
fprintf(fileid, ' X(%04d,%s) ',event(f),venue_name{v});
counter=counter+1;
else
fprintf(fileid, ' + X(%04d,%s) ',event(f),venue_name{v});
end
end
end
end
Can anyone help me with adapting this code such that it just writes the datafile at the end instead of once every iteration?

回答 (1 件)

Jan
Jan 2016 年 12 月 1 日
編集済み: Jan 2016 年 12 月 1 日

0 投票

The runtime of the code is not the bottleneck, but the disk access. Therefore creating the large string at first has the additional disadvantage, that a lot of RAM must be allocated.
Most likely using an efficient buffering is more useful. How do you open the file? Try:
fileid = fopen(FileName, 'W'); % Uppercase!
Some small improvements of the code:
for f = events
fprintf(fileid, ' Event%03d: ', f);
if event(f)==1 % Move outside the "v" loop
first = true;
for v = venue_name % ??? 1:numel(venue_name) ???
if first
fprintf(fileid, ' X(%04d,%s) ',event(f),venue_name{v});
first = false;
else
fprintf(fileid, ' + X(%04d,%s) ',event(f),venue_name{v});
end
end
end
end

4 件のコメント

Bas
Bas 2016 年 12 月 1 日
Thanks this saves me about 15% time already. I opened it with the 'w' instead of 'W'. Do you have any other solutions to improve even more?
Jan
Jan 2016 年 12 月 1 日
Please explain, if you really mean "for v = venue_name" and "venue_name{v}". This looks strange. The same for "for f = events" together with "event(f)".
Bas
Bas 2016 年 12 月 1 日
Yes, it's the name of a decision variable which will later be used in an optimization problem..
Jan
Jan 2016 年 12 月 9 日
@Bas: Then you use the first element of venu_name as index of the same variable? Strange!

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

質問済み:

Bas
2016 年 12 月 1 日

コメント済み:

Jan
2016 年 12 月 9 日

Community Treasure Hunt

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

Start Hunting!

Translated by