-
Notifications
You must be signed in to change notification settings - Fork 30
/
addbin.m
69 lines (56 loc) · 1.44 KB
/
addbin.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
65
66
67
68
69
function status=addbin(filename,x)
%ADDBIN add columns to a V4 matfile
% Usage: addbin(filename,x)
% Adds columns of x to data in mat file. Columns are added because
% MATLAB stores data columnwise. Transpose the data to add rows.
% The file should contain only one double precision matrix.
% This is used in MCMCRUN to save and append temporary mcmc
% chains into binary files.
% See also SAVEBIN and READBIN.
% $Revision: 1.3 $ $Date: 2010/12/10 10:47:38 $
m = size(x,1);
n = size(x,2);
fid = fopen(filename,'r+b');
if fid == -1
error(sprintf('error opening binary file %s',filename));
end
if fseek(fid,4,'bof') == -1
fclose(fid);
error('error seeking file');
end
mrows = fread(fid,1,'integer*4');
mcols = fread(fid,1,'integer*4');
imagf = fread(fid,1,'integer*4');
namelen = fread(fid,1,'integer*4');
if mrows > 0 & mrows ~= m
error('x should have same number of rows');
end
% goto begining of x
if fseek(fid,namelen,'cof') == -1
fclose(fid);
error('error seeking file');
end
if fseek(fid,0,'eof') == -1
fclose(fid);
error('error seeking eof');
end
% write the new data to file
fwrite(fid,x,'real*8');
% fix mrows
if mrows == 0
if fseek(fid,4,'bof') == -1
fclose(fid);
error('error seeking file');
end
fwrite(fid,m,'integer*4');
end
% write the new column size
if fseek(fid,8,'bof') == -1
fclose(fid);
error('error seeking file');
end
fwrite(fid,mcols+n,'integer*4');
fclose(fid);
if nargout > 0
status = 1;
end