-
Notifications
You must be signed in to change notification settings - Fork 2
/
create_blocking_file.m
67 lines (54 loc) · 2.14 KB
/
create_blocking_file.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
function blk_file_name = create_blocking_file( varargin )
% CREATE_BLOCKING_FILE - create a temporary file to prevent Matlab from
% continuing execution until some programmatically-specified condition is
% met.
%
% Matlab's various commands to pause execution (waitfor, pause, etc.) will not
% wait for the completion of a windows system call (as of Matlab version
% R2011b). This is a somewhat inelegant brute-force workaround for this
% problem. A temporary "blocking" file is created in the directory specified by
% tempname(). The file contains the text "placeholder file to pause Matlab
% execution" and the time the file was created, followed by an optional user
% message (e.g. "this file was create to pause Matlab for XYZ"). The full path
% of the file is returned. If the last command of the Windows system call
% deletes the blocking file, then placing the following code immediately after
% the system call causes Matlab to wait for the system call to complete before
% continuing:
%
% pause on;
% while( exist( blk_fname ) == 2 )
% pause( 5 );
% end
% pause off
%
% The blocking file is named PREFIX_blocking_yyyymmdd_HHMMSS.txt, with PREFIX
% a long random string generated by tempname() and yyyymmdd_HHMMSS the output
% of now() converted to a character string.
%
% USAGE
% blk_file_name = create_blocking_file();
% blk_file_name = create_blocking_file( 'optional message for file' );
%
% INPUTS
% variable: if provided, a character string will be placed in the blocking
% file. Additional arguments are ignored.
%
% OUTPUTS
% blk_file_name: the full path to the blocking file
%
% SEE ALSO
% tempname, now
%
% author: Timothy W. Hilton, UNM, May 2012
p = inputParser();
p.addOptional( 'user_message', '', @ischar );
p.parse( varargin{ : } );
t_string = datestr( now(), 'blocking_yyyymmdd_HHMMSS' );
blk_file_name = sprintf( '%s%s.txt', tempname(), t_string );
fid = fopen( blk_file_name, 'w' );
fprintf( fid, 'placeholder file to pause Matlab execution\r\n' );
fprintf( fid, 'created %s\r\n', datestr( now() ) );
if not( isempty( p.Results.user_message ) )
fprintf( fid, '%s\r\n', p.Results.user_message );
end
fclose( fid );