1 function LDR =
openldr(FN,PERMISSION,Mode,arg4,arg5,arg6)
2 % OPENLDR loads neuroscan LDR files
3 % LDR = OPENLDR(Filename [, PERMISSION [, Mode]]);
4 % LDR = OPENLDR(LDR [, PERMISSION [, Mode]]);
6 % LDR is a
struct with the following fields
7 % LDR.FileName Name of LDR-file
8 % LDR.Label_Out Labels of output channels
9 % LDR.Label_In Labels of input channels
10 % LDR.RR re-referencing matrix
11 % LDR.datatype
'REREF_MATRIX' indicates
this datatype
13 % PERMISSION
'r' reads LDR file
15 %
'r+w' reads and writes LDR file
16 % (useful in combination with RESCALE-Mode)
18 % Mode [optional]
'RESCALE' performs a rescaling of the weights
19 % sum of positive weights becomes +1
20 % sum of negative weights becomes -1
23 % $Id:
openldr.m 2473 2010-06-04 09:56:57Z schloegl $
24 % Copyright (C) 1997-2003,2008 by Alois Schloegl <a.schloegl@ieee.org>
25 % This is part of the BIOSIG-toolbox http:
27 % This program is free software; you can redistribute it and/or
28 % modify it under the terms of the GNU General Public License
29 % as published by the Free Software Foundation; either version 3
30 % of the License, or (at your option) any later version.
33 if nargin<2, PERMISSION=
''; end;
34 if nargin<3, Mode=
''; end;
38 PERMISSION = [PERMISSION,
'r'];
41 PERMISSION = [PERMISSION,
'w'];
45 if any(PERMISSION==
'r');
47 fid = fopen(LDR.FileName);
48 if fid<0, fprintf(2,
'File %s not found.\n',LDR.FileName);
return; end;
50 % reads line 1: size information
52 if ~ischar(s), fprintf(2,
'ERROR LOADLDR: file %s corrupted\n',FN); end;
55 % reads line 2: output labels
57 if ~ischar(s), fprintf(2,
'ERROR LOADLDR: file %s corrupted\n',FN); end;
58 tmp = reshape(s,12,sz(2)+1)
';
59 LDR.Label_Out = tmp(2:sz(2)+1,:);
61 % read lines 3+: input labels and weights
64 if ~ischar(s), fprintf(2,'ERROR LOADLDR: file %s corrupted\n
',FN); end;
65 r = reshape(s,12,sz(2)+1)';
66 LDR.Label_In(k,1:12) = char(abs(s(1:12)));
67 LDR.RR(k,1:sz(2)) = str2num(r(2:size(r,1),:))
';
71 LDR.datatype='REREF_MATRIX
';
76 if strcmp(Mode,'RESCALE
');
83 RR(:,ix) = tmp(:,ix)./w(ones(sz(1),1),ix);
84 % RR(:,ix) = (tmp(:,ix)>0)./(ones(sz(1),1)*sum(tmp(:,ix)>0));
91 RR(:,ix) = tmp(:,ix)./w(ones(sz(1),1),ix);
92 % RR(:,ix) = RR(:,ix)-(tmp(:,ix)<0)./(ones(sz(1),1)*sum(tmp(:,ix)<0));
97 if any(PERMISSION=='w
');
98 tmp = isfield(LDR,'datatype
') & strcmp(LDR.datatype,'REREF_MATRIX
');
100 warning('LDR.datatype does not fit
');
103 if sz(1)~=size(LDR.Label_In,1);
104 fprintf(2,'Size of Label_In does not fit RR\n
');
107 if sz(2)~=size(LDR.Label_Out,1);
108 fprintf(2,'Size of Label_Out does not fit RR\n
');
113 fid = fopen(LDR.FileName,'w+
');
114 if fid<0, fprintf(2,'Couldnot open file %s .\n
',LDR.FileName); return; end;
116 % write line 1: size information
117 fprintf(fid,'%i %i\n
',sz);
119 % condition label information
120 %LDR.Label_Out = char(LDR.Label_Out);
122 nc = size(LDR.Label_Out,2);
124 LDR.Label_Out = [LDR.Label_Out,abs(' ')*ones(sz(1),12-nc)];
126 LDR.Label_Out = LDR.Label_Out(:,1:12);
128 %LDR.Label_In = char(LDR.Label_In);
129 nc = size(LDR.Label_In,2);
131 LDR.Label_In = [LDR.Label_In,abs(' ')*ones(sz(1),12-nc)];
133 LDR.Label_In = LDR.Label_In(:,1:12);
136 % write line 2: out-labels
137 fwrite(fid,32+zeros(12,1),'uint8
');
138 %fprintf(fid,'%c
',abs(' ')*ones(12,1));
140 fwrite(fid,abs(LDR.Label_Out(k,1:12)),'uint8
');
143 % write lines 3+: in-labels and weights
146 fwrite(fid,abs(LDR.Label_In(k,1:12)),'uint8
');
147 fprintf(fid,'%12.5f
',LDR.RR(k,:));