1function [asc, extra] = readasc(filePathBasic);
2% reads Siemens ASC ascii-formatted textfiles
3% return is a matlab structure with fields from the file(s).
4% usage:
5% myAsc= readasc(path,fileName);
6% [prot, yaps] = readasc(path,fileName);
8% Ralph Strecker 16/02/2005
9% Maxim Zaitsev 08/10/2019
11% starting from NumarisX va60a some files are split in two, like ####.asc
12% and ####_GSWD_SAFETY.asc, therefore we need to attempt to read both parts
13% and merge the structs
14[FILEPATH,NAME,EXT]=fileparts(filePathBasic);
15if exist([FILEPATH filesep NAME '_GSWD_SAFETY' EXT],'file')
16 filePaths={filePathBasic,[FILEPATH filesep NAME '_GSWD_SAFETY' EXT]};
17else
18 filePaths={filePathBasic};
19end
21for fn=1:length(filePaths)
22 %%% read one asc file and convert it into a structure
23 fid= fopen(filePaths{fn});
24 if fid<0
25 error('Failed to open file ''%s'', exiting',filePaths{fn});
26 end
27 endOfAsc=0;
29 %nextLine=fgetl(fid); %read next line
30 %while nextLine~=-1
31 while ~feof(fid)
32 openbrack= [];
33 closebrack= [];
34 nextLine=strtrim(fgetl(fid));
35 if strcmp(nextLine,'### ASCCONV END ###') % find end of mrProt in the asc file
36 endOfAsc=1;
37 end
38 if isempty(nextLine) || nextLine(1)=='#'
39 continue;
40 end
41 indEqualSign= findstr(nextLine,'=');
42 if ~isempty(indEqualSign)
43 fieldName=deblank(nextLine(1:indEqualSign-1));
44 openbrack= findstr(fieldName,'[');
45 closebrack= findstr(fieldName,']');
46 if ~isempty(openbrack) & ~isempty(closebrack)
47 fieldName(openbrack)='(';
48 fieldName(closebrack)=')';
49 % if strcmp(fieldName(end),fieldName(closebrack))
50 % fieldName(closebrack)='}';
51 % fieldName(openbrack)='{';
52 % end
53 for k=1:length(openbrack)
54 counter= str2num(fieldName(openbrack(k)+1:closebrack(k)-1));
55 fieldName= [fieldName(1:openbrack(k)),num2str(counter+1),fieldName(closebrack(k):end)];
56 end
57 end
58 openclosebrack= findstr(fieldName,')(');
59 fieldName(openclosebrack)=[];
60 fieldName(openclosebrack)=',';
61 %if findstr(fieldName,'atImagedNucleus')
62 % fieldName= [fieldName,'.value'];
63 %end
65 fieldValue= deblank(nextLine(indEqualSign+2:end));
66 com=[strfind(fieldValue,'#') strfind(fieldValue,'//')];
67 if ~isempty(com)
68 com=min(com);
69 fieldValue=fieldValue(1:(com-1));
70 end
72 % if findstr(fieldValue,'i0') || findstr(fieldValue,'i1')
73 % ind= findstr(fieldValue,'i');
74 % fieldValue=[fieldValue(1:ind),'*',fieldValue(ind+1:end)];
75 % end
76 if ischar(fieldValue)
77 ind=findstr(fieldValue,'"');
78 %fieldValue(ind)=[];
79 fieldValue(ind)='';
80 end
82 if length(fieldValue)>1 & strcmp(fieldValue(1:2),'0x') & isempty(findstr(fieldName,'atImagedNucleus'))
83 fieldValue= hex2dec(fieldValue(3:end)); %fieldValue is hexadecimal
84 elseif ~isempty(str2num(fieldValue))
85 fieldValue= str2num(fieldValue);
86 end
88 if ischar(fieldValue) && fieldName(end)==')'
89 fieldName(end)='}';
90 ib=max(strfind(fieldName,'('));
91 fieldName(ib)='{';
92 end
94 % fprintf('< %s > ',nextLine);
95 % fprintf('fieldName= %s fieldValue= %s\n', fieldName, mat2str(fieldValue));
97 if endOfAsc==0
98 eval(['asc.' fieldName '=','fieldValue;']);
99 else
100 eval(['extra.' fieldName '=','fieldValue;']);
101 end
102 end
104 end
106 fclose(fid);
107end
109end