source: an2_saveres.m @ 63

Last change on this file since 63 was 53, checked in by tjniskan, 11 years ago
  • Fixed a bug in an2_readfid.m that in some cases didn't calculate

the array length in procpar correctly. Also changed the handling of
array parameter in an2_readprocpar.m.

  • Fixed a bug in an2_rot3d.m
  • Fixed bugs here and there
  • Added some incomplete code for upcoming new features.

M misclib/nifti4dto3d.m
M misclib/fmri_corr.m
M an2_rot3d.m
M an2_revision.m
M an2_readprocpar.m
M an2_data_read.m
M aedes.m
M an2_res2table.m
M an2_copy_roi.m
M an2_roi_copy_gui.m
M an2_readfid.m
M an2_saveres.m
M an2_write_nifti.m

File size: 7.0 KB
Line 
1function [done,msg] = an2_saveres(DATA,ROI,filename,varargin)
2% AN2_SAVERES - Save Aedes results (statistics and ROIs)
3%   
4%
5% Synopsis:
6%
7% Description:
8%
9% Examples:
10%
11% See also:
12%
13
14% This function is a part of Aedes - A graphical tool for analyzing
15% medical images
16%
17% Copyright (C) 2006 Juha-Pekka Niskanen <Juha-Pekka.Niskanen@uku.fi>
18%
19% Department of Physics, Department of Neurobiology
20% University of Kuopio, FINLAND
21%
22% This program may be used under the terms of the GNU General Public
23% License version 2.0 as published by the Free Software Foundation
24% and appearing in the file LICENSE.TXT included in the packaging of
25% this program.
26%
27% This program is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28% WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29
30
31done=false;
32msg='';
33
34%% Default values for properties
35SaveType = 'all';      % Save ROIs and statistics by default
36FileName = 'Untitled'; % Filename(s) Untitled.res and Untitled.roi
37FilePath = pwd;        % Use working directory by default
38ShowWbar = true;       % Show waitbar by default
39RotateFlip = [];
40
41
42%% Parse input arguments
43if nargin<3
44  msg = 'Too few input arguments';
45  return
46end
47
48%% Parse varargin
49for ii=1:2:length(varargin)
50  switch lower(varargin{ii})
51   case {'wbar','waitbar'}
52    if ischar(varargin{ii+1})
53      if strcmpi(varargin{ii+1},'on')
54        ShowWbar = true;
55      else
56        ShowWbar = false;
57      end
58    else
59      if varargin{ii+1}==1
60        ShowWbar = true;
61      else
62        ShowWbar = false;
63      end
64    end
65   case 'savetype'
66    SaveType = varargin{ii+1};
67   
68   case 'rotateflip'
69    RotateFlip = varargin{ii+1};
70   
71   otherwise
72    done=false;
73    msg=sprintf('Unknown parameter "%s"!',varargin{ii});
74    return
75  end
76end
77
78[fp,fn,fe]=fileparts(filename);
79if any(strcmpi(fe,{'.roi','.res'}))
80  FileName = fn;
81else
82  FileName = [fn,fe];
83end
84if ~isempty(fp)
85  FilePath = [fp,filesep];
86else
87  FilePath = [pwd,filesep];
88end
89
90if isstruct(DATA)
91  DATA = {DATA};
92elseif ~iscell(DATA)
93  msg='Input argument "DATA" is not valid';
94  return
95end
96
97if isempty(ROI) || ~isstruct(ROI)
98  msg='Input argument "ROI" is not valid';
99  return
100end
101
102%% Save results and/or ROIs
103switch SaveType
104 case 'all' % Save both ROI and RES --------------------
105 
106  %% Check if files to be written already exist
107  tmp=dir(FilePath);
108  fnames = {tmp(~[tmp(:).isdir]).name};
109  if any(strcmpi(fnames,[FileName,'.roi']))
110    RoiFileExists = true;
111  else
112    RoiFileExists = false;
113  end
114  if any(strcmpi(fnames,[FileName,'.res']))
115    ResFileExists = true;
116  else
117    ResFileExists = false;
118  end
119 
120  if ResFileExists
121    resp=questdlg({['"',FilePath,FileName,'.res" already exists.'],...
122                  'Overwrite?'},'Overwrite File?',...
123                  'Yes','No','No');
124    if strcmpi(resp,'No')
125      msg='Overwrite cancel';
126      return
127    end
128  end
129  if RoiFileExists
130    resp=questdlg({['"',FilePath,FileName,'.roi" already exists.'],...
131                  'Overwrite?'},'Overwrite File?',...
132                  'Yes','No','No');
133    if strcmpi(resp,'No')
134      msg='Overwrite cancel';
135      return
136    end
137  end
138 
139  if ShowWbar
140    [h,txh]=an2_calc_wait({'Saving ROI(s)...',...
141                      ['(',FilePath,FileName,'.roi)']});
142  end
143 
144  [ok,msg]=l_SaveRoi(DATA,ROI,FileName,FilePath,RotateFlip);
145  if ~ok
146    if ShowWbar
147      delete(h)
148    end
149    return
150  end
151  set(txh,'string',{'Saving ROI Statistics...',...
152                   ['(',FilePath,FileName,'.res)']})
153  [ok,msg]=l_SaveRes(DATA,ROI,FileName,FilePath);
154  if ~ok
155    if ShowWbar
156      delete(h)
157    end
158    return
159  end
160  if ShowWbar
161    delete(h)
162  end
163 
164 case 'roi' % Save only ROI ----------------------------
165 
166  %% Check if files to be written already exist
167  tmp=dir(FilePath);
168  fnames = {tmp(~[tmp(:).isdir]).name};
169  if any(strcmpi(fnames,[FileName,'.roi']))
170    RoiFileExists = true;
171  else
172    RoiFileExists = false;
173  end
174 
175  if RoiFileExists
176    resp=questdlg({['"',FilePath,FileName,'.roi" already exists.'],...
177                  'Overwrite?'},'Overwrite File?',...
178                  'Yes','No','No');
179    if strcmpi(resp,'No')
180      msg='Overwrite cancel';
181      return
182    end
183  end
184 
185  if ShowWbar
186    [h,txh]=an2_calc_wait({'Saving ROI(s)...',...
187                      ['(',FilePath,FileName,'.roi)']});
188  end
189 
190  [ok,msg]=l_SaveRoi(DATA,ROI,FileName,FilePath,RotateFlip);
191  if ~ok
192    if ShowWbar
193      delete(h)
194    end
195    return
196  end
197  if ShowWbar
198    delete(h)
199  end
200 
201 case 'res' % Save only RES ----------------------------
202 
203  %% Check if files to be written already exist
204  tmp=dir(FilePath);
205  fnames = {tmp(~[tmp(:).isdir]).name};
206  if any(strcmpi(fnames,[FileName,'.res']))
207    ResFileExists = true;
208  else
209    ResFileExists = false;
210  end
211 
212  if ResFileExists
213    resp=questdlg({['"',FilePath,FileName,'.res" already exists.'],...
214                  'Overwrite?'},'Overwrite File?',...
215                  'Yes','No','No');
216    if strcmpi(resp,'No')
217      msg='Overwrite cancel';
218      return
219    end
220  end
221 
222  if ShowWbar
223    [h,txh]=an2_calc_wait({'Saving ROI Statistics...',...
224                       ['(',FilePath,FileName,'.res)']});
225  end 
226 
227  [ok,msg]=l_SaveRes(DATA,ROI,FileName,FilePath);
228  if ~ok
229    if ShowWbar
230      delete(h)
231    end
232    return
233  end
234  if ShowWbar
235    delete(h)
236  end
237 
238 otherwise
239  msg='Unknown save type';
240  return
241end
242
243% No problems if this point is reached...
244done=true;
245return
246
247
248%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
249% Save ROIs
250%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
251function [ok,msg]=l_SaveRoi(DATA,ROI,FileName,FilePath,rotflip)
252
253ok = false;
254msg='';
255
256% Generate timestamp
257DateTime = datestr(now);
258
259% Append file information to the save ROI file
260for ii=1:length(DATA)
261  FileInfo.DataFileName{ii} = DATA{ii}.HDR.fname;
262  FileInfo.DataPathName{ii} = DATA{ii}.HDR.fpath;
263end
264
265% Append rotate and flip information
266if iscell(rotflip)
267  RotateFlip = rotflip;
268else
269  RotateFlip.Rotate = zeros(1,length(DATA));
270  RotateFlip.Flip = zeros(1,length(DATA));
271  if ~isempty(rotflip)
272        RotateFlip.Rotate = rotflip(1,:);
273        RotateFlip.Flip = rotflip(2,:);
274  end
275end
276
277% Save ROI(s)
278try
279  save([FilePath,FileName,'.roi'],'ROI','DateTime','FileInfo','RotateFlip','-mat')
280catch
281  msg={'Could not save ROI(s). Following error was returned:',lasterr};
282  return
283end
284
285% All went well
286ok=true;
287
288
289%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
290% Save RES
291%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
292function [ok,msg]=l_SaveRes(DATA,ROI,FileName,FilePath)
293
294ok = false;
295msg = '';
296
297
298try
299  Res = [];
300  % Calculate results
301  Res = an2_roi_stats(DATA,ROI);
302  if isempty(Res)
303    msg = 'Error while calculating statistics. Could not save results.';
304    return
305  end
306 
307  % Generate timestamp
308  %DateTime = datestr(now);
309 
310  % Generate file info
311  %for ii=1:length(DATA)
312  %  FileInfo.DataFileName{ii} = DATA{ii}.HDR.fname;
313  %  FileInfo.DataPathName{ii} = DATA{ii}.HDR.fpath;
314  %end
315  %Res.DateTime = DateTime;
316  %Res.FileInfo = FileInfo;
317  %Res.Stat = Stat;
318 
319  % Save results
320  save([FilePath,FileName,'.res'],'Res','-mat')
321catch
322  msg = {'Could not save results. Following error was returned:', ...
323         lasterr};
324  return
325end
326
327% All went well
328ok = true;
Note: See TracBrowser for help on using the repository browser.

Powered by Trac 1.0.9.Copyright © Juha-Pekka Niskanen 2008