N=10; fid=fopen('progress.txt','at'); for l=1: fprintf(fid,[datestr(now),' the loop step is %d \n'],l); end fclose(fid);
这样的话,就会在程序里面输出每一个循环已经执行的信息。可以看见输出的txt文件夹具有如下的内容:
1 2 3 4 5 6 7 8 9 10
02-Mar-201923:50:17 the loop step is 1 02-Mar-201923:50:17 the loop step is 2 02-Mar-201923:50:17 the loop step is 3 02-Mar-201923:50:17 the loop step is 4 02-Mar-201923:50:17 the loop step is 5 02-Mar-201923:50:17 the loop step is 6 02-Mar-201923:50:17 the loop step is 7 02-Mar-201923:50:17 the loop step is 8 02-Mar-201923:50:17 the loop step is 9 02-Mar-201923:50:17 the loop step is 10
03-Mar-201900:02:45 the loop step is 10 03-Mar-201900:03:03 the loop step is 4 03-Mar-201900:03:03 the loop step is 7 03-Mar-201900:03:03 the loop step is 1 03-Mar-201900:03:03 the loop step is 3 03-Mar-201900:03:03 the loop step is 6 03-Mar-201900:03:03 the loop step is 5 03-Mar-201900:03:03 the loop step is 2 03-Mar-201900:03:03 the loop step is 8 03-Mar-201900:03:03 the loop step is 10 03-Mar-201900:03:03 the loop step is 9
%每次运行确保文件‘progress_1.txt',‘progress_2.txt'都是空的 N=10; for l=1:N %首先输出一个运行循环的结果到'progress_1.txt'文件 fid=fopen('progress_1.txt','a'); fprintf(fid,[datestr(now),' the loop step is %d \n'],l); fclose(fid); %然后从'progress_1.txt'文件读取文件的字符长度 fid=fopen('progress_1.txt','r'); [A, count]=fscanf(fid,'%s');%count是读取字符的个数,我们每一行都有7个字符,所以count是7的倍数 fclose(fid); fid=fopen('progress_2.txt','a'); %最后在另外一个文件输出我们的总共计算了多少步 fprintf(fid,[datestr(now),' has looped for %d steps \n'],count/7); fclose(fid); end
classdef parfor_progressbar_v1 < handle % PARFOR_PROGRESSBAR Progress bar suitable for multi-process computation. % % H = parfor_progressbar(N, 'message', 'property',value, ...) % creates a graphical progress bar with N iterations before completion. % A temporary file in tempdir is used to communicate among threads. % Any property/value pairs are passed to waitbar internally (optional). % % H.iterate(X) updates the progress bar by X iterations. % % Example: % -------- % N=50; %total number of parfor iterations % hbar = parfor_progressbar(N,'Computing...'); %create the progress bar % parfor i=1:N, % pause(rand); % computation % hbar.iterate(1); % update progress by one iteration % end % close(hbar); %close progress bar % % Notes: % ------ % Properties cannot be modified while inside a parfor loop. Use iterate only. % % With many short iterations, call iterate() periodically. Example: % if mod(i,10)==0, hbar.iterate(10); end % % Inspired by the parfor_progress script made by Jeremy Scheff: % http://www.mathworks.com/matlabcentral/fileexchange/32101 % % See also: WAITBAR, PARFOR.
% Copyright 2015-2016 Cornell University All Rights Reserved.
% Public properties properties (SetAccess=protected, GetAccess=public) wbh; % Waitbar figure object handle N; % Total number of iterations expected before completion Msg; end
properties (Dependent, GetAccess=public) percent; % Percentage of completed iterations end
properties (Dependent, SetAccess=public, GetAccess=public) message; % Message text displayed in waitbar end
% Internal properties properties (SetAccess=protected, GetAccess=protected, Hidden) ipcfile; % Path to temporary file for inter-process communication htimer; % Timer object that checks ipcfile for completed iterations end
methods %======================== CONSTRUCTOR ========================% functionthis = parfor_progressbar_v1(N_init, varargin) % Create a new progress bar with N_init iterations before completion. % Create a unique inter-process communication file. fori=1:10 f = sprintf('%s%d.txt', mfilename, round(rand*1000)); this.ipcfile = fullfile(tempdir, f); if ~exist(this.ipcfile,'file'), break; end end
if exist(this.ipcfile,'file') error('Too many temporary files. Clear out tempdir.'); end % Create a new waitbar this.N = N_init; this.Msg = varargin{1}; this.wbh = waitbar(0, [this.Msg, '0% Complete']); % Create timer to periodically update the waitbar in the GUI thread. this.htimer = timer( 'ExecutionMode','fixedSpacing', 'Period',0.5, ... 'BusyMode','drop', 'Name',mfilename, ... 'TimerFcn',@(x,y)this.tupdate ); start(this.htimer); end %========================= DESTRUCTOR ========================% functiondelete(this) this.close(); end functionclose(this) % Closer the progress bar and clean up internal state. % Stop the timer if isa(this.htimer,'timer') && isvalid(this.htimer) stop(this.htimer); pause(0.01); delete(this.htimer); end this.htimer = []; % Delete the IPC file. if exist(this.ipcfile,'file') delete(this.ipcfile); end % Close the waitbar if ishandle(this.wbh) close(this.wbh); end this.wbh = []; end %====================== GET/SET METHODS ======================% functionpercent = get.percent(this) % Calculate the fraction of completed iterations from IPC file. if ~exist(this.ipcfile, 'file') percent = 0; % File may not exist before the first iteration else fid = fopen( this.ipcfile, 'r' ); percent = sum(fscanf(fid, '%d')) / this.N; percent = max(0, min(1,percent) ); fclose(fid); end end functionset.message(this, newMsg) % Update the progress bar's displayed message. if ishandle(this.wbh) waitbar( this.percent, this.wbh, newMsg ); end end functioniterate(this, Nitr) % Update the progress bar by Nitr iterations (or 1 if not specified). if nargin<2, Nitr = 1; end fid = fopen(this.ipcfile, 'a'); fprintf(fid, '%d\n', Nitr); fclose(fid); end end%public methods
%===================== INTERNAL METHODS =====================% methods (Access=protected, Hidden) functiontupdate(this) % Check the IPC file and update the waitbar with progress. if ishandle(this.wbh) waitbar( this.percent, this.wbh, sprintf('%s...%2.0f%% complete', this.Msg, this.percent*100)); else % Kill the timer if the waitbar is closed. close(this); end end end%private methods
# Call MATLAB with the appropriate input and output. # Be careful to include 'exit' MATLAB command in M-file. # Insert a cd command in the main M-file to change the working directory. # Run the matlab program using runmatlab.sh myfile.m myoutput.txt. # Save workspace.mat.