source: trunk/src/uvmat.m @ 408

Last change on this file since 408 was 408, checked in by sommeria, 13 years ago

various bugs corrected in series and uvmat.

File size: 198.5 KB
Line 
1%'uvmat': function associated with the GUI 'uvmat.fig' for images and data field visualization
2%------------------------------------------------------------------------
3% function huvmat=uvmat(input)
4%
5%OUTPUT
6% huvmat=current handles of the GUI uvmat.fig
7%%
8%
9%INPUT:
10% input: input file name (if character chain), or input image matrix to
11% visualize, or Matlab structure representing  netcdf fields (with fields
12% ListVarName....)
13%
14%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
15%  Copyright Joel Sommeria,  2008, LEGI / CNRS-UJF-INPG, joel.sommeria@legi.grenoble-inp.fr.
16%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
17%     This open is part of the toolbox UVMAT.
18%
19%     UVMAT is free software; you can redistribute it and/or modify
20%     it under the terms of the GNU General Public License as published by
21%     the Free Software Foundation; either version 2 of the License, or
22%     (at your option) any later version.
23%
24%     UVMAT is distributed in the hope that it will be useful,
25%     but WITHOUT ANY WARRANTY; without even the implied warranty of
26%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27%     GNU General Public License (open UVMAT/COPYING.txt) for more details.
28%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
29%
30% Information stored on the interface:
31%    'Strings' of all edit boxes and menus: get(handles.Tag,'String')
32%    'Values' of all menus and toggle buttons: get(handles.Tag,'Value')
33%     Matlab structure called UvData stored as 'UserData' of the figure uvmat.fig,(can be obtained by right mouse click on the interface).
34%          It contains the following fields:
35%     - Fixed specifiacation of plotting figures and axes (defined bu uvmat_OpeningFcn)
36%          .PosColorbar: [0.8210 0.4710 0.0190 0.4450]; specified position of the colorbar on figures
37%     - Information read in the documentation open of a series (activated by RootPath_Callback) :
38%          .XmlData, with fields:
39%               .Time: matrix of times of the images with index i and j
40%               .GeometryCalib: [1x1 struct]
41%     - Information defined from the interface:
42%           .NewSeries: =1 when the first view of a new field series is displayed, else 0
43%           .filename:(char string)
44%           .FieldName: (char string) main field selected('image', 'velocity'...)
45%           .CName: (char string)name of the scalar used for vector colors
46%          .MovieObject{1}: movie object representing an input movie
47%          .MovieObject{2}: idem for a second input series (_1)
48%          .filename_1 : last second input file name (to deal with a constant second input without reading again the file)
49%          .ZMin, .ZMax: range of the z coordinate
50%..... to complement
51%     - Information on  projection objects
52%           .Object: {[1x1 struct]}
53%           .CurrentObjectIndex: index of the projection object .Object currently selected for editing
54%     -Information on the current field (Field{i})
55%            .Txt : text information to display (e.g. error message)
56%            .NbDim: number of dimensions (=0 by default)
57%            .NbCoord: number of vector components
58%            .CoordType: expresses the type of coordinate ('px' for image, 'sig' for instruments, or 'phys')
59%            .dt: time interval for the corresponding image pair
60%            .Mesh: estimated typical distance between vectors
61%            .ZMax:
62%            .ZMin:
63%            .X, .Y, .Z: set of vector coordinates
64%            .U,.V,.W: corresponding set of vector components
65%            .F: corresponding set of warning flags
66%            .FF: corresponding set of false flags, =0 for good vectors
67%            .C: corresponding values of the scalar used for vector color
68%             (.X, .Y, .Z,.U,.V,.W,.F,.FF,.C are matlab vectors of the same length,
69%                     equal to the number of vectors stored in the input open)
70%            .CName: name of the scalar .C
71%            .CType: type of the scalar .C, setting how the scalar is obtained (see 'Scalars' below)
72%            .A image or scalar
73%            .AX: vector of dimension 2 representing the first and last values
74%              of the X coordinates for the image or scalar known on a regular grid,
75%              or vector of dimension .A for a scaler defined on irregular grid.
76%            .AY: same as .AX along the Y direction
77%            .AName: name of the scalar, ='image' for an image
78
79% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   DATA FLOW  (for run0_Callback) %%%%%%%%%%%%%%%%%%%%:
80%
81%
82% 1) Input filenames are determined by MenuBrowse (first field), MenuBrowse_1
83% (second field), or by the stored file names under  Browse, or as an input of uvmat.
84% 2) These functions call 'display_file_name.m' which detects the file series, and fills the file index boxes
85% 3) Then 'update_rootinfo.m' Updates information about a new field series (indices to scan, timing, calibration from an xml file)
86% 4) Then fields are opened and visualised by the main sub-function 'refresh_field.m'
87% The function first reads the name of the input file from the edit boxes  of the GUI
88% A second input file can be introduced for file comparison
89% It then reads the input file(s) with the appropriate function, read for
90% images, read_civxdata.m for CIVx PIV data, nc2struct for other netcdf
91% files.
92%               Main input open   second input open(_1)        second image (pair animation)
93%            |                 |                             
94%            |                 |                                               
95%                     Field{1}         Field{2}               
96%                                                                         |
97% coord transform (phys.m) or other user defined  fct acting on Field{i}  |
98%                                                                   Field{i}
99%                                                                    |
100% calc_field.m: calculate scalar or other derived fields (vort, div..).
101%
102% sub_field.m: combine the input Field{i} in a single set of fields (vector + scalar):
103%              Field{i=1->3}.X --> UvData.X                          |
104%                                                                    |
105%                                                                 UvData
106%                                                                    |
107% plot histograms of the whole  field
108% proj_field.m: project the set of fields on the current projection objects defined by UvData.Object
109%                                                                    |                                                                          |
110%                                                                ObjectData
111%                                                                    |
112% plot_field.m: plot the projected fields and store them as          |
113% UvData.axes3                                        |
114%                                                                    |
115%                                                                AxeData
116%
117%%%%%%%%%%%%%%    SCALARS: %%%%%%%%%%%%??%%%
118% scalars are displayed either as an image or countour plot, either as a color of
119% velocity vectors. The scalar values in the first case is represented by
120% UvData.Field.A, and by UvData.Field.C in the second case. The corresponding set of X
121% and Y coordinates are represented by UvData.Field.AX and UvData.Field.AY, and .X and
122% .Y for C (the same as velocity vectors). If A is a nxxny matrix (scalar
123% on a regtular grid), then .AX andf.AY contains only two elements, represneting the
124% coordinates of the four image corners. The scalar name is represented by
125% the strings .AName and/or .CName.
126% If the scalar exists in an input open (image or scalar stored under its
127% name in a netcdf open), it is directly read at the level of Field{1}or Field{2}.
128% Else only its name AName is recorded in Field{i}, and its field is then calculated
129%by the fuction calc_scal after the coordinate transform or after projection on an edit_object
130     
131% Properties attached to plotting figures (standard Matlab properties):
132%    'CurrentAxes'= gca or get(gcf,'CurrentAxes');
133%    'CurrentPoint'=get(gcf,'CurrentPoint'): figure coordinates of the point over which the mouse is positioned
134%    'CurrentCharacter'=get(gcf,'CurrentCharacter'): last character typed  over the figure where the mouse is positioned
135%    'WindowButtonMotionFcn': function permanently called by mouse motion over the figure
136%    'KeyPressFcn': function called by pressing a key on the key board
137%    'WindowButtonDownFcn':  function called by pressing the mouse over the  figure
138%    'WindowButtonUpFcn': function called by releasing  the mouse pressure over the  figure
139
140% Properties attached to plotting axes:
141%    'CurrentPoint'=get(gca,'CurrentPoint'); (standard Matlab) same as for the figure, but position in plot coordinates.
142%     AxeData:=get(gca,'UserData');
143%     AxeData.Drawing  = create: create a new object
144%                       = deform: modify an existing object by moving its defining create
145%                      = off: no current drawing action
146%                     = translate: translate an existing object
147%                    = calibration: move a calibration point
148%                    = CheckZoom: isolate a subregion for CheckZoom in=1 if an object is being currently drawn, 0 else (set to 0 by releasing mouse button)
149%            .CurrentOrigin: Origin of a curently drawn edit_object
150%            .CurrentLine: currently drawn menuline (A REVOIR)
151%            .CurrentObject: handle of the currently drawn edit_object
152%            .CurrentRectZoom: current rectangle used for CheckZoom
153
154% Properties attached to projection objects (create, menuline, menuplane...):
155%    'Tag'='proj_object': for all projection objects
156%    ObjectData.Type=...: style of projection object:
157%              .ProjMode
158%              .Coord: defines the position of the object
159%              .XMin,YMin....
160%              .XMax,YMax....
161%              .DX,DY,DZ
162%              .Phi, .Theta, .Psi : Euler angles
163%              .X,.Y,.U,.V.... : field data projected on the object
164%              .IndexObj: index in the list of UvData.Object
165           %during plotting
166%               .plotaxes: handles of the current axes used to plot the  result of field projection on the object
167%               .plothandle: vector of handle(s) of the object graphic represnetation in all the opened plotting axes
168% To each projection object #iobj, corresponds an axis
169% Object{iobj}.plotaxes and nbobj representation graphs  Object{iobj}.plothandles(:) (where nbobj is the
170% nbre of current objects opened in uvmat. Note that Object{iobj}.plothandles(iobj)=[] : an object is not represented in its own projection field;
171
172%------------------------------------------------------------------------
173%------------------------------------------------------------------------
174%  I - MAIN FUNCTION uvmat
175%------------------------------------------------------------------------
176%------------------------------------------------------------------------
177function varargout = uvmat(varargin)
178
179% Begin initialization code - DO NOT EDIT
180gui_Singleton = 1;
181gui_State = struct('gui_Name',          mfilename, ...
182                   'gui_Singleton',     gui_Singleton, ...
183                   'gui_OpeningFcn',    @uvmat_OpeningFcn, ...
184                   'gui_OutputFcn',     @uvmat_OutputFcn, ...
185                   'gui_LayoutFcn',     [], ...
186                   'gui_Callback',      []);
187if nargin && ischar(varargin{1})&& ~isempty(regexp(varargin{1},'_Callback','once'))
188    gui_State.gui_Callback = str2func(varargin{1});
189end
190
191if nargout
192    varargout{1:nargout} = gui_mainfcn(gui_State, varargin{:});
193else
194    gui_mainfcn(gui_State, varargin{:});
195end
196% End initialization code - DO NOT EDIT
197
198%------------------------------------------------------------------------
199% --- Executes just before the GUI uvmat is made visible.
200function uvmat_OpeningFcn(hObject, eventdata, handles, input )
201%------------------------------------------------------------------------
202
203%% Choose default command menuline output for uvmat (standard GUI)
204handles.output = hObject;
205
206%% Update handles structure (standard GUI)
207guidata(hObject, handles);
208
209%% check the path and date of modification of all functions in uvmat
210path_to_uvmat=which ('uvmat');% check the path detected for source file uvmat
211[errormsg,date_str,svn_info]=check_files;%check the path of the functions called by uvmat.m
212date_str=['last modification: ' date_str];
213
214
215%% set the position of colorbar and ancillary GUIs:
216set(hObject,'Units','Normalized')
217movegui(hObject,'center')
218UvData.OpenParam.PosColorbar=[0.805 0.022 0.019 0.445];
219UvData.OpenParam.SetObjectOrigin=[-0.05 -0.03]; %position for set_object
220UvData.OpenParam.SetObjectSize=[0.3 0.7];
221UvData.OpenParam.CalOrigin=[0.95 -0.03];%position for geometry_calib (TO IMPROVE)
222UvData.OpenParam.CalSize=[0.28 1];
223UvData.axes3=[];%initiate the record of plotted field
224UvData.axes2=[];
225UvData.axes1=[];
226AxeData.LimEditBox=1; %initialise AxeData
227set(handles.axes3,'UserData',AxeData)
228
229%% set functions for the mouse and keyboard
230set(handles.histo_u,'NextPlot','replacechildren');
231set(handles.histo_v,'NextPlot','replacechildren');
232set(hObject,'KeyPressFcn',{'keyboard_callback',handles})%set keyboard action function
233set(hObject,'WindowButtonMotionFcn',{'mouse_motion',handles})%set mouse action functio
234set(hObject,'WindowButtonDownFcn',{'mouse_down'})%set mouse click action function
235set(hObject,'WindowButtonUpFcn',{'mouse_up',handles})
236set(hObject,'DeleteFcn',{@closefcn})%
237
238%% refresh projection plane
239UvData.Object{1}.ProjMode='projection';%main plotting plane
240set(handles.ListObject,'Value',1)% default: empty projection objectproj_field
241set(handles.ListObject,'String',{''})
242set(handles.Fields,'Value',1)
243set(handles.Fields,'string',{''})
244
245%% TRANSFORM menu: builtin fcts
246menu_str={'';'phys';'px';'phys_polar'};
247UvData.OpenParam.NbBuiltin=numel(menu_str); %number of functions
248path_uvmat=fileparts(which('uvmat'));
249addpath (path_uvmat) ; %add the path to UVMAT, (useful in case of change of working directory after civ has been s opened in the working directory)
250addpath(fullfile(path_uvmat,'transform_field'))%add the path to transform functions,
251fct_handle{1,1}=[];
252testexist=zeros(size(menu_str'));%default
253testexist(1)=1;
254for ilist=2:length(menu_str)
255    if exist(menu_str{ilist},'file')
256        fct_handle{ilist,1}=str2func(menu_str{ilist});
257        testexist(ilist)=1;
258    else
259        testexist(ilist)=0;
260    end
261end
262rmpath(fullfile(path_uvmat,'transform_field'))
263
264%% load the list of previously browsed files in menus Open and Open_1
265 dir_perso=prefdir; % path to the directory .matlab for personal data
266 profil_perso=fullfile(dir_perso,'uvmat_perso.mat');% personal data file uvmauvmat_perso.mat' in .matlab
267 if exist(profil_perso,'file')
268      h=load (profil_perso);
269      if isfield(h,'MenuFile')
270          for ifile=1:min(length(h.MenuFile),5)
271              eval(['set(handles.MenuFile_' num2str(ifile) ',''Label'',h.MenuFile{ifile});'])
272               eval(['set(handles.MenuFile_' num2str(ifile) '_1,''Label'',h.MenuFile{ifile});'])
273          end
274      end
275      if isfield(h,'transform_fct') && iscell(h.transform_fct)
276         for ilist=1:length(h.transform_fct);
277             if exist(h.transform_fct{ilist},'file')
278                [path,file]=fileparts(h.transform_fct{ilist});
279                addpath(path)
280                h_func=str2func(file);
281                rmpath(path)
282                testexist=[testexist 1];
283             else
284                file='';
285                h_func=[];
286                testexist=[testexist 0];
287             end
288             fct_handle=[fct_handle; {h_func}]; %concatene the list of paths
289             menu_str=[menu_str; {file}];
290         end
291      end
292 end
293menu_str=menu_str(testexist==1);%=menu_str(testexist~=0)
294fct_handle=fct_handle(testexist==1);
295menu_str=[menu_str;{'more...'}];
296set(handles.transform_fct,'String',menu_str)
297set(handles.transform_fct,'UserData',fct_handle)% store the list of path in UserData of ACTION
298
299%% case of an input argument for uvmat
300testinputfield=0;
301inputfile=[];
302Field=[];
303if exist('input','var')
304%     if ~isempty(errormsg)
305%         msgbox_uvmat('WARNING',errormsg)
306%     end
307    if ishandle(handles.UVMAT_title)
308        delete(handles.UVMAT_title)
309    end   
310    if isstruct(input)
311        if isfield(input,'InputFile')
312            inputfile=input.InputFile;
313        end
314        if isfield(input,'TimeIndex')
315            set(handles.i1,num2str(input.TimeIndex))
316        end
317        if isfield(input,'FieldsString')
318            UvData.FieldsString=input.FieldsString;
319        end
320    elseif ischar(input)% file name introduced as input
321           inputfile=input;
322    elseif isnumeric(input)%simple matrix introduced as input
323        sizinput=size(input);
324        if sizinput(1)<=1 || sizinput(2)<=1
325            msgbox_uvmat('ERROR','bad input for uvmat: file name, structure or numerical matrix accepted')
326            return
327        end
328        UvData.Field.ListVarName={'A','coord_y','coord_x'};
329        UvData.Field.VarDimName={{'coord_y','coord_x'},'cord_y','coord_x'};
330        UvData.Field.A=input;
331        UvData.Field.coord_x=[0.5 size(input,2)-0.5];
332        UvData.Field.coord_y=[size(input,1)-0.5 0.5];
333        testinputfield=1;
334    end
335else
336   if ishandle(handles.UVMAT_title)
337       set(handles.UVMAT_title,'String',...
338           [{'Copyright  LEGI UMR 5519 /CNRS-UJF-Grenoble INP, 2010'};...
339           {'GNU General Public License'};...
340           {path_to_uvmat};...
341           {date_str};...
342           {['SVN revision : ' num2str(svn_info.cur_rev)]};...
343           errormsg]);
344   end
345end
346set(handles.uvmat,'UserData',UvData)
347if ~isempty(inputfile)
348    %%%%% display the input field %%%%%%%
349    display_file_name(handles,inputfile)
350    %%%%%%%
351    testinputfield=1;
352end
353
354%% plot input field if exists
355if testinputfield
356    %delete drawn objects
357    hother=findobj(handles.axes3,'Tag','proj_object');%find all the proj objects
358    for iobj=1:length(hother)
359        delete_object(hother(iobj))
360    end 
361    if isempty(inputfile)
362        errormsg=refresh_field(handles,[],[],[],[],[],[],{Field});
363        set(handles.MenuTools,'Enable','on')
364        set(handles.OBJECT_txt,'Visible','on')
365        set(handles.edit_object,'Visible','on')
366        set(handles.ListObject,'Visible','on')
367        set(handles.frame_object,'Visible','on')
368        if ~isempty(errormsg)
369            msgbox_uvmat('ERROR',errormsg)
370        end
371    end
372end
373
374set_vec_col_bar(handles) %update the display of color code for vectors
375
376%------------------------------------------------------------------------
377% --- Outputs from this function are returned to the command menuline.
378function varargout = uvmat_OutputFcn(hObject, eventdata, handles)
379varargout{1} = handles.output;% the only output argument is the handle to the GUI figure
380
381%------------------------------------------------------------------------
382% --- executed when closing uvmat: delete or desactivate the associated figures if exist
383function closefcn(gcbo,eventdata)
384%------------------------------------------------------------------------
385hh=findobj(allchild(0),'tag','view_field');
386if ~isempty(hh)
387    delete(hh)
388end
389hh=findobj(allchild(0),'tag','geometry_calib');
390if ~isempty(hh)
391    delete(hh)
392end
393hh=findobj(allchild(0),'tag','set_object');
394if ~isempty(hh)
395    hhh=findobj(hh,'tag','PLOT');
396    set(hhh,'enable','off')
397end
398
399%------------------------------------------------------------------------
400%------------------------------------------------------------------------
401%  II - FUNCTIONS FOR INTRODUCING THE INPUT FILES
402% automatically sets the global properties when the rootfile name is introduced
403% then activate the view-field action if selected
404% it is activated either by clicking on the RootPath window or by the
405% browser
406%------------------------------------------------------------------------
407%------------------------------------------------------------------------
408% --- Executes on the menu Open/Browse...
409% search the files, recognize their type according to their name and fill the rootfile input windows
410function MenuBrowse_Callback(hObject, eventdata, handles)
411[RootPath,SubDir,RootFile,FileIndices,FileExt]=read_file_boxes(handles);
412oldfile=[fullfile(RootPath,SubDir,RootFile) FileIndices FileExt];
413% oldfile=read_file_box,es(handles);
414if isempty(oldfile)||isequal(oldfile,'') %loads the previously stored file name and set it as default in the file_input box
415         dir_perso=prefdir;
416         profil_perso=fullfile(dir_perso,'uvmat_perso.mat');
417         if exist(profil_perso,'file')
418              h=load (profil_perso);
419             if isfield(h,'MenuFile_1')
420                  oldfile=h.MenuFile_1;
421             end
422         end
423end
424[FileName, PathName] = uigetfile( ...
425       {'*.xml;*.xls;*.civ;*.png;*.jpg;*.tif;*.avi;*.AVI;*.vol;*.nc;*.cmx;*.fig;*.log;*.dat;*.bat;', ' (*.xml,*.xls,*.civ,*.jpg ,*.png, .tif, *.avi,*.vol,*.nc,*.cmx,*.fig,*.log,*.dat,*.bat)';
426       '*.xml',  '.xml files '; ...
427        '*.xls',  '.xls files '; ...
428        '*.civ',  '.civ files '; ...
429        '*.jpg',' jpeg image files'; ...
430        '*.png','.png image files'; ...
431        '*.tif','.tif image files'; ...
432        '*.avi;*.AVI','.avi movie files'; ...
433        '*.vol','.volume images (png)'; ...
434        '*.nc','.netcdf files'; ...
435        '*.cdf','.netcdf files'; ...
436        '*.cmx','.cmx text files ';...
437        '*.fig','.fig files (matlab fig)';...
438        '*.log','.log text files ';...
439        '*.dat','.dat text files ';...
440        '*.bat','.bat system command text files';...
441        '*.*',  'All Files (*.*)'}, ...
442        'Pick a file',oldfile);
443fileinput=[PathName FileName];%complete file name
444sizf=size(fileinput);
445if (~ischar(fileinput)||~isequal(sizf(1),1)),return;end
446
447% display the selected field and related information
448display_file_name( handles,fileinput)
449
450% -----------------------------------------------------------------------
451% --- Open again the file whose name has been recorded in MenuFile_1
452function MenuFile_1_Callback(hObject, eventdata, handles)
453%------------------------------------------------------------------------
454fileinput=get(handles.MenuFile_1,'Label');
455display_file_name( handles,fileinput)
456
457% -----------------------------------------------------------------------
458% --- Open again the file whose name has been recorded in MenuFile_2
459function MenuFile_2_Callback(hObject, eventdata, handles)
460%------------------------------------------------------------------------
461fileinput=get(handles.MenuFile_2,'Label');
462display_file_name(handles,fileinput)
463
464% -----------------------------------------------------------------------
465% --- Open again the file whose name has been recorded in MenuFile_3
466function MenuFile_3_Callback(hObject, eventdata, handles)
467%------------------------------------------------------------------------
468fileinput=get(handles.MenuFile_3,'Label');
469display_file_name(handles,fileinput)
470
471% -----------------------------------------------------------------------
472% --- Open again the file whose name has been recorded in MenuFile_4
473function MenuFile_4_Callback(hObject, eventdata, handles)
474%------------------------------------------------------------------------
475fileinput=get(handles.MenuFile_4,'Label');
476display_file_name(handles,fileinput)
477
478% -----------------------------------------------------------------------
479% --- Open again the file whose name has been recorded in MenuFile_5
480function MenuFile_5_Callback(hObject, eventdata, handles)
481%------------------------------------------------------------------------
482fileinput=get(handles.MenuFile_5,'Label');
483display_file_name(handles,fileinput)
484
485%------------------------------------------------------------------------
486% --- Executes on the menu Open/Browse_1 for the second input field,
487%     search the files, recognize their type according to their name and fill the rootfile input windows
488function MenuBrowse_1_Callback(hObject, eventdata, handles)
489%------------------------------------------------------------------------
490% huvmat=get(handles.run0,'parent');
491UvData=get(handles.uvmat,'UserData');
492
493RootPath=get(handles.RootPath,'String');
494[FileName, PathName, filterindex] = uigetfile( ...
495       {'*.xml;*.xls;*.civ;*.jpg;*.png;*.avi;*.AVI;*.nc;*.cmx;*.fig;*.log;*.dat', ' (*.xml,*.xls,*.civ, *.jpg,*.png, *.avi,*.nc,*.cmx ,*.fig,*.log,*.dat)';
496       '*.xml',  '.xml files '; ...
497        '*.xls',  '.xls files '; ...
498        '*.civ',  '.civ files '; ...
499        '*.jpg','.jpg image files'; ...
500        '*.png','.png image files'; ...
501        '*.avi;*.AVI','.avi movie files'; ...
502        '*.nc','.netcdf files'; ...
503        '*.cdf','.netcdf files'; ...
504        '*.cmx','.cmx text files';...
505        '*.cmx2','.cmx2 text files';...
506        '*.fig','.fig files (matlab fig)';...
507        '*.log','.log text files ';...
508        '*.dat','.dat text files ';...
509        '*.*',  'All Files (*.*)'}, ...
510        'Pick a second file for comparison',RootPath);
511fileinput_1=[PathName FileName];%complete file name
512sizf=size(fileinput_1);
513if (~ischar(fileinput_1)||~isequal(sizf(1),1)),return;end
514
515% refresh the current displayed field
516set(handles.SubField,'Value',1)
517display_file_name(handles,fileinput_1,2)
518
519%update list of recent files in the menubar
520MenuFile_1=fileinput_1;
521MenuFile_2=get(handles.MenuFile_1,'Label');
522MenuFile_3=get(handles.MenuFile_2,'Label');
523MenuFile_4=get(handles.MenuFile_3,'Label');
524MenuFile_5=get(handles.MenuFile_4,'Label');
525set(handles.MenuFile_1,'Label',MenuFile_1)
526set(handles.MenuFile_2,'Label',MenuFile_2)
527set(handles.MenuFile_3,'Label',MenuFile_3)
528set(handles.MenuFile_4,'Label',MenuFile_4)
529set(handles.MenuFile_5,'Label',MenuFile_5)
530set(handles.MenuFile_1_1,'Label',MenuFile_1)
531set(handles.MenuFile_2_1,'Label',MenuFile_2)
532set(handles.MenuFile_3_1,'Label',MenuFile_3)
533set(handles.MenuFile_4_1,'Label',MenuFile_4)
534set(handles.MenuFile_5_1,'Label',MenuFile_5)
535dir_perso=prefdir;
536profil_perso=fullfile(dir_perso,'uvmat_perso.mat');
537if exist(profil_perso,'file')
538    save (profil_perso,'MenuFile_1','MenuFile_2','MenuFile_3','MenuFile_4', 'MenuFile_5','-append'); %store the file names for future opening of uvmat
539else
540    txt=ver('MATLAB');
541    Release=txt.Release;
542    relnumb=str2double(Release(3:4));
543    if relnumb >= 14
544        save (profil_perso,'MenuFile_1','MenuFile_2','MenuFile_3','MenuFile_4', 'MenuFile_5','-V6'); %store the file names for future opening of uvmat
545    else
546        save (profil_perso,'MenuFile_1','MenuFile_2','MenuFile_3','MenuFile_4', 'MenuFile_5'); %store the file names for future opening of uvmat
547    end
548end
549
550% -----------------------------------------------------------------------
551% --- Open again as second field the file whose name has been recorded in MenuFile_1
552function MenuFile_1_1_Callback(hObject, eventdata, handles)
553% -----------------------------------------------------------------------
554fileinput_1=get(handles.MenuFile_1_1,'Label');
555set(handles.SubField,'Value',1)
556display_file_name(handles,fileinput_1,2)
557
558% -----------------------------------------------------------------------
559% --- Open again as second field the file whose name has been recorded in MenuFile_2
560function MenuFile_2_1_Callback(hObject, eventdata, handles)
561% -----------------------------------------------------------------------
562fileinput_1=get(handles.MenuFile_2_1,'Label');
563set(handles.SubField,'Value',1)
564display_file_name(handles,fileinput_1,2)
565
566% -----------------------------------------------------------------------
567% --- Open again as second field the file whose name has been recorded in MenuFile_3
568function MenuFile_3_1_Callback(hObject, eventdata, handles)
569% -----------------------------------------------------------------------
570fileinput_1=get(handles.MenuFile_3_1,'Label');
571set(handles.SubField,'Value',1)
572display_file_name(handles,fileinput_1,2)
573
574% -----------------------------------------------------------------------
575% --- Open again as second field the file whose name has been recorded in MenuFile_4
576function MenuFile_4_1_Callback(hObject, eventdata, handles)
577% -----------------------------------------------------------------------
578fileinput_1=get(handles.MenuFile_4_1,'Label');
579set(handles.SubField,'Value',1)
580display_file_name(handles,fileinput_1,2)
581
582% -----------------------------------------------------------------------
583% --- Open again as second field the file whose name has been recorded in MenuFile_5
584function MenuFile_5_1_Callback(hObject, eventdata, handles)
585% -----------------------------------------------------------------------
586fileinput_1=get(handles.MenuFile_5_1,'Label');
587set(handles.SubField,'Value',1)
588display_file_name(handles,fileinput_1,2)
589
590%------------------------------------------------------------------------
591% --- Called by action in RootPath edit box
592function RootPath_Callback(hObject,eventdata,handles)
593%------------------------------------------------------------------------
594% read the current input file name:
595[RootPath,SubDir,RootFile,FileIndices,FileExt]=read_file_boxes(handles);
596% detect the file type, get the movie object if relevant, and look for the corresponding file series:
597[RootPath,SubDir,RootFile,i1_series,i2_series,j1_series,j2_series,tild,FileType,MovieObject]=find_file_series(fullfile(RootPath,SubDir),[RootFile FileIndices FileExt]);
598% initiate the input file series and refresh the current field view:
599update_rootinfo(handles,i1_series,i2_series,j1_series,j2_series,FileType,MovieObject,1);
600
601%-----------------------------------------------------------------------
602% --- Called by action in RootPath_1 edit box
603function RootPath_1_Callback(hObject,eventdata,handles)
604% -----------------------------------------------------------------------
605% update_rootinfo_1(hObject,eventdata,handles)
606[RootPath,SubDir,RootFile,FileIndices,FileExt]=read_file_boxes_1(handles);
607% detect the file type, get the movie object if relevant, and look for the corresponding file series:
608[RootPath,SubDir,RootFile,i1_series,i2_series,j1_series,j2_series,tild,FileType,MovieObject]=find_file_series(fullfile(RootPath,SubDir),[RootFile FileIndices FileExt]);
609% initiate the input file series and refresh the current field view:
610update_rootinfo(handles,i1_series,i2_series,j1_series,j2_series,FileType,MovieObject,2);
611
612%------------------------------------------------------------------------
613% --- Called by action in RootFile edit box
614function SubDir_Callback(hObject, eventdata, handles)
615%------------------------------------------------------------------------
616%refresh the menu of input fields
617Fields_Callback(hObject, eventdata, handles);
618% refresh the current field view
619run0_Callback(hObject, eventdata, handles);
620
621%------------------------------------------------------------------------
622% --- Called by action in RootFile edit box
623function RootFile_Callback(hObject, eventdata, handles)
624%------------------------------------------------------------------------
625RootPath_Callback(hObject,eventdata,handles)
626
627%-----------------------------------------------------------------------
628% --- Called by action in RootFile_1 edit box
629function RootFile_1_Callback(hObject, eventdata, handles)
630% -----------------------------------------------------------------------
631RootPath_1_Callback(hObject,eventdata,handles)
632
633%------------------------------------------------------------------------
634% --- Called by action in FileIndex edit box
635function FileIndex_Callback(hObject, eventdata, handles)
636%------------------------------------------------------------------------
637[tild,tild,tild,i1,i2,j1,j2]=fileparts_uvmat(get(handles.FileIndex,'String'));
638set(handles.i1,'String',num2str(i1));
639set(handles.i2,'String',num2str(i2));
640set(handles.j1,'String',num2str(j1));
641set(handles.j2,'String',num2str(j2));
642
643% refresh the current field view
644run0_Callback(hObject, eventdata, handles)
645
646%------------------------------------------------------------------------
647% --- Called by action in FileIndex_1 edit box
648function FileIndex_1_Callback(hObject, eventdata, handles)
649%------------------------------------------------------------------------
650run0_Callback(hObject, eventdata, handles)
651
652%------------------------------------------------------------------------
653% --- Called by action in NomType edit box
654function NomType_Callback(hObject, eventdata, handles)
655%------------------------------------------------------------------------
656i1=str2num(get(handles.i1,'String'));
657i2=str2num(get(handles.i2,'String'));
658j1=str2num(get(handles.j1,'String'));
659j2=str2num(get(handles.j2,'String'));
660FileIndex=fullfile_uvmat('','','','',get(handles.NomType,'String'),i1,i2,j1,j2);
661set(handles.FileIndex,'String',FileIndex)
662% refresh the current settings and refresh the field view
663RootPath_Callback(hObject,eventdata,handles)
664
665%------------------------------------------------------------------------
666% --- Called by action in NomType edit box
667function NomType_1_Callback(hObject, eventdata, handles)
668%------------------------------------------------------------------------
669i1=str2num(get(handles.i1,'String'));
670i2=str2num(get(handles.i2,'String'));
671j1=str2num(get(handles.j1,'String'));
672j2=str2num(get(handles.j2,'String'));
673FileIndex=fullfile_uvmat('','','','',get(handles.NomType_1,'String'),i1,i2,j1,j2);
674set(handles.FileIndex_1,'String',FileIndex)
675% refresh the current settings and refresh the field view
676RootPath_1_Callback(hObject,eventdata,handles)
677
678%------------------------------------------------------------------------
679% --- Fills the edit boxes RootPath, RootFile,NomType...from an input file name 'fileinput'
680function display_file_name(handles,fileinput,index)
681%------------------------------------------------------------------------
682%% look for the input file existence
683if ~exist(fileinput,'file')
684    msgbox_uvmat('ERROR',['input file ' fileinput  ' does not exist'])
685    return
686end
687
688%% set the mouse pointer to 'watch'
689set(handles.uvmat,'Pointer','watch')
690set(handles.RootPath,'BackgroundColor',[1 1 0])
691drawnow
692
693%% define the relevant handles for the first field series (index=1) or the second file series (index=2)
694if ~exist('index','var')
695    index=1;
696end
697if index==1
698    handles_RootPath=handles.RootPath;
699    handles_SubDir=handles.SubDir;
700    handles_RootFile=handles.RootFile;
701    handles_FileIndex=handles.FileIndex;
702    handles_NomType=handles.NomType;
703    handles_FileExt=handles.FileExt;
704elseif index==2
705    handles_RootPath=handles.RootPath_1;
706    handles_SubDir=handles.SubDir_1;
707    handles_RootFile=handles.RootFile_1;
708    handles_FileIndex=handles.FileIndex_1;
709    handles_NomType=handles.NomType_1;
710    handles_FileExt=handles.FileExt_1;
711    set(handles.RootPath_1,'Visible','on')
712    set(handles.RootFile_1,'Visible','on')
713    set(handles.SubDir_1,'Visible','on');
714    set(handles.FileIndex_1,'Visible','on');
715    set(handles.FileExt_1,'Visible','on');
716    set(handles.NomType_1,'Visible','on');
717end
718
719%% detect root name, nomenclature and indices in the input file name:
720[FilePath,FileName,FileExt]=fileparts(fileinput);
721% detect the file type, get the movie object if relevant, and look for the corresponding file series:
722% the root name and indices may be corrected by including the first index i1 if a corresponding xml file exists
723[RootPath,SubDir,RootFile,i1_series,i2_series,j1_series,j2_series,NomType,FileType,MovieObject,i1,i2,j1,j2]=find_file_series(FilePath,[FileName FileExt]);
724
725%% open the file or fill the GUI uvmat according to the detected file type
726switch FileType
727    case ''
728        msgbox_uvmat('ERROR','invalid input file type')
729    case 'txt'
730        edit(fileinput)
731    case 'figure'                           %display matlab figure
732        hfig=open(fileinput);
733        set(hfig,'WindowButtonMotionFcn','mouse_motion')%set mouse action functio
734        set(hfig,'WindowButtonUpFcn','mouse_up')%set mouse click action function
735        set(hfig,'WindowButtonUpFcn','mouse_down')%set mouse click action function
736    case {'xml','xls'}                % edit xml or Excel files
737        editxml(fileinput);
738    otherwise
739        set(handles_RootPath,'String',RootPath);
740        rootname=fullfile(RootPath,SubDir,RootFile);
741        set(handles_SubDir,'String',['/' SubDir]);
742        set(handles_RootFile,'String',['/' RootFile]); %display the separator
743        indices=fileinput(length(rootname)+1:end);
744        indices(end-length(FileExt)+1:end)=[]; %remove extension
745        set(handles_FileIndex,'String',indices);
746        set(handles_NomType,'String',NomType);
747        set(handles_FileExt,'String',FileExt);
748        if index==1
749            % fill file index counters if the first file series is opened
750            set(handles.i1,'String',num2str(i1));
751            set(handles.i2,'String',num2str(i2));
752            set(handles.j1,'String',num2stra(j1,NomType));
753            set(handles.j2,'String',num2stra(j2,NomType));
754        else %read the current field index if the second file series is opened
755            i1=str2num(get(handles.i1,'String'));
756            i2=str2num(get(handles.i2,'String'));
757            j1=stra2num(get(handles.j1,'String'));
758            j2=stra2num(get(handles.j2,'String'));
759        end
760       
761        % synchronise indices of the second  input file if it exists
762        if get(handles.SubField,'Value')==1% if the subfield button is activated, update the field numbers
763            Input=read_GUI(handles.InputFile);
764            if ~isfield(Input,'RootPath_1')||strcmp(Input.RootPath_1,'"')
765                Input.RootPath_1=Input.RootPath;
766            end
767            if ~isfield(Input,'SubDir_1')||strcmp(Input.SubDir_1,'"')
768                Input.SubDir_1=Input.SubDir;
769            end
770            if ~isfield(Input,'RootFile_1')||strcmp(Input.RootFile_1,'"')
771                Input.RootFile_1=Input.RootFile;
772            end
773            if ~isfield(Input,'FileExt_1')||strcmp(Input.FileExt_1,'"')
774                Input.FileExt_1=Input.FileExt;
775            end
776            if ~isfield(Input,'NomType_1')||strcmp(Input.NomType_1,'"')
777                Input.NomType_1=Input.NomType;
778            end
779            %updtate the indices of the second field series to correspond to the newly opened one
780            FileName_1=fullfile_uvmat(Input.RootPath_1,Input.SubDir_1,Input.RootFile_1,Input.FileExt_1,Input.NomType_1,i1,i2,j1,j2);
781            if exist(FileName_1,'file')
782                FileIndex_1=fullfile_uvmat('','','','',Input.NomType_1,i1,i2,j1,j2);
783                set(handles.FileIndex_1,'String',FileIndex_1)
784            else
785                msgbox_uvmat('WARNING','unable to synchronise the indices of the two series')
786%                 set(handles.SubField,'Value',0)
787%                 SubField_Callback([], [], handles)
788            end
789        end
790       
791        %enable other menus
792        set(handles.MenuOpen_1,'Enable','on')
793        set(handles.MenuFile_1_1,'Enable','on')
794        set(handles.MenuFile_2_1,'Enable','on')
795        set(handles.MenuFile_3_1,'Enable','on')
796        set(handles.MenuFile_4_1,'Enable','on')
797        set(handles.MenuFile_5_1,'Enable','on')
798        set(handles.MenuExport,'Enable','on')
799        set(handles.MenuExportFigure,'Enable','on')
800        set(handles.MenuExportMovie,'Enable','on')
801        set(handles.MenuTools,'Enable','on')
802        set(handles.OBJECT_txt,'Visible','on')
803        set(handles.edit_object,'Visible','on')
804        set(handles.ListObject,'Visible','on')
805        set(handles.frame_object,'Visible','on')
806       
807        % initiate input file series and refresh the current field view:     
808        update_rootinfo(handles,i1_series,i2_series,j1_series,j2_series,FileType,MovieObject,index);
809
810end
811
812%% set back the mouse pointer to arrow
813set(handles.uvmat,'Pointer','arrow')
814
815
816%------------------------------------------------------------------------
817% --- Update information about a new field series (indices to scan, timing,
818%     calibration from an xml file, then refresh current plots
819function update_rootinfo(handles,i1_series,i2_series,j1_series,j2_series,FileType,VideoObject,index)
820%------------------------------------------------------------------------
821%% define the relevant handles depending on the index (1=first file series, 2= second file series)
822if ~exist('index','var')
823    index=1;
824end
825if index==1
826    handles_RootPath=handles.RootPath;
827    handles_Fields=handles.Fields;
828elseif index==2
829    handles_RootPath=handles.RootPath_1;
830    handles_Fields=handles.Fields_1;
831end
832
833set(handles_RootPath,'BackgroundColor',[1 1 0])
834drawnow
835set(handles.Fields,'UserData',[])% reinialize data from uvmat opening
836UvData=get(handles.uvmat,'UserData');%huvmat=handles of the uvmat interface
837UvData.NewSeries=1; %flag for run0: begin a new series
838UvData.TestInputFile=1;
839UvData.FileType{index}=FileType;
840UvData.i1_series{index}=i1_series;
841UvData.i2_series{index}=i2_series;
842UvData.j1_series{index}=j1_series;
843UvData.j2_series{index}=j2_series;
844% set(handles.CheckFixPair,'Value',1) % activate by default the comp_input '-'input window
845set(handles.FixVelType,'Value',0); %desactivate fixed veltype
846if index==1
847    [RootPath,SubDir,RootFile,FileIndices,FileExt]=read_file_boxes(handles);
848else
849    [RootPath,SubDir,RootFile,FileIndices,FileExt]=read_file_boxes_1(handles);
850end
851FileName=[fullfile(RootPath,SubDir,RootFile) FileIndices FileExt];
852FileBase=fullfile(RootPath,RootFile);
853if ~exist(FileName,'file')
854   msgbox_uvmat('ERROR',['input file ' FileName ' not found']);
855    return
856end
857nbfield=[];%default
858nbfield_j=[];%default
859
860%% read timing and total frame number from the current file (movie files) !! may be overrid by xml file
861XmlData.Time=[];%default
862XmlData.GeometryCalib=[];%default
863TimeUnit=[];%default
864testima=0; %test for image input
865imainfo=[];
866ColorType='falsecolor'; %default
867hhh=[];
868UvData.MovieObject{index}=VideoObject;
869if ~isempty(VideoObject)
870    imainfo=get(VideoObject);
871    testima=1;
872    nbfield_j=1;
873    TimeUnit='s';
874    XmlData.Time=(0:1/imainfo.FrameRate:(imainfo.NumberOfFrames-1)/imainfo.FrameRate)';
875    % %         nbfield=imainfo.NumberOfFrames;
876    set(handles.Dt_txt,'String',['Dt=' num2str(1000/imainfo.FrameRate) 'ms']);%display the elementary time interval in millisec
877    ColorType='truecolor';
878elseif ~isempty(FileExt(2:end))&&(~isempty(imformats(FileExt(2:end))) || isequal(FileExt,'.vol'))%&& isequal(NomType,'*')% multi-frame image
879    testima=1;
880    if ~isequal(SubDir,'')
881        RootFile=get(handles.RootFile,'String');
882        imainfo=imfinfo([fullfile(RootPath,SubDir,RootFile) FileIndices FileExt]);
883    else
884        imainfo=imfinfo([FileBase FileIndices FileExt]);
885    end
886    ColorType=imainfo.ColorType;%='truecolor' for color images
887    if length(imainfo) >1 %case of image with multiple frames
888        nbfield=length(imainfo);
889        nbfield_j=1;
890    end
891end
892if isfield(imainfo,'Width') && isfield(imainfo,'Height')
893    if length(imainfo)>1
894        set(handles.num_Npx,'String',num2str(imainfo(1).Width));%fills nbre of pixels x box
895        set(handles.num_Npy,'String',num2str(imainfo(1).Height));%fills nbre of pixels x box
896    else
897        set(handles.num_Npx,'String',num2str(imainfo.Width));%fills nbre of pixels x box
898        set(handles.num_Npy,'String',num2str(imainfo.Height));%fills nbre of pixels x box
899    end
900else
901    set(handles.num_Npx,'String','');%fills nbre of pixels x box
902    set(handles.num_Npy,'String','');%fills nbre of pixels x box
903end
904set(handles.CheckBW,'Value',strcmp(ColorType,'grayscale'))% select handles.CheckBW if grayscale image
905
906%% read parameters (time, geometric calibration..) from a documentation file (.xml advised)
907filexml=[FileBase '.xml'];
908fileciv=[FileBase '.civ'];
909warntext='';%default warning message
910NbSlice=1;%default
911set(handles.RootPath,'BackgroundColor',[1 1 1])
912if exist(filexml,'file')
913    set(handles.view_xml,'Visible','on')
914    set(handles.view_xml,'BackgroundColor',[1 1 0])
915    set(handles.view_xml,'String','view .xml')
916    drawnow
917    [XmlData,warntext]=imadoc2struct(filexml);
918    if ~isempty(warntext)
919        msgbox_uvmat('WARNING',warntext)
920    end
921    if isfield(XmlData,'TimeUnit')
922        if isfield(XmlData,'TimeUnit')&& ~isempty(XmlData.TimeUnit)
923            TimeUnit=XmlData.TimeUnit;
924        end
925    end
926    set(handles.view_xml,'BackgroundColor',[1 1 1])
927    drawnow
928    if isfield(XmlData, 'GeometryCalib') && ~isempty(XmlData.GeometryCalib)
929        if isfield(XmlData.GeometryCalib,'VolumeScan') && isequal(XmlData.GeometryCalib.VolumeScan,'y')
930            set (handles.nb_slice,'String','volume')
931        end
932        hgeometry_calib=findobj('tag','geometry_calib');
933        if ~isempty(hgeometry_calib)
934            GUserData=get(hgeometry_calib,'UserData');
935            if ~(isfield(GUserData,'XmlInputFile') && strcmp(GUserData.XmlInputFile,filexml))
936                answer=msgbox_uvmat('INPUT_Y-N','replace the display of geometry_calib with the new input data?');
937                if strcmp(answer,'Yes')
938                    geometry_calib(filexml);%diplay the new calibration points and parameters in geometry_calib
939                end
940            end
941        end
942    end 
943elseif exist(fileciv,'file')% if .civ file found
944    [error,XmlData.Time,TimeUnit,mode,npx,npy,pxcmx,pxcmy]=read_imatext([FileBase '.civ']);
945    GeometryCalib.R=[pxcmx 0 0; 0 pxcmy 0;0 0 0];
946    GeometryCalib.Tx=0;
947    GeometryCalib.Ty=0;
948    GeometryCalib.Tz=1;
949    GeometryCalib.dpx=1;
950    GeometryCalib.dpy=1;
951    GeometryCalib.sx=1;
952    GeometryCalib.Cx=0;
953    GeometryCalib.Cy=0;
954    GeometryCalib.f=1;
955    GeometryCalib.kappa1=0;
956    GeometryCalib.CoordUnit='cm';
957    XmlData.GeometryCalib=GeometryCalib;
958    if error==2, warntext=['no file ' FileBase '.civ'];
959    elseif error==1, warntext='inconsistent number of fields in the .civ file';
960    end 
961    set(handles.num_Npx,'String',num2str(npx));%fills nbre of pixels x box
962    set(handles.num_Npy,'String',num2str(npy));%fills nbre of pixels y box
963    set(handles.pxcm,'String',num2str(pxcmx));%fills scale x (pixel/cm) box
964    set(handles.pycm,'String',num2str(pxcmy));%fills scale y (pixel/cm) box
965    set(handles.pxcm,'Visible','on');%fills scale x (pixel/cm) box
966    set(handles.pycm,'Visible','on');%fills scale y (pixel/cm) box
967    set(handles.view_xml,'Visible','on')   
968    set(handles.view_xml,'String','view .civ')
969else
970    set(handles.view_xml,'Visible','off')
971end
972
973%% store last index in handles.lat_i and .last_j
974nbfield=max(max(i2_series));
975if isempty(nbfield)
976    nbfield=max(max(i1_series));
977end
978nbfield_j=max(max(j2_series));
979if isempty(nbfield_j)
980    nbfield_j=max(max(j1_series));
981end
982if ~isempty(XmlData.Time)
983%     nbfield=size(XmlData.Time,1);
984%     nbfield_j=size(XmlData.Time,2);
985    %transform .Time to a column vector if it is a line vector the nomenclature uses a single index
986    if isequal(nbfield,1) && ~isequal(nbfield_j,1)% .Time is a line vector
987        NomType=get(handles.NomType,'String');
988        if numel(NomType)>=2 &&(strcmp(NomType,'_i')||strcmp(NomType(1:2),'%0')||strcmp(NomType(1:2),'_%'))
989            XmlData.Time=(XmlData.Time)';
990%             nbfield=nbfield_j;
991%             nbfield_j=1;
992        end
993    end
994end
995last_i_cell=get(handles.last_i,'String');
996if isempty(nbfield)
997    last_i_cell{1}='';
998else
999    last_i_cell{1}=num2str(nbfield);
1000end
1001set(handles.last_i,'String',last_i_cell)
1002last_j_cell=get(handles.last_j,'String');
1003if isempty(nbfield_j)
1004     last_j_cell{1}='';
1005else
1006     last_j_cell{1}=num2str(nbfield_j);
1007end
1008set(handles.last_j,'String',last_j_cell);
1009
1010%% store geometric calibration in UvData
1011if isfield(XmlData,'GeometryCalib')
1012    GeometryCalib=XmlData.GeometryCalib;
1013    if isempty(GeometryCalib)
1014        set(handles.pxcm,'String','')
1015        set(handles.pycm,'String','')
1016        set(handles.transform_fct,'Value',1); %  no transform by default
1017    else
1018        if (isfield(GeometryCalib,'R')&& ~isequal(GeometryCalib.R(2,1),0) && ~isequal(GeometryCalib.R(1,2),0)) ||...
1019            (isfield(GeometryCalib,'kappa1')&& ~isequal(GeometryCalib.kappa1,0))
1020            set(handles.pxcm,'String','var')
1021            set(handles.pycm,'String','var')
1022        elseif isfield(GeometryCalib,'fx_fy')
1023            pixcmx=GeometryCalib.fx_fy(1);%*GeometryCalib.R(1,1)*GeometryCalib.sx/(GeometryCalib.Tz*GeometryCalib.dpx);
1024            pixcmy=GeometryCalib.fx_fy(2);%*GeometryCalib.R(2,2)/(GeometryCalib.Tz*GeometryCalib.dpy);
1025            set(handles.pxcm,'String',num2str(pixcmx))
1026            set(handles.pycm,'String',num2str(pixcmy))
1027        end
1028        if ~get(handles.CheckFixLimits,'Value')
1029            set(handles.transform_fct,'Value',2); % phys transform by default if fixedLimits is off
1030        end
1031        if isfield(GeometryCalib,'SliceCoord')
1032           
1033           siz=size(GeometryCalib.SliceCoord);
1034           if siz(1)>1
1035               NbSlice=siz(1);
1036               set(handles.slices,'Visible','on')
1037               set(handles.slices,'Value',1)
1038           end
1039           if isfield(GeometryCalib,'VolumeScan') && isequal(GeometryCalib.VolumeScan,'y')
1040               set(handles.nb_slice,'String','volume')
1041           else
1042               set(handles.nb_slice,'String',num2str(NbSlice))
1043           end
1044           slices_Callback([],[], handles)
1045        end           
1046    end
1047end
1048
1049%% update the data attached to the uvmat interface
1050if ~isempty(TimeUnit)
1051    set(handles.time_txt,'String',['time (' TimeUnit ')'])
1052end
1053UvData.TimeUnit=TimeUnit;
1054UvData.XmlData{index}=XmlData;
1055UvData.NewSeries=1;
1056% UvData.MovieObject=MovieObject;
1057
1058%display warning message
1059if ~isequal(warntext,'')
1060    msgbox_uvmat('WARNING',warntext);
1061end
1062
1063%% set default options in menu 'Fields'
1064if ~testima
1065    testcivx=0;
1066    if isfield(UvData,'FieldsString') && isequal(UvData.FieldsString,{'get_field...'})% field menu defined as input (from get_field)
1067        set(handles_Fields,'Value',1)
1068        set(handles_Fields,'String',{'get_field...'})
1069        UvData=rmfield(UvData,'FieldsString');
1070    else
1071        Data=nc2struct(FileName,'ListGlobalAttribute','Conventions','absolut_time_T0','civ');
1072        if strcmp(Data.Conventions,'uvmat/civdata') ||( ~isempty(Data.absolut_time_T0)&& ~isequal(Data.civ,0))%if the new input is Civx
1073            FieldList=calc_field;
1074            set(handles_Fields,'String',[{'image'};FieldList;{'get_field...'}]);%standard menu for civx data
1075            set(handles_Fields,'Value',2) % set menu to 'velocity'
1076            col_vec=FieldList;
1077            col_vec(1)=[];%remove 'velocity' option for vector color (must be a scalar)
1078            testcivx=1;
1079        end
1080        if ~testcivx
1081            set(handles_Fields,'Value',1) % set menu to 'get_field...
1082            set(handles_Fields,'String',{'get_field...'})
1083            col_vec={'get_field...'};
1084        end
1085        set(handles.ColorScalar,'String',col_vec)
1086    end
1087end
1088set(handles.uvmat,'UserData',UvData)
1089
1090%% set index navigation options and refresh plots
1091scan_option='i';%default
1092state_j='off'; %default
1093if index==2
1094    if get(handles.scan_j,'Value')
1095        scan_option='j'; %keep the scan option for the second fiel series
1096    end
1097    if strcmp(get(handles.j1,'Visible'),'on')
1098        state_j='on';
1099    end
1100end
1101if ~isempty(j1_series)
1102        state_j='on';
1103        if isequal(nbfield,1) &&index==1
1104            scan_option='j'; %scan j index by default if nbfield=1               
1105        end
1106end
1107if isequal(scan_option,'i')
1108     set(handles.scan_i,'Value',1)
1109     scan_i_Callback([],[], handles);
1110else
1111     set(handles.scan_j,'Value',1)
1112     scan_j_Callback([],[], handles);
1113end
1114set(handles.scan_j,'Visible',state_j)
1115set(handles.j1,'Visible',state_j)
1116set(handles.j2,'Visible',state_j)
1117set(handles.last_j,'Visible',state_j);
1118set(handles.frame_j,'Visible',state_j);
1119set(handles.j_text,'Visible',state_j);
1120if ~isempty(i2_series)||~isempty(j2_series)
1121    set(handles.CheckFixPair,'Visible','on')
1122elseif index==1
1123    set(handles.CheckFixPair,'Visible','off')
1124end
1125
1126%% view the field 
1127run0_Callback([],[], handles); %view field
1128mask_test=get(handles.CheckMask,'value');
1129if mask_test
1130    MaskData=get(handles.CheckMask,'UserData');
1131    if isfield(MaskData,'maskhandle') && ishandle(MaskData.maskhandle)
1132          delete(MaskData.maskhandle)    %delete old mask
1133    end
1134    CheckMask_Callback([],[],handles)
1135end
1136
1137%% update list of recent files in the menubar and save it for future opening
1138MenuFile=[{get(handles.MenuFile_1,'Label')};{get(handles.MenuFile_2,'Label')};...
1139    {get(handles.MenuFile_3,'Label')};{get(handles.MenuFile_4,'Label')};{get(handles.MenuFile_5,'Label')}];
1140str_find=strcmp(FileName,MenuFile);
1141if isempty(find(str_find,1))
1142    MenuFile=[{FileName};MenuFile];%insert the current file if not already in the list
1143end
1144for ifile=1:min(length(MenuFile),5)
1145    eval(['set(handles.MenuFile_' num2str(ifile) ',''Label'',MenuFile{ifile});'])
1146    eval(['set(handles.MenuFile_' num2str(ifile) '_1,''Label'',MenuFile{ifile});'])
1147end
1148dir_perso=prefdir;
1149profil_perso=fullfile(dir_perso,'uvmat_perso.mat');
1150if exist(profil_perso,'file')
1151    save (profil_perso,'MenuFile','-append'); %store the file names for future opening of uvmat
1152else
1153    save (profil_perso,'MenuFile','-V6'); %store the file names for future opening of uvmat
1154end
1155
1156
1157%------------------------------------------------------------------------
1158% --- switch file index scanning options scan_i and scan_j in an exclusive way
1159function scan_i_Callback(hObject, eventdata, handles)
1160%------------------------------------------------------------------------
1161if get(handles.scan_i,'Value')==1
1162    set(handles.scan_i,'BackgroundColor',[1 1 0])
1163    set(handles.scan_j,'Value',0)
1164else
1165    set(handles.scan_i,'BackgroundColor',[0.831 0.816 0.784])
1166    set(handles.scan_j,'Value',1)
1167end
1168scan_j_Callback(hObject, eventdata, handles)
1169
1170%------------------------------------------------------------------------
1171% --- switch file index scanning options scan_i and scan_j in an exclusive way
1172function scan_j_Callback(hObject, eventdata, handles)
1173%------------------------------------------------------------------------
1174if get(handles.scan_j,'Value')==1
1175    set(handles.scan_j,'BackgroundColor',[1 1 0])
1176    set(handles.scan_i,'Value',0)
1177    set(handles.scan_i,'BackgroundColor',[0.831 0.816 0.784])
1178%     NomType=get(handles.NomType,'String');
1179%     switch NomType
1180%     case {'_1_1-2','#_ab','%3dab'},% pair with j index
1181%         set(handles.CheckFixPair,'Visible','on')% option fixed pair on/off made visible (choice of avaible pair with buttons + and - if ='off')
1182%     otherwise
1183%         set(handles.CheckFixPair,'Visible','off')
1184%     end
1185else
1186    set(handles.scan_j,'BackgroundColor',[0.831 0.816 0.784])
1187    set(handles.scan_i,'Value',1)
1188    set(handles.scan_i,'BackgroundColor',[1 1 0])
1189    set(handles.CheckFixPair,'Visible','off')
1190end
1191
1192%------------------------------------------------------------------------
1193function i1_Callback(hObject, eventdata, handles)
1194%------------------------------------------------------------------------
1195set(handles.i1,'BackgroundColor',[0.7 0.7 0.7])
1196NomType=get(handles.NomType,'String');
1197i1=stra2num(get(handles.i1,'String'));
1198i2=stra2num(get(handles.i2,'String'));
1199j1=stra2num(get(handles.j1,'String'));
1200j2=stra2num(get(handles.j2,'String'));
1201indices=fullfile_uvmat('','','','',NomType,i1,i2,j1,j2);
1202%indices=name_generator('',num1,num_a,'',NomType,1,num2,num_b,'');
1203set(handles.FileIndex,'String',indices)
1204set(handles.FileIndex,'BackgroundColor',[0.7 0.7 0.7])
1205if get(handles.SubField,'Value')==1
1206    NomType_1=get(handles.NomType_1,'String');
1207    indices=fullfile_uvmat('','','','',NomType,i1,i2,j1,j2);
1208     %indices=name_generator('',num1,num_a,'',NomType_1,1,num2,num_b,'');
1209     set(handles.FileIndex_1,'String',indices)
1210     set(handles.FileIndex_1,'BackgroundColor',[0.7 0.7 0.7])
1211end
1212
1213%------------------------------------------------------------------------
1214function i2_Callback(hObject, eventdata, handles)
1215%------------------------------------------------------------------------
1216set(handles.i2,'BackgroundColor',[0.7 0.7 0.7])
1217i1_Callback(hObject, eventdata, handles)
1218
1219%------------------------------------------------------------------------
1220function j1_Callback(hObject, eventdata, handles)
1221%------------------------------------------------------------------------
1222set(handles.j1,'BackgroundColor',[0.7 0.7 0.7])
1223i1_Callback(hObject, eventdata, handles)
1224
1225%------------------------------------------------------------------------
1226function j2_Callback(hObject, eventdata, handles)
1227%------------------------------------------------------------------------
1228set(handles.j2,'BackgroundColor',[0.7 0.7 0.7])
1229i1_Callback(hObject, eventdata, handles)
1230
1231%------------------------------------------------------------------------
1232function slices_Callback(hObject, eventdata, handles)
1233%------------------------------------------------------------------------
1234if get(handles.slices,'Value')==1
1235    set(handles.slices,'BackgroundColor',[1 1 0])
1236    set(handles.nb_slice,'Visible','on')
1237    set(handles.z_text,'Visible','on')
1238    set(handles.z_index,'Visible','on')
1239    nb_slice_Callback(hObject, eventdata, handles)
1240else
1241    set(handles.nb_slice,'Visible','off')
1242    set(handles.slices,'BackgroundColor',[0.7 0.7 0.7])
1243    set(handles.z_text,'Visible','off')
1244    set(handles.z_index,'Visible','off')
1245    set(handles.masklevel,'Value',1)
1246    set(handles.masklevel,'String',{'1'})
1247end
1248
1249%------------------------------------------------------------------------
1250function nb_slice_Callback(hObject, eventdata, handles)
1251%------------------------------------------------------------------------
1252nb_slice_str=get(handles.nb_slice,'String');
1253if isequal(nb_slice_str,'volume')
1254    num=stra2num(get(handles.j1,'String'));
1255    last_j=get(handles.last_j,'String');
1256    nbslice=str2double(last_j{1});
1257else
1258    num=str2double(get(handles.i1,'String'));
1259    nbslice=str2double(get(handles.nb_slice,'String'));
1260end
1261z=mod(num-1,nbslice)+1;
1262set(handles.z_index,'String',num2str(z))
1263for ilist=1:nbslice
1264    list_index{ilist,1}=num2str(ilist);
1265end   
1266set(handles.masklevel,'String',list_index)
1267set(handles.masklevel,'Value',z)
1268
1269%------------------------------------------------------------------------
1270% --- Executes on button press in view_xml.
1271function view_xml_Callback(hObject, eventdata, handles)
1272%------------------------------------------------------------------------
1273%[FileName,RootPath,FileBase,FileIndices,FileExt]=read_file_boxes(handles);
1274 [RootPath,SubDir,RootFile,FileIndices,FileExt]=read_file_boxes(handles);
1275FileBase=fullfile(RootPath,RootFile);
1276option=get(handles.view_xml,'String');
1277if isequal(option,'view .xml')
1278    FileXml=[FileBase '.xml'];
1279    heditxml=editxml(FileXml);
1280end
1281
1282%------------------------------------------------------------------------
1283% --- Executes on button press in CheckMask.
1284function CheckMask_Callback(hObject, eventdata, handles)
1285%------------------------------------------------------------------------
1286%case of view mask selection
1287if isequal(get(handles.CheckMask,'Value'),1)
1288   % [FF,RootPath,FileBase]=read_file_boxes(handles);
1289     [RootPath,SubDir,RootFile,FileIndices,FileExt]=read_file_boxes(handles);
1290     FileBase=fullfile(RootPath,RootFile);
1291    num_i1=stra2num(get(handles.i1,'String'));
1292    num_j1=stra2num(get(handles.j1,'String'));
1293    currentdir=pwd; 
1294    cd(RootPath);
1295    maskfiles=dir('*_*mask_*.png');%look for a mask file
1296    cd(currentdir);%come back to the working directory
1297    mdetect=0;
1298    if ~isempty(maskfiles)
1299        for ilist=1:length(maskfiles)
1300            maskname=maskfiles(ilist).name;% take the first mask file in the list
1301            [tild,tild,tild,tild,tild,tild,tild,MaskExt,Mask_NomType{ilist}]=fileparts_uvmat(maskname);
1302%             [rr,ff,x1,x2,xa,xb,xext,Mask_NomType{ilist}]=name2display(maskname);
1303             [tild,Name]=fileparts(maskname);
1304            Namedouble=double(Name);
1305            val=(48>Namedouble)|(Namedouble>57);% select the non-numerical characters
1306            ind_mask=findstr('mask',Name);
1307            i=ind_mask-1;
1308            while val(i)==0 && i>0
1309                i=i-1;
1310            end
1311            nbmask_str=str2num(Name(i+1:ind_mask-1));
1312            if ~isempty(nbmask_str)
1313                nbslice(ilist)=nbmask_str; % number of different masks (slices)
1314            end
1315        end
1316        if isequal(min(nbslice),max(nbslice))
1317            nbslice=nbslice(1);
1318        else
1319            msgbox_uvmat('ERROR','several inconsistent mask sets coexist in the current image directory')
1320            return
1321        end
1322        if ~isempty(nbslice) && Name(i)=='_'
1323            Mask.Base=[FileBase Name(i:ind_mask+3)];
1324            Mask.NbSlice=nbslice;
1325            num_i1=mod(num_i1-1,nbslice)+1;
1326            Mask.NomType=regexprep(Mask_NomType{1},'0','');%remove '0' in nom type for masks
1327            [RootPath,RootFile]=fileparts(Mask.Base);
1328            maskname=fullfile_uvmat(RootPath,'',RootFile,'.png',Mask.NomType,num_i1,[],num_j1);
1329            %maskname=name_generator(Mask.Base,num_i1,num_j1,'.png',Mask.NomType);%
1330            mdetect=exist(maskname,'file');
1331            if mdetect
1332                set(handles.nb_slice,'String',Name(i+1:ind_mask-1));
1333                set(handles.nb_slice,'BackgroundColor',[1 1 0])
1334                set(handles.CheckMask,'UserData',Mask);
1335                set(handles.CheckMask,'BackgroundColor',[1 1 0])
1336                if nbslice > 1
1337                    set(handles.slices,'value',1)
1338                    slices_Callback(hObject, eventdata, handles)
1339                end
1340            end
1341        end
1342    end
1343    errormsg=[];%default
1344    if mdetect==0
1345        [FileName, PathName, filterindex] = uigetfile( ...
1346            {'*.png', ' (*.png)';
1347            '*.png',  '.png files '; ...
1348            '*.*', 'All Files (*.*)'}, ...
1349            'Pick a mask file *.png',FileBase);
1350        maskname=fullfile(PathName,FileName);
1351        if ~exist(maskname,'file')
1352            errormsg='no file browsed';
1353        end
1354        [RootDir,tild,RootFile,tild,tild,tild,tild,tild,Mask.NomType]=fileparts_uvmat(maskname);
1355%         [RootDir,RootFile,x1,x2,xa,xb,xext,Mask.NomType]=name2display(maskname);
1356        Mask.Base=fullfile(RootDir,RootFile);
1357        Mask.NbSlice=1;
1358        set(handles.CheckMask,'UserData',Mask);
1359        set(handles.CheckMask,'BackgroundColor',[1 1 0])
1360    end
1361    if isempty(errormsg)
1362        errormsg=update_mask(handles,num_i1,num_j1);
1363    end
1364    if ~isempty(errormsg)
1365            set(handles.CheckMask,'Value',0)
1366            set(handles.CheckMask,'BackgroundColor',[0.7 0.7 0.7])
1367     end
1368else % desactivate mask display
1369    MaskData=get(handles.CheckMask,'UserData');
1370    if isfield(MaskData,'maskhandle') && ishandle(MaskData.maskhandle)
1371          delete(MaskData.maskhandle)   
1372    end
1373    set(handles.CheckMask,'UserData',[])   
1374    UvData=get(handles.uvmat,'UserData');
1375    if isfield(UvData,'MaskName')
1376        UvData=rmfield(UvData,'MaskName');
1377        set(handles.uvmat,'UserData',UvData)
1378    end
1379    set(handles.CheckMask,'BackgroundColor',[0.7 0.7 0.7])
1380end
1381
1382%------------------------------------------------------------------------
1383function errormsg=update_mask(handles,num_i1,num_j1)
1384%------------------------------------------------------------------------
1385errormsg=[];%default
1386MaskData=get(handles.CheckMask,'UserData');
1387if isfield(MaskData,'maskhandle')&& ishandle(MaskData.maskhandle)
1388    uistack(MaskData.maskhandle,'top');
1389end
1390num_i1_mask=mod(num_i1-1,MaskData.NbSlice)+1;
1391[RootPath,RootFile]=fileparts(MaskData.Base);
1392MaskName=fullfile_uvmat(RootPath,'',RootFile,'.png',MaskData.NomType,num_i1_mask,[],num_j1);
1393%MaskName=name_generator(MaskData.Base,num_i1_mask,num_j1,'.png',MaskData.NomType);
1394% huvmat=get(handles.CheckMask,'parent');
1395UvData=get(handles.uvmat,'UserData');
1396%update mask image if the mask is new
1397if ~ (isfield(UvData,'MaskName') && isequal(UvData.MaskName,MaskName))
1398    UvData.MaskName=MaskName; %update the recorded name on UvData
1399    set(handles.uvmat,'UserData',UvData);
1400    if ~exist(MaskName,'file')
1401        if isfield(MaskData,'maskhandle')&& ishandle(MaskData.maskhandle)
1402            delete(MaskData.maskhandle)   
1403        end
1404    else
1405        %read mask image
1406        Mask.AName='image';
1407        Mask.A=imread(MaskName);
1408        npxy=size(Mask.A);
1409        test_error=0;
1410        if length(npxy)>2
1411            errormsg=[MaskName ' is not a grey scale image'];
1412            return
1413        elseif ~isa(Mask.A,'uint8')
1414            errormsg=[MaskName ' is not a 8 bit grey level image'];
1415            return
1416        end
1417        Mask.AX=[0.5 npxy(2)-0.5];
1418        Mask.AY=[npxy(1)-0.5 0.5 ];
1419        Mask.CoordUnit='pixel';
1420        if isequal(get(handles.slices,'Value'),1)
1421           NbSlice=str2num(get(handles.nb_slice,'String'));
1422           num_i1=str2num(get(handles.i1,'String'));
1423           Mask.ZIndex=mod(num_i1-1,NbSlice)+1;
1424        end
1425        %px to phys or other transform on field
1426         menu_transform=get(handles.transform_fct,'String');
1427        choice_value=get(handles.transform_fct,'Value');
1428        transform_name=menu_transform{choice_value};%name of the transform fct  given by the menu 'transform_fct'
1429        transform_list=get(handles.transform_fct,'UserData');
1430        transform=transform_list{choice_value};
1431        if  ~isequal(transform_name,'') && ~isequal(transform_name,'px')
1432            if isfield(UvData,'XmlData') && isfield(UvData.XmlData{1},'GeometryCalib')%use geometry calib recorded from the ImaDoc xml file as first priority
1433                Calib=UvData.XmlData{1}.GeometryCalib;
1434                Mask=transform(Mask,UvData.XmlData{1});
1435            end
1436        end
1437        flagmask=Mask.A < 200;
1438       
1439        %make brown color image
1440        imflag(:,:,1)=0.9*flagmask;
1441        imflag(:,:,2)=0.7*flagmask;
1442        imflag(:,:,3)=zeros(size(flagmask));
1443       
1444        %update mask image
1445        hmask=[]; %default
1446        if isfield(MaskData,'maskhandle')&& ishandle(MaskData.maskhandle)
1447            hmask=MaskData.maskhandle;
1448        end
1449        if ~isempty(hmask)
1450            set(hmask,'CData',imflag)   
1451            set(hmask,'AlphaData',flagmask*0.6)
1452            set(hmask,'XData',Mask.AX);
1453            set(hmask,'YData',Mask.AY);
1454%             uistack(hmask,'top')
1455        else
1456            axes(handles.axes3)
1457            hold on   
1458            MaskData.maskhandle=image(Mask.AX,Mask.AY,imflag,'Tag','mask','HitTest','off','AlphaData',0.6*flagmask);
1459%             set(MaskData.maskhandle,'AlphaData',0.6*flagmask)
1460            set(handles.CheckMask,'UserData',MaskData)
1461        end
1462    end
1463end
1464
1465%------------------------------------------------------------------------
1466%------------------------------------------------------------------------
1467% III - MAIN REFRESH FUNCTIONS : 'FRAME PLOT'
1468%------------------------------------------------------------------------
1469
1470%------------------------------------------------------------------------
1471% --- Executes on button press in runplus: make one step forward and call
1472% --- run0. The step forward is along the fields series 1 or 2 depending on
1473% --- the scan_i and scan_j check box (exclusive each other)
1474function runplus_Callback(hObject, eventdata, handles)
1475%------------------------------------------------------------------------
1476set(handles.runplus,'BackgroundColor',[1 1 0])%paint the command button in yellow
1477drawnow
1478%TODO: introduce the option: increment ='*' to move to the next available view
1479increment=str2num(get(handles.increment_scan,'String')); %get the field increment d
1480% if isnan(increment)
1481%     set(handles.increment_scan,'String','1')%default value
1482%     increment=1;
1483% end
1484errormsg=runpm(hObject,eventdata,handles,increment);
1485if ~isempty(errormsg)
1486    msgbox_uvmat('ERROR',errormsg);
1487end
1488set(handles.runplus,'BackgroundColor',[1 0 0])%paint the command button back to red
1489
1490%------------------------------------------------------------------------
1491% --- Executes on button press in runmin: make one step backward and call
1492% --- run0. The step backward is along the fields series 1 or 2 depending on
1493% --- the scan_i and scan_j check box (exclusive each other)
1494function runmin_Callback(hObject, eventdata, handles)
1495%------------------------------------------------------------------------
1496set(handles.runmin,'BackgroundColor',[1 1 0])%paint the command button in yellow
1497drawnow
1498increment=-str2num(get(handles.increment_scan,'String')); %get the field increment d
1499% if isnan(increment)
1500%     set(handles.increment_scan,'String','1')%default value
1501%     increment=1;
1502% end
1503errormsg=runpm(hObject,eventdata,handles,increment);
1504if ~isempty(errormsg)
1505    msgbox_uvmat('ERROR',errormsg);
1506end
1507set(handles.runmin,'BackgroundColor',[1 0 0])%paint the command button back to red
1508
1509%------------------------------------------------------------------------
1510% -- Executes on button press in Movie: make a series of +> steps
1511function Movie_Callback(hObject, eventdata, handles)
1512%------------------------------------------------------------------------
1513set(handles.Movie,'BackgroundColor',[1 1 0])%paint the command button in yellow
1514drawnow
1515increment=str2num(get(handles.increment_scan,'String')); %get the field increment d
1516% if isnan(increment)
1517%     set(handles.increment_scan,'String','1')%default value
1518%     increment=1;
1519% end
1520set(handles.STOP,'Visible','on')
1521set(handles.speed,'Visible','on')
1522set(handles.speed_txt,'Visible','on')
1523set(handles.Movie,'BusyAction','queue')
1524UvData=get(handles.uvmat,'UserData');
1525
1526while get(handles.speed,'Value')~=0 && isequal(get(handles.Movie,'BusyAction'),'queue') % enable STOP command
1527        errormsg=runpm(hObject,eventdata,handles,increment);
1528        if ~isempty(errormsg)
1529            set(handles.Movie,'BackgroundColor',[1 0 0])%paint the command buttonback to red
1530            return
1531        end
1532        pause(1.02-get(handles.speed,'Value'))% wait for next image
1533end
1534if isfield(UvData,'aviobj') && ~isempty( UvData.aviobj),
1535    UvData.aviobj=close(UvData.aviobj);
1536   set(handles.uvmat,'UserData',UvData);
1537end
1538set(handles.Movie,'BackgroundColor',[1 0 0])%paint the command buttonback to red
1539
1540%------------------------------------------------------------------------
1541% -- Executes on button press in Movie: make a series of <- steps
1542function MovieBackward_Callback(hObject, eventdata, handles)
1543%------------------------------------------------------------------------
1544set(handles.MovieBackward,'BackgroundColor',[1 1 0])%paint the command button in yellow
1545drawnow
1546increment=-str2num(get(handles.increment_scan,'String')); %get the field increment d
1547set(handles.STOP,'Visible','on')
1548set(handles.speed,'Visible','on')
1549set(handles.speed_txt,'Visible','on')
1550set(handles.MovieBackward,'BusyAction','queue')
1551UvData=get(handles.uvmat,'UserData');
1552
1553while get(handles.speed,'Value')~=0 && isequal(get(handles.MovieBackward,'BusyAction'),'queue') % enable STOP command
1554        errormsg=runpm(hObject,eventdata,handles,increment);
1555        if ~isempty(errormsg)
1556            set(handles.MovieBackward,'BackgroundColor',[1 0 0])%paint the command buttonback to red
1557            return
1558        end
1559        pause(1.02-get(handles.speed,'Value'))% wait for next image
1560end
1561if isfield(UvData,'aviobj') && ~isempty( UvData.aviobj),
1562    UvData.aviobj=close(UvData.aviobj);
1563   set(handles.uvmat,'UserData',UvData);
1564end
1565set(handles.MovieBackward,'BackgroundColor',[1 0 0])%paint the command buttonback to red
1566
1567%------------------------------------------------------------------------
1568function STOP_Callback(hObject, eventdata, handles)
1569%------------------------------------------------------------------------
1570set(handles.movie_pair,'BusyAction','Cancel')
1571set(handles.movie_pair,'value',0)
1572set(handles.Movie,'BusyAction','Cancel')
1573set(handles.MovieBackward,'BusyAction','Cancel')
1574set(handles.MenuExportMovie,'BusyAction','Cancel')
1575set(handles.movie_pair,'BackgroundColor',[1 0 0])%paint the command buttonback to red
1576set(handles.Movie,'BackgroundColor',[1 0 0])%paint the command buttonback to red
1577set(handles.MovieBackward,'BackgroundColor',[1 0 0])%paint the command buttonback to red
1578
1579%------------------------------------------------------------------------
1580% --- function activated by runplus and run minus
1581function errormsg=runpm(hObject,eventdata,handles,increment)
1582%------------------------------------------------------------------------
1583errormsg='';%default
1584%% check for movie pair status
1585movie_status=get(handles.movie_pair,'Value');
1586if isequal(movie_status,1)
1587    STOP_Callback(hObject, eventdata, handles)%interrupt movie pair if active
1588end
1589
1590%% read the current input file name(s) and field indices
1591InputFile=read_GUI(handles.InputFile);
1592InputFile.RootFile=regexprep(InputFile.RootFile,'^[\\/]|[\\/]$','');%suppress possible / or \ separator at the beginning or the end of the string
1593InputFile.SubDir=regexprep(InputFile.SubDir,'^[\\/]|[\\/]$','');%suppress possible / or \ separator at the beginning or the end of the string
1594FileExt=InputFile.FileExt;
1595NomType=get(handles.NomType,'String');
1596i1=str2num(get(handles.i1,'String'));%read the field indices (for movie, it is not given by the file name)
1597i2=[];%default
1598if strcmp(get(handles.i2,'Visible'),'on')
1599    i2=str2num(get(handles.i2,'String'));
1600end
1601j1=[];
1602if strcmp(get(handles.j1,'Visible'),'on')
1603    j1=stra2num(get(handles.j1,'String'));
1604end
1605j2=j1;
1606if strcmp(get(handles.j2,'Visible'),'on')
1607    j2=stra2num(get(handles.j2,'String'));
1608end
1609sub_value= get(handles.SubField,'Value');
1610if sub_value % a second input file has been entered
1611    [FileName_1,RootPath_1,filebase_1,FileIndices_1,FileExt_1,SubDir_1]=read_file_boxes_1(handles);
1612    [tild,tild,tild,i1_1,i2_1,j1_1,j2_1]=fileparts_uvmat(FileIndices_1);
1613    NomType_1=get(handles.NomType_1,'String');
1614else
1615    filename_1=[];
1616end   
1617
1618%% increment (or decrement) the field indices and update the input filename(s)
1619CheckSearch=0;
1620if isempty(increment)
1621    CheckSearch=1;% search for the next available file
1622    set(handles.CheckFixPair,'Value',0)
1623end
1624CheckFixPair=get(handles.CheckFixPair,'Value')||(isempty(i2)&&isempty(j2));
1625if CheckFixPair
1626    if get(handles.scan_i,'Value')==1% case of scanning along index i
1627        i1=i1+increment;
1628        i2=i2+increment;
1629        if sub_value
1630            i1_1=i1_1+increment;
1631            i2_1=i2_1+increment;
1632        end
1633    else % case of scanning along index j (burst numbers)
1634        j1=j1+increment;
1635        j2=j2+increment;
1636        if sub_value
1637            j1_1=j1_1+increment;
1638            j2_1=j2_1+increment;
1639        end
1640    end
1641else
1642    UvData=get(handles.uvmat,'UserData');
1643    ref_i=i1;
1644    if ~isempty(i2)
1645        ref_i=floor((i1+i2)/2);
1646    end
1647    ref_j=1;
1648    if ~isempty(j1)
1649        ref_j=j1;
1650        if ~isempty(j2)
1651            ref_j=floor((j1+j2)/2);
1652        end
1653    end
1654    if ~isempty(increment)
1655        if get(handles.scan_i,'Value')==1% case of scanning along index i
1656            ref_i=ref_i+increment;
1657        else % case of scanning along index j (burst numbers)
1658            ref_j=ref_j+increment;
1659        end
1660    else % free increment
1661        if isequal(get(handles.runplus,'BackgroundColor'),[1 1 0])% if runplus is activated
1662            step=1;
1663        else
1664            step=-1;
1665        end
1666        if get(handles.scan_i,'Value')==1% case of scanning along index i
1667            ref_i=ref_i+step;
1668            while ref_i>=0  && size(UvData.i1_series{1},1)>=ref_i+1 && UvData.i1_series{1}(ref_i+1,ref_j+1,1)==0
1669                ref_i=ref_i+step;
1670            end
1671        else % case of scanning along index j (burst numbers)
1672            ref_j=ref_j+step;
1673            while ref_j>=0  && size(UvData.i1_series{1},2)>=ref_j+1 && UvData.i1_series{1}(ref_i+1,ref_j+1,1)==0
1674                ref_j=ref_j+step;
1675            end
1676        end
1677    end
1678    if ref_i<0
1679        errormsg='minimum i index reached';
1680    elseif ref_j<0
1681        errormsg='minimum j index reached';
1682    elseif ref_i+1>size(UvData.i1_series{1},1)
1683        errormsg='maximum i index reached';
1684    elseif ref_j+1>size(UvData.i1_series{1},2)
1685        errormsg='maximum j index reached';
1686    end
1687    if ~isempty(errormsg)
1688        return
1689    end
1690    if get(handles.scan_i,'Value')==1% case of scanning along index i
1691        i1_subseries=UvData.i1_series{1}(ref_i+1,ref_j+1,:);
1692    else
1693        i1_subseries=UvData.i1_series{1}(ref_i+1,ref_j+1,:);
1694    end
1695    i1_subseries=i1_subseries(i1_subseries>0);
1696    if isempty(i1_subseries)
1697        errormsg='no next file';
1698        return
1699    end
1700    i1=i1_subseries(end);
1701    if ~isempty(UvData.i2_series{1})
1702        if get(handles.scan_i,'Value')==1% case of scanning along index i
1703            i2_subseries=UvData.i2_series{1}(ref_i+1,ref_j+1,:);
1704        else
1705            i2_subseries=UvData.i2_series{1}(ref_i+1,ref_j+1,:);
1706        end
1707        i2_subseries=i2_subseries(i2_subseries>0);
1708        i2=i2_subseries(end);
1709    end
1710    if ~isempty(UvData.j1_series{1})
1711        if get(handles.scan_i,'Value')==1% case of scanning along index i
1712            j1_subseries=UvData.j1_series{1}(ref_i+1,ref_j+1,:);
1713        else
1714            j1_subseries=UvData.j1_series{1}(ref_i+1,ref_j+1,:);
1715        end
1716        j1_subseries=j1_subseries(j1_subseries>0);
1717        j1=j1_subseries(end);
1718    end
1719    if ~isempty(UvData.j2_series{1})
1720        if get(handles.scan_i,'Value')==1% case of scanning along index i
1721            j2_subseries=UvData.j2_series{1}(ref_i+1,ref_j+1,:);
1722        else
1723            j2_subseries=UvData.j2_series{1}(ref_i+1,ref_j+1,:);
1724        end
1725        j2_subseries=j2_subseries(j2_subseries>0);
1726        j2=j2_subseries(end);
1727    end
1728   
1729end
1730filename=fullfile_uvmat(InputFile.RootPath,InputFile.SubDir,InputFile.RootFile,FileExt,NomType,i1,i2,j1,j2);
1731if sub_value
1732    filename_1=fullfile_uvmat(InputFile.RootPath_1,InputFile.SubDir_1,InputFile.RootFile_1,FileExt_1,NomType_1,i1_1,i2_1,j1_1,j2_1);
1733end
1734
1735%% refresh plots
1736errormsg=refresh_field(handles,filename,filename_1,i1,i2,j1,j2);
1737
1738%% update the index counters if the index move is successfull
1739if isempty(errormsg)
1740    set(handles.i1,'String',num2stra(i1,NomType,1));
1741    if isequal(i2,i1)
1742        set(handles.i2,'String','');
1743    else
1744        set(handles.i2,'String',num2stra(i2,NomType,1));
1745    end
1746    set(handles.j1,'String',num2stra(j1,NomType,2));
1747    if isequal(j2,j1)
1748        set(handles.j2,'String','');
1749    else
1750        set(handles.j2,'String',num2stra(j2,NomType,2));
1751    end
1752   % [indices]=name_generator('',i1,j1,'',NomType,1,i2,j2,'');
1753    indices=fullfile_uvmat('','','','',NomType,i1,i2,j1,j2);
1754    set(handles.FileIndex,'String',indices);
1755    if ~isempty(filename_1)
1756        indices_1=fullfile_uvmat('','','','',NomType_1,i1_1,i2_1,j1_1,j2_1);
1757        %indices_1=name_generator('',i1_1,j1_1,'',NomType_1,1,i2_1,j2_1,'');
1758        set(handles.FileIndex_1,'String',indices_1);
1759    end
1760    if isequal(movie_status,1)
1761        set(handles.movie_pair,'Value',1)
1762        movie_pair_Callback(hObject, eventdata, handles); %reactivate moviepair if it was activated
1763    end
1764end
1765
1766%------------------------------------------------------------------------
1767% --- Executes on button press in movie_pair: create an alternating movie with two view
1768function movie_pair_Callback(hObject, eventdata, handles)
1769%------------------------------------------------------------------------
1770%% stop movie action if the movie_pair button is off
1771if ~get(handles.movie_pair,'value')
1772    set(handles.movie_pair,'BusyAction','Cancel')%stop movie pair if button is 'off'
1773    set(handles.i2,'String','')
1774    set(handles.j2,'String','')
1775    return
1776else
1777    set(handles.movie_pair,'BusyAction','queue')
1778end
1779
1780%% initialisation
1781set(handles.movie_pair,'BackgroundColor',[1 1 0])%paint the command button in yellow
1782drawnow
1783list_fields=get(handles.Fields,'String');% list menu fields
1784index_fields=get(handles.Fields,'Value');% selected string index
1785FieldName=list_fields{index_fields}; % selected field
1786UvData=get(handles.uvmat,'UserData');
1787if isequal(FieldName,'image')
1788    test_1=0;
1789    [RootPath,SubDir,RootFile,FileIndices,Ext]=read_file_boxes(handles);
1790    NomType=get(handles.NomType,'String');
1791else
1792    list_fields=get(handles.Fields_1,'String');% list menu fields
1793    index_fields=get(handles.Fields_1,'Value');% selected string index
1794    FieldName=list_fields{index_fields}; % selected field
1795    if isequal(FieldName,'image')
1796        test_1=1;
1797        [RootPath,tild,RootFile,FileIndex_1,Ext,NomType]=read_file_boxes_1(handles);
1798    else
1799        msgbox_uvmat('ERROR','an image or movie must be first introduced as input')
1800        set(handles.movie_pair,'BackgroundColor',[1 0 0])%paint the command button in red
1801        set(handles.movie_pair,'Value',0)
1802        return
1803    end
1804end
1805num_i1=str2num(get(handles.i1,'String'));
1806num_j1=stra2num(get(handles.j1,'String'));
1807num_i2=str2num(get(handles.i2,'String'));
1808num_j2=stra2num(get(handles.j2,'String'));
1809if isempty(num_j2)
1810    if isempty(num_i2)   
1811        msgbox_uvmat('ERROR', 'a second image index i2 or j2 is needed to show the pair as a movie')
1812        set(handles.movie_pair,'BackgroundColor',[1 0 0])%paint the command button in red
1813         set(handles.movie_pair,'Value',0)
1814        return
1815    else
1816        num_j2=num_j1;%repeat the index i1 by default
1817    end
1818end
1819if isempty(num_i2)
1820    num_i2=num_i1;%repeat the index i1 by default
1821end
1822% imaname_1=name_generator(filebase,num_i2,num_j2,Ext,NomType);
1823imaname_1=fullfile_uvmat(RootPath,'',RootFile,Ext,NomType,num_i2,[],num_j2);
1824if ~exist(imaname_1,'file')
1825      msgbox_uvmat('ERROR',['second input open (-)  ' imaname_1 ' not found']);
1826      set(handles.movie_pair,'BackgroundColor',[1 0 0])%paint the command button in red
1827       set(handles.movie_pair,'Value',0)
1828      return
1829end
1830
1831%% read the second image
1832Field.AName='image';
1833if test_1
1834    Field_a=UvData.Field_1;
1835else
1836    Field_a=UvData.Field;
1837end
1838Field_b.AX=Field_a.AX;
1839Field_b.AY=Field_a.AY;
1840% z index
1841nbslice=str2double(get(handles.nb_slice,'String'));
1842if ~isempty(nbslice)
1843    Field_b.ZIndex=mod(num_i2-1,nbslice)+1;
1844end
1845Field_b.CoordUnit='pixel';
1846
1847%% determine the input file type
1848if (test_1 && isfield(UvData,'MovieObject')&& numel(UvData.MovieObject>=2))||(~test_1 && ~isempty(UvData.MovieObject{1}))
1849    FileType='movie';
1850elseif isequal(lower(Ext),'.avi')
1851    FileType='avi';
1852elseif isequal(lower(Ext),'.vol')
1853    FileType='vol';
1854else
1855   form=imformats(Ext(2:end));
1856   if ~isempty(form)% if the extension corresponds to an image format recognized by Matlab
1857       if isequal(NomType,'*');
1858           FileType='multimage';
1859       else
1860           FileType='image';
1861       end
1862   end
1863end
1864switch FileType
1865        case 'movie'
1866            if test_1
1867                Field_b.A=read(UvData.MovieObject{2},num_i2);
1868            else
1869                Field_b.A=read(UvData.MovieObject{1},num_i2);
1870            end
1871        case 'avi'
1872            mov=aviread(imaname_1,num_i2);
1873            Field_b.A=frame2im(mov(1));
1874        case 'vol'
1875            Field_b.A=imread(imaname_1);
1876        case 'multimage'
1877            Field_b.A=imread(imaname_1,num_i2);
1878        case 'image'
1879            Field_b.A=imread(imaname_1);
1880end
1881if get(handles.slices,'Value')
1882    Field.ZIndex=str2double(get(handles.z_index,'String'));
1883end
1884
1885%px to phys or other transform on field
1886menu_transform=get(handles.transform_fct,'String');
1887choice_value=get(handles.transform_fct,'Value');
1888transform_name=menu_transform{choice_value};%name of the transform fct  given by the menu 'transform_fct'
1889transform_list=get(handles.transform_fct,'UserData');
1890transform=transform_list{choice_value};
1891if  ~isequal(transform_name,'') && ~isequal(transform_name,'px')
1892    if test_1 && isfield(UvData,'XmlData') && numel(UvData.XmlData)==2 && isfield(UvData.XmlData{2},'GeometryCalib')%use geometry calib recorded from the ImaDoc xml file as first priority
1893        Field_a=transform(Field_a,UvData.XmlData{2});%the first field has been stored without transform
1894        Field_b=transform(Field_b,UvData.XmlData{2});
1895    elseif ~test_1 && isfield(UvData,'XmlData') && isfield(UvData.XmlData{1},'GeometryCalib')%use geometry calib
1896        Field_b=transform(Field_b,UvData.XmlData{1});
1897    end
1898end
1899
1900 % make movie until movie speed is set to 0 or STOP is activated
1901hima=findobj(handles.axes3,'Tag','ima');% %handles.axes3 =main plotting window (A GENERALISER)
1902set(handles.STOP,'Visible','on')
1903set(handles.speed,'Visible','on')
1904set(handles.speed_txt,'Visible','on')
1905while get(handles.speed,'Value')~=0 && isequal(get(handles.movie_pair,'BusyAction'),'queue')%isequal(get(handles.run0,'BusyAction'),'queue'); % enable STOP command
1906    % read and plot the series of images in non erase mode
1907    set(hima,'CData',Field_b.A);
1908    pause(1.02-get(handles.speed,'Value'));% wait for next image
1909    set(hima,'CData',Field_a.A);
1910    pause(1.02-get(handles.speed,'Value'));% wait for next image
1911end
1912set(handles.movie_pair,'BackgroundColor',[1 0 0])%paint the command button in red
1913 set(handles.movie_pair,'Value',0)
1914
1915%------------------------------------------------------------------------
1916% --- Executes on button press in run0.
1917function run0_Callback(hObject, eventdata, handles)
1918%------------------------------------------------------------------------
1919set(handles.run0,'BackgroundColor',[1 1 0])%paint the command button in yellow
1920drawnow
1921[RootPath,SubDir,RootFile,FileIndices,FileExt]=read_file_boxes(handles);
1922filename=[fullfile(RootPath,SubDir,RootFile) FileIndices FileExt];
1923filename_1=[];%default
1924if get(handles.SubField,'Value')
1925    [RootPath_1,SubDir_1,RootFile_1,FileIndices_1,FileExt_1]=read_file_boxes_1(handles);
1926    filename_1=[fullfile(RootPath_1,SubDir_1,RootFile_1) FileIndices_1 FileExt_1];
1927end
1928num_i1=stra2num(get(handles.i1,'String'));
1929num_i2=stra2num(get(handles.i2,'String'));
1930num_j1=stra2num(get(handles.j1,'String'));
1931num_j2=stra2num(get(handles.j2,'String'));
1932
1933errormsg=refresh_field(handles,filename,filename_1,num_i1,num_i2,num_j1,num_j2);
1934
1935if ~isempty(errormsg)
1936      msgbox_uvmat('ERROR',errormsg);
1937else
1938    set(handles.i1,'BackgroundColor',[1 1 1])
1939    set(handles.i2,'BackgroundColor',[1 1 1])
1940    set(handles.j1,'BackgroundColor',[1 1 1])
1941    set(handles.j2,'BackgroundColor',[1 1 1])
1942    set(handles.FileIndex,'BackgroundColor',[1 1 1])
1943    set(handles.FileIndex_1,'BackgroundColor',[1 1 1])
1944end   
1945set(handles.run0,'BackgroundColor',[1 0 0])
1946
1947
1948%------------------------------------------------------------------------
1949% --- read the input files and refresh all the plots, including projection.
1950% OUTPUT:
1951%  errormsg: error message char string  =[] by default
1952% INPUT:
1953% filename: first input file (=[] in the absence of input file)
1954% filename_1: second input file (=[] in the asbsenc of secodn input file)
1955% num_i1,num_i2,num_j1,num_j2; frame indices
1956% Field: structure describing an optional input field (then replace the input file)
1957
1958function errormsg=refresh_field(handles,filename,filename_1,num_i1,num_i2,num_j1,num_j2,Field)
1959%------------------------------------------------------------------------
1960
1961%% initialisation
1962abstime=[];
1963abstime_1=[];
1964dt=[];
1965if ~exist('Field','var')
1966    Field={};
1967end
1968UvData=get(handles.uvmat,'UserData');
1969if ishandle(handles.UVMAT_title) %remove title panel on uvmat
1970    delete(handles.UVMAT_title)
1971end
1972
1973%% determine the main input file information for action
1974FileType=[];%default
1975if ~exist(filename,'file')
1976    errormsg=['input file ' filename ' does not exist'];
1977    return
1978end
1979NomType=get(handles.NomType,'String');
1980% NomType=get(handles.FileIndex,'UserData');
1981%update the z position index
1982nbslice_str=get(handles.nb_slice,'String');
1983if isequal(nbslice_str,'volume')%NOT USED
1984    z_index=num_j1;
1985    set(handles.z_index,'String',num2str(z_index))
1986else
1987    nbslice=str2num(nbslice_str);
1988    z_index=mod(num_i1-1,nbslice)+1;
1989    set(handles.z_index,'String',num2str(z_index))
1990end
1991% refresh menu for save_mask if relevant
1992masknumber=get(handles.masklevel,'String');
1993if length(masknumber)>=z_index
1994    set(handles.masklevel,'Value',z_index)
1995end
1996
1997%% read the first input field if a filename has been introduced
1998if ~isempty(filename)
1999    ObjectName=filename;
2000    FieldName='';%default
2001    VelType='';%default
2002%     FileType=UvData.FileType{1};
2003    switch UvData.FileType{1}
2004        case {'civx','civdata','netcdf'};
2005            list_fields=get(handles.Fields,'String');% list menu fields
2006  %          index_fields=get(handles.Fields,'Value');% selected string index
2007            FieldName= list_fields{get(handles.Fields,'Value')}; % selected field
2008            if ~strcmp(FieldName,'get_field...')
2009                if get(handles.FixVelType,'Value')
2010                    VelTypeList=get(handles.VelType,'String');
2011                    VelType=VelTypeList{get(handles.VelType,'Value')};
2012                end
2013            end
2014            if strcmp(FieldName,'velocity')
2015                list_code=get(handles.ColorCode,'String');% list menu fields
2016                index_code=get(handles.ColorCode,'Value');% selected string index
2017                if  ~strcmp(list_code{index_code},'black') &&  ~strcmp(list_code{index_code},'white')
2018                    list_code=get(handles.ColorScalar,'String');% list menu fields
2019                    index_code=get(handles.ColorScalar,'Value');% selected string index
2020                    ParamIn.ColorVar= list_code{index_code}; % selected field
2021                end
2022            end
2023        case 'video'
2024            ObjectName=UvData.MovieObject{1};         
2025        case 'vol' %TODO: update
2026            if isfield(UvData.XmlData,'Npy') && isfield(UvData.XmlData,'Npx')
2027                ParamIn.Npy=UvData.XmlData.Npy;
2028                ParamIn.Npx=UvData.XmlData.Npx;
2029            else           
2030                errormsg='Npx and Npy need to be defined in the xml file for volume images .vol';
2031                return
2032            end
2033    end
2034    ParamIn.FieldName=FieldName;
2035    ParamIn.VelType=VelType;
2036    ParamIn.GUIName='get_field';
2037    [Field{1},ParamOut,errormsg] = read_field(ObjectName,UvData.FileType{1},ParamIn,num_i1);
2038    if ~isempty(errormsg)
2039        errormsg=['error in reading ' filename ': ' errormsg];
2040        return
2041    end 
2042    if isfield(ParamOut,'Npx')&& isfield(ParamOut,'Npy')
2043        set(handles.num_Npx,'String',num2str(ParamOut.Npx));% display image size on the interface
2044        set(handles.num_Npy,'String',num2str(ParamOut.Npy));
2045    end
2046    if isfield(ParamOut,'TimeIndex')
2047        set(handles.i1,'String',num2str(ParamOut.TimeIndex))
2048    end
2049    if isfield(ParamOut,'TimeValue')
2050        Field{1}.Time=ParamOut.TimeValue;
2051    end
2052end
2053
2054%% choose and read a second field filename_1 if defined
2055VelType_1=[];%default
2056FieldName_1=[];
2057ParamOut_1=[];
2058if ~isempty(filename_1)
2059    if ~exist(filename_1,'file')
2060        errormsg=['second file ' filename_1 ' does not exist'];
2061        return
2062    end
2063    Name=filename_1;
2064    switch UvData.FileType{2}
2065        case {'civx','civdata','netcdf'};
2066            list_fields=get(handles.Fields_1,'String');% list menu fields
2067            FieldName_1= list_fields{get(handles.Fields_1,'Value')}; % selected field
2068            if ~strcmp(FieldName,'get_field...')
2069                if get(handles.FixVelType,'Value')
2070                    VelTypeList=get(handles.VelType_1,'String');
2071                    VelType_1=VelTypeList{get(handles.VelType_1,'Value')};% read the velocity type.
2072                end
2073            end
2074            if strcmp(FieldName_1,'velocity')&& strcmp(get(handles.ColorCode,'Visible'),'on')
2075                list_code=get(handles.ColorCode,'String');% list menu fields
2076                index_code=get(handles.ColorCode,'Value');% selected string index
2077                if  ~strcmp(list_code{index_code},'black') &&  ~strcmp(list_code{index_code},'white')
2078                    list_code=get(handles.ColorScalar,'String');% list menu fields
2079                    index_code=get(handles.ColorScalar,'Value');% selected string index
2080                    ParamIn_1.ColorVar= list_code{index_code}; % selected field for vector color display                 
2081                end
2082            end
2083        case 'video'
2084            Name=UvData.MovieObject{2};
2085        case 'vol' %TODO: update
2086            if isfield(UvData.XmlData,'Npy') && isfield(UvData.XmlData,'Npx')
2087                ParamIn_1.Npy=UvData.XmlData.Npy;
2088                ParamIn_1.Npx=UvData.XmlData.Npx;
2089            else
2090                errormsg='Npx and Npy need to be defined in the xml file for volume images .vol';
2091                return
2092            end
2093    end
2094    NomType_1=get(handles.NomType_1,'String');
2095    test_keepdata_1=0;% test for keeping the previous stored data if the input files are unchanged
2096    if ~isequal(NomType_1,'*')%in case of a series of files (not avi movie)
2097        if isfield(UvData,'filename_1')%&& isfield(UvData,'VelType_1') && isfield(UvData,'FieldName_1')
2098            test_keepdata_1= strcmp(filename_1,UvData.filename_1) ;%&& strcmp(FieldName_1,UvData.FieldName_1);
2099        end
2100    end
2101    if test_keepdata_1
2102        Field{2}=UvData.Field_1;% keep the stored field
2103    else
2104        ParamIn_1.FieldName=FieldName_1;
2105        ParamIn_1.VelType=VelType_1;
2106        ParamIn_1.GUIName='get_field_1';
2107        [Field{2},ParamOut_1,errormsg] = read_field(Name,UvData.FileType{2},ParamIn_1,num_i1);
2108        if ~isempty(errormsg)
2109            errormsg=['error in reading ' FieldName_1 ' in ' filename_1 ': ' errormsg];
2110            return
2111        end
2112%         UvData.Field_1=Field{2}; %store the second field for possible use at next RUN
2113    end
2114end
2115
2116%% update uvmat interface
2117if isfield(ParamOut,'Npx')
2118    set(handles.num_Npx,'String',num2str(ParamOut.Npx));% display image size on the interface
2119    set(handles.num_Npy,'String',num2str(ParamOut.Npy));
2120elseif isfield(ParamOut_1,'Npx')
2121    set(handles.num_Npx,'String',num2str(ParamOut_1.Npx));% display image size on the interface
2122    set(handles.num_Npy,'String',num2str(ParamOut_1.Npy));
2123end
2124
2125%% update the display menu for the first velocity type (first menuline)
2126test_veltype=0;
2127% if ~isequal(FileType,'netcdf')|| isequal(FieldName,'get_field...')
2128if (strcmp(UvData.FileType{1},'civx')||strcmp(UvData.FileType{1},'civdata'))&& ~strcmp(FieldName,'get_field...')
2129    test_veltype=1;
2130    set(handles.VelType,'Visible','on')
2131    set(handles.VelType_1,'Visible','on')
2132    set(handles.FixVelType,'Visible','on')
2133    menu=set_veltype_display(ParamOut.CivStage,UvData.FileType{1});
2134    index_menu=strcmp(ParamOut.VelType,menu);%look for VelType in  the menu
2135    index_val=find(index_menu,1);
2136    if isempty(index_val)
2137        index_val=1;
2138    end
2139    set(handles.VelType,'Value',index_val)
2140    if ~get(handles.SubField,'value')
2141        set(handles.VelType,'String',menu)
2142        set(handles.VelType_1,'Value',1)
2143        set(handles.VelType_1,'String',[{''};menu])
2144    end
2145else
2146    set(handles.VelType,'Visible','off')
2147end
2148% display the Fields menu from the input file and pick the selected one:
2149field_index=strcmp(ParamOut.FieldName,ParamOut.FieldList);
2150set(handles.Fields,'String',ParamOut.FieldList); %update the field menu
2151set(handles.Fields,'Value',find(field_index,1))
2152
2153%% update the display menu for the second velocity type (second menuline)
2154
2155test_veltype_1=0;
2156if isempty(filename_1)
2157    set(handles.Fields_1,'Value',1); %update the field menu
2158    set(handles.Fields_1,'String',[{''};ParamOut.FieldList]); %update the field menu
2159elseif ~test_keepdata_1
2160    if (~strcmp(UvData.FileType{2},'netcdf')&&~strcmp(UvData.FileType{2},'civdata')&&~strcmp(UvData.FileType{2},'civx'))|| isequal(FieldName_1,'get_field...')
2161        set(handles.VelType_1,'Visible','off')
2162    else
2163        test_veltype_1=1;
2164        set(handles.VelType_1,'Visible','on')
2165%         if ~get(handles.FixVelType,'Value')
2166            menu=set_veltype_display(ParamOut_1.CivStage);
2167            index_menu=strcmp(ParamOut_1.VelType,menu);
2168            set(handles.VelType_1,'Value',1+find(index_menu,1))
2169            set(handles.VelType_1,'String',[{''};menu])
2170%         end
2171    end
2172    % update the second field menu: the same quantity
2173    set(handles.Fields_1,'String',[{''};ParamOut_1.FieldList]); %update the field menu
2174    % display the Fields menu from the input file and pick the selected one:
2175    field_index=strcmp(ParamOut_1.FieldName,ParamOut_1.FieldList);
2176    set(handles.Fields_1,'Value',find(field_index,1)+1) 
2177%     % synchronise  with the first menu if the first selection is not 'velocity'
2178%     if ~strcmp(ParamOut.FieldName,'velocity')
2179%         field_index=strcmp(ParamOut_1.FieldName,ParamOut_1.FieldList);
2180%         set(handles.Fields_1,'Value',field_index); %update the field menu
2181%         ParamOut_1.FieldName=ParamOut.FieldName;
2182%         set(handles.Fields_1,'String',ParamOut_1.FieldList)
2183%     end
2184end
2185if test_veltype||test_veltype_1
2186     set(handles.FixVelType,'Visible','on')
2187else
2188     set(handles.FixVelType,'Visible','off')
2189end
2190
2191% field_index=strcmp(ParamOut_1.FieldName,ParamOut_1.FieldList);
2192% set(handles.Fields,'String',ParamOut.FieldList); %update the field menu
2193% set(handles.Fields,'Value',find(field_index,1))
2194   
2195%% introduce w as background image by default for a new series (only for nbdim=2)
2196if ~isfield(UvData,'NewSeries')
2197    UvData.NewSeries=1;
2198end
2199%put W as background image by default if NbDim=2:
2200if  UvData.NewSeries && isequal(get(handles.SubField,'Value'),0) && isfield(Field{1},'W') && ~isempty(Field{1}.W) && ~isequal(Field{1}.NbDim,3);
2201        set(handles.SubField,'Value',1);
2202        set(handles.RootPath_1,'String','"')
2203        set(handles.RootFile_1,'String','"')
2204        set(handles.SubDir_1,'String','"');
2205         indices=fullfile_uvmat('','','','',NomType,num_i1,num_i2,num_j1,num_j2);
2206        set(handles.FileIndex_1,'String',indices)
2207        set(handles.FileExt_1,'String','"');
2208        set(handles.Fields_1,'Visible','on');
2209        set(handles.Fields_1,'Visible','on');
2210        set(handles.RootPath_1,'Visible','on')
2211        set(handles.RootFile_1,'Visible','on')
2212        set(handles.SubDir_1,'Visible','on');
2213        set(handles.FileIndex_1,'Visible','on');
2214        set(handles.FileExt_1,'Visible','on');
2215        set(handles.Fields_1,'Visible','on');
2216        Field{1}.AName='w';
2217end           
2218
2219%% store the current open names, fields and vel types in uvmat interface
2220UvData.filename_1=filename_1;
2221
2222%% apply coordinate transform or other user fct
2223XmlData=[];%default
2224XmlData_1=[];%default
2225if isfield(UvData,'XmlData')%use geometry calib recorded from the ImaDoc xml file as first priority
2226    XmlData=UvData.XmlData{1};
2227    if numel(UvData.XmlData)==2
2228        XmlData_1=UvData.XmlData{2};
2229    end
2230end
2231choice_value=get(handles.transform_fct,'Value');
2232transform_list=get(handles.transform_fct,'UserData');
2233transform=transform_list{choice_value};%selected function handles
2234% z index
2235if ~isempty(filename)
2236    Field{1}.ZIndex=z_index;
2237end
2238if ~isempty(transform)
2239    if length(Field)>=2
2240        Field{2}.ZIndex=z_index;
2241        [Field{1},Field{2}]=transform(Field{1},XmlData,Field{2},XmlData_1);
2242        if isempty(Field{2})
2243            Field(2)=[];
2244        end
2245    else
2246        Field{1}=transform(Field{1},XmlData);
2247    end
2248    %% update tps in phys coordinates if needed
2249    if (strcmp(VelType,'filter1')||strcmp(VelType,'filter2'))&& strcmp(UvData.FileType{1},'civdata')&&isfield(Field{1},'U')&& isfield(Field{1},'V')
2250        Field{1}.X=Field{1}.X(Field{1}.FF==0);
2251        Field{1}.Y=Field{1}.Y(Field{1}.FF==0);
2252        Field{1}.U=Field{1}.U(Field{1}.FF==0);
2253        Field{1}.V=Field{1}.V(Field{1}.FF==0);
2254        [Field{1}.SubRange,Field{1}.NbSites,Field{1}.Coord_tps,Field{1}.U_tps,Field{1}.V_tps]=filter_tps([Field{1}.X Field{1}.Y],Field{1}.U,Field{1}.V,[],Field{1}.Patch1_SubDomain,0);
2255    end
2256    if numel(Field)==2 && ~test_keepdata_1 && isequal(UvData.FileType{2}(1:3),'civ') && ~isequal(ParamOut_1.FieldName,'get_field...')%&&~isempty(FieldName_1)
2257        %update tps in phys coordinates if needed
2258        if (strcmp(VelType_1,'filter1')||strcmp(VelType_1,'filter2'))&& strcmp(UvData.FileType{2},'civdata')&&isfield(Field{2},'U')&& isfield(Field{2},'V')
2259            Field{2}.X=Field{2}.X(Field{2}.FF==0);
2260            Field{2}.Y=Field{1}.Y(Field{2}.FF==0);
2261            Field{2}.U=Field{1}.U(Field{2}.FF==0);
2262            Field{2}.V=Field{1}.V(Field{2}.FF==0);
2263            [Field{2}.SubRange,Field{2}.NbSites,Field{2}.Coord_tps,Field{2}.U_tps,Field{2}.V_tps]=filter_tps([Field{2}.X Field{2}.Y],Field{2}.U,Field{2}.V,[],1500,0);
2264        end
2265    end
2266end
2267
2268%% calculate scalar
2269if (strcmp(UvData.FileType{1},'civdata')||strcmp(UvData.FileType{1},'civx'))%&&~strcmp(ParamOut.FieldName,'velocity')&& ~strcmp(ParamOut.FieldName,'get_field...');% ~isequal(ParamOut.CivStage,0)%&&~isempty(FieldName)%
2270    Field{1}=calc_field([{ParamOut.FieldName} {ParamOut.ColorVar}],Field{1});
2271end
2272if numel(Field)==2 && ~test_keepdata_1 && (strcmp(UvData.FileType{2},'civdata')||strcmp(UvData.FileType{2},'civx'))  &&~strcmp(ParamOut_1.FieldName,'velocity') && ~strcmp(ParamOut_1.FieldName,'get_field...')
2273     Field{2}=calc_field([{ParamOut_1.FieldName} {ParamOut_1.ColorVar}],Field{2});
2274end
2275
2276%% combine the two input fields (e.g. substract velocity fields)
2277%Field{1}.FieldList=[{ParamOut.FieldName} {ParamOut.ColorVar}];
2278if numel(Field)==2
2279%    Field{2}.FieldList=[{ParamOut_1.FieldName} {ParamOut_1.ColorVar}];
2280   [UvData.Field,errormsg]=sub_field(Field{1},Field{2}); 
2281   UvData.Field_1=Field{2}; %store the second field for possible use at next RUN
2282else
2283   UvData.Field=Field{1};
2284end
2285if ~isempty(errormsg)
2286    errormsg=['error in uvmat/refresh_field/sub_field:' errormsg];
2287    return
2288end
2289%UvData.Field.FieldList={FieldName}; % TODO: to generalise, used for proj_field with tps interpolation
2290
2291
2292%% get bounds and mesh (needed for mouse action and to open set_object)
2293test_x=0;
2294test_z=0;% test for unstructured z coordinate
2295[errormsg,ListDimName,DimValue,VarDimIndex]=check_field_structure(UvData.Field);
2296if ~isempty(errormsg)
2297    errormsg=['error in uvmat/refresh_field/check_field_structure: ' errormsg];
2298    return
2299end
2300[CellVarIndex,NbDim,VarType,errormsg]=find_field_indices(UvData.Field);
2301if ~isempty(errormsg)
2302    errormsg=['error in uvmat/refresh_field/find_field_indices: ' errormsg];
2303    return
2304end
2305[NbDim,imax]=max(NbDim);
2306if isfield(UvData.Field,'NbDim')
2307    NbDim=UvData.Field.NbDim;% deal with plane fields containing z coordinates
2308end
2309if ~isempty(VarType{imax}.coord_x)  && ~isempty(VarType{imax}.coord_y)    %unstructured coordinates
2310    XName=UvData.Field.ListVarName{VarType{imax}.coord_x};
2311    YName=UvData.Field.ListVarName{VarType{imax}.coord_y};
2312    eval(['nbvec=length(UvData.Field.' XName ');'])%nbre of measurement points (e.g. vectors)
2313    test_x=1;%test for unstructured coordinates
2314    if ~isempty(VarType{imax}.coord_z)
2315        ZName=UvData.Field.ListVarName{VarType{imax}.coord_z};
2316    else
2317        NbDim=2;
2318    end
2319elseif numel(VarType)>=imax && numel(VarType{imax}.coord)>=NbDim && VarType{imax}.coord(NbDim)>0 %structured coordinate
2320    XName=UvData.Field.ListVarName{VarType{imax}.coord(NbDim)};
2321    if NbDim>1
2322        YName=UvData.Field.ListVarName{VarType{imax}.coord(NbDim-1)}; %structured coordinates
2323    end
2324end
2325if NbDim==3
2326    if ~test_x
2327        ZName=UvData.Field.ListVarName{VarType{imax}.coord(1)};%structured coordinates in 3D
2328    end
2329    eval(['ZMax=max(UvData.Field.' ZName ');'])
2330    eval(['ZMin=min(UvData.Field.' ZName ');'])
2331    UvData.Field.ZMax=ZMax;
2332    UvData.Field.ZMin=ZMin;
2333    test_z=1;
2334    if isequal(ZMin,ZMax)%no z dependency
2335        NbDim=2;
2336        test_z=0;
2337    end
2338end
2339if exist('XName','var')
2340    eval(['XMax=max(max(UvData.Field.' XName '));'])
2341    eval(['XMin=min(min(UvData.Field.' XName '));'])
2342    UvData.Field.NbDim=NbDim;
2343    UvData.Field.XMax=XMax;
2344    UvData.Field.XMin=XMin;
2345    if NbDim >1
2346        eval(['YMax=max(max(UvData.Field.' YName '));'])
2347        eval(['YMin=min(min(UvData.Field.' YName '));'])
2348        UvData.Field.YMax=YMax;
2349        UvData.Field.YMin=YMin;
2350    end
2351    eval(['nbvec=length(UvData.Field.' XName ');'])
2352    if test_x %unstructured coordinates
2353        if test_z
2354            UvData.Field.Mesh=((XMax-XMin)*(YMax-YMin)*(ZMax-ZMin))/nbvec;% volume per vector
2355            UvData.Field.Mesh=(UvData.Field.Mesh)^(1/3);
2356        else
2357            UvData.Field.Mesh=sqrt((XMax-XMin)*(YMax-YMin)/nbvec);%2D
2358        end
2359    else
2360        VarIndex=CellVarIndex{imax}; % list of variable indices
2361        DimIndex=VarDimIndex{VarIndex(1)}; %list of dim indices for the variable
2362        nbpoints_x=DimValue(DimIndex(NbDim));
2363        DX=(XMax-XMin)/(nbpoints_x-1);
2364        if NbDim >1
2365            nbpoints_y=DimValue(DimIndex(NbDim-1));
2366            DY=(YMax-YMin)/(nbpoints_y-1);
2367        end
2368        if NbDim==3
2369            nbpoints_z=DimValue(DimIndex(1));
2370            DZ=(ZMax-ZMin)/(nbpoints_z-1);
2371            UvData.Field.Mesh=(DX*DY*DZ)^(1/3);
2372            UvData.Field.ZMax=ZMax;
2373            UvData.Field.ZMin=ZMin;
2374        else
2375            UvData.Field.Mesh=DX;%sqrt(DX*DY);
2376        end
2377    end
2378    % adjust the mesh to a value 1, 2 , 5 *10^n
2379    ord=10^(floor(log10(UvData.Field.Mesh)));%order of magnitude
2380    if UvData.Field.Mesh/ord>=5
2381        UvData.Field.Mesh=5*ord;
2382    elseif UvData.Field.Mesh/ord>=2
2383        UvData.Field.Mesh=2*ord;
2384    else
2385        UvData.Field.Mesh=ord;
2386    end
2387end
2388
2389%% 3D case (menuvolume)
2390if NbDim==3% && UvData.NewSeries
2391    test_set_object=1;
2392    hset_object=findobj(allchild(0),'tag','set_object');% look for the set_object GUI
2393    ZBounds(1)=UvData.Field.ZMin; %minimum for the Z slider
2394    ZBounds(2)=UvData.Field.ZMax;%maximum for the Z slider
2395    if ~isempty(hset_object) %if set_object is detected
2396          delete(hset_object);% delete the GUI set_object if it does not fit
2397    end
2398    if test_set_object% reinitiate the GUI set_object
2399        delete_object(1);% delete the current projection object in the list UvData.Object, delete its graphic representations and update the list displayed in handles.ListObject and 2
2400        UvData.Object{1}.Type='plane';%main plotting plane
2401        UvData.Object{1}.ProjMode='projection';%main plotting plane
2402        UvData.Object{1}.DisplayHandle_uvmat=[]; %plane not visible in uvmat
2403        UvData.Object{1}.NbDim=NbDim;%test for 3D objects
2404        UvData.Object{1}.RangeZ=UvData.Field.Mesh;%main plotting plane
2405        UvData.Object{1}.Coord(1,3)=(UvData.Field.ZMin+UvData.Field.ZMax)/2;%section at a middle plane chosen
2406        UvData.Object{1}.Angle=[0 0 0];
2407        UvData.Object{1}.HandlesDisplay=plot(0,0,'Tag','proj_object');% A REVOIR
2408        UvData.Object{1}.Name='1-PLANE';
2409        UvData.Object{1}.enable_plot=1;
2410        set_object(UvData.Object{1},handles,ZBounds);
2411        set(handles.ListObject,'Value',1);
2412        set(handles.ListObject,'String',{'1-PLANE'});
2413        set(handles.edit_object,'Value',1)% put the plane in edit mode to enable the z cursor
2414        edit_object_Callback([],[], handles)
2415    end
2416    %multilevel case (single menuplane in a 3D space)
2417elseif isfield(UvData,'Z')
2418    if isfield(UvData,'CoordType')&& isequal(UvData.CoordType,'phys') && isfield(UvData,'XmlData')
2419        XmlData=UvData.XmlData{1};
2420        if isfield(XmlData,'PlanePos')
2421            UvData.Object{1}.Coord=XmlData.PlanePos(UvData.ZIndex,:);
2422        end
2423        if isfield(XmlData,'PlaneAngle')
2424            siz=size(XmlData.PlaneAngle);
2425            indangle=min(siz(1),UvData.ZIndex);%take first angle if a single angle is defined (translating scanning)
2426            UvData.Object{1}.PlaneAngle=XmlData.PlaneAngle(indangle,:);
2427        end
2428    elseif isfield(UvData,'ZIndex')
2429        UvData.Object{1}.ZObject=UvData.ZIndex;
2430    end
2431else
2432%     % create a default projection
2433%     UvData.Object{1}.ProjMode='projection';%main plotting plane
2434%     UvData.Object{1}.DisplayHandle_uvmat=[]; %plane not visible in uvmat
2435%     set(handles.ListObject,'Value',1);
2436%     list_object=get(handles.ListObject,'String');
2437%     if isempty(list_object)
2438%         list_object={''};
2439%     elseif ~isempty(list_object{1})
2440%         list_object=[{''};list_object];
2441%     end
2442%     set(handles.ListObject,'String',list_object);
2443% %     set(handles.list_object_2,'String',list_object);
2444end
2445testnewseries=UvData.NewSeries;
2446UvData.NewSeries=0;% put to 0 the test for a new field series (set by RootPath_callback)
2447set(handles.uvmat,'UserData',UvData)
2448
2449%% reset the min and max of scalar if only the mask is displayed(TODO: check the need)
2450% if isfield(UvData,'Mask')&& ~isfield(UvData,'A')
2451%     set(handles.num_MinA,'String','0')
2452%     set(handles.num_MaxA,'String','255')
2453% end
2454
2455%% Plot the projections on the selected  projection objects
2456% main projection object (uvmat display)
2457list_object=get(handles.ListObject,'String');
2458if isequal(list_object,{''})%refresh list of objects if the menu is empty
2459    UvData.Object={[]};
2460    set(handles.ListObject,'Value',1)
2461end
2462IndexObj=get(handles.ListObject,'Value');%selected projection object for main view
2463if IndexObj(1)> numel(UvData.Object)
2464    IndexObj(1)=1;%select the first object if the selected one does not exist
2465    set(handles.ListObject,'Value',1)
2466end
2467plot_handles{1}=handles;
2468if isfield(UvData,'plotaxes')%case of movies
2469    haxes(1)=UvData.plotaxes;
2470else
2471    haxes(1)=handles.axes3;
2472end
2473%PlotParam{1}=read_plot_param(handles);%read plotting parameters on the uvmat interfac
2474PlotParam{1}=read_GUI(handles.uvmat);
2475%default settings if vectors not visible (should not be needed)
2476if ~isfield(PlotParam{1},'Vectors')
2477    PlotParam{1}.Vectors.MaxVec=1;
2478    PlotParam{1}.Vectors.MinVec=0;
2479    PlotParam{1}.Vectors.CheckFixVecColor=1;
2480    PlotParam{1}.Vectors.ColCode1=0.33;
2481    PlotParam{1}.Vectors.ColCode2=0.66;
2482    PlotParam{1}.Vectors.ColorScalar={'ima_cor'};
2483    PlotParam{1}.Vectors.ColorCode= {'rgb'};
2484end
2485%keeplim(1)=get(handles.CheckFixLimits,'Value');% test for fixed graph limits
2486PosColorbar{1}=UvData.OpenParam.PosColorbar;%prescribe the colorbar position on the uvmat interface
2487
2488% second projection object (view_field display)
2489if length( IndexObj)>=2
2490    view_field_handle=findobj(allchild(0),'tag','view_field');%handles of the view_field GUI
2491    if ~isempty(view_field_handle)
2492        plot_handles{2}=guidata(view_field_handle);
2493        haxes(2)=plot_handles{2}.axes3;
2494        PlotParam{2}=read_GUI(handles.uvmat);%read plotting parameters on the uvmat interface
2495        PosColorbar{2}='*'; %TODO: deal with colorbar position on view_field
2496    end
2497end
2498
2499%% loop on the projection objects: one or two
2500for imap=1:numel(IndexObj)
2501    iobj=IndexObj(imap);
2502%      if imap==2 || check_proj==0  % field not yet projected) && ~isfield(UvData.Object{iobj},'Type')% case with no projection (only for the first empty object)
2503% %          [ObjectData,errormsg]=calc_field(UvData.Field.FieldList,UvData.Field);
2504% %      else
2505        [ObjectData,errormsg]=proj_field(UvData.Field,UvData.Object{iobj});% project field on the object
2506%      end
2507    if ~isempty(errormsg)
2508        return
2509    end
2510    %     if testnewseries && isfield(ObjectData,'CoordUnit')&& isfield(PlotParam{imap},'Coordinates')
2511    %         PlotParam{imap}.Coordinates=rmfield(PlotParam{imap}.Coordinates,'CheckFixEqual'); %set FixEqual to depend on the field (=1 if Data.CoordUnit=1 in plot_field)
2512    %     end
2513    if testnewseries && isfield(ObjectData,'CoordUnit')
2514        PlotParam{imap}.Coordinates.CheckFixEqual=1;
2515    end
2516    %use of mask (TODO: check)
2517    if isfield(ObjectData,'NbDim') && isequal(ObjectData.NbDim,2) && isfield(ObjectData,'Mask') && isfield(ObjectData,'A')
2518        flag_mask=double(ObjectData.Mask>200);%=0 for masked regions
2519        AX=ObjectData.AX;%x coordiantes for the scalar field
2520        AY=ObjectData.AY;%y coordinates for the scalar field
2521        MaskX=ObjectData.MaskX;%x coordiantes for the mask
2522        MaskY=ObjectData.MaskY;%y coordiantes for the mask
2523        if ~isequal(MaskX,AX)||~isequal(MaskY,AY)
2524            nxy=size(flag_mask);
2525            sizpx=(ObjectData.MaskX(end)-ObjectData.MaskX(1))/(nxy(2)-1);%size of a mask pixel
2526            sizpy=(ObjectData.MaskY(1)-ObjectData.MaskY(end))/(nxy(1)-1);
2527            x_mask=ObjectData.MaskX(1):sizpx:ObjectData.MaskX(end); % pixel x coordinates for image display
2528            y_mask=ObjectData.MaskY(1):-sizpy:ObjectData.MaskY(end);% pixel x coordinates for image display
2529            %project on the positions of the scalar
2530            npxy=size(ObjectData.A);
2531            dxy(1)=(ObjectData.AY(end)-ObjectData.AY(1))/(npxy(1)-1);%grid mesh in y
2532            dxy(2)=(ObjectData.AX(end)-ObjectData.AX(1))/(npxy(2)-1);%grid mesh in x
2533            xi=ObjectData.AX(1):dxy(2):ObjectData.AX(end);
2534            yi=ObjectData.AY(1):dxy(1):ObjectData.AY(end);
2535            [XI,YI]=meshgrid(xi,yi);% creates the matrix of regular coordinates
2536            flag_mask = interp2(x_mask,y_mask,flag_mask,XI,YI);
2537        end
2538        AClass=class(ObjectData.A);
2539        ObjectData.A=flag_mask.*double(ObjectData.A);
2540        ObjectData.A=feval(AClass,ObjectData.A);
2541        ind_off=[];
2542        if isfield(ObjectData,'ListVarName')
2543            for ilist=1:length(ObjectData.ListVarName)
2544                if isequal(ObjectData.ListVarName{ilist},'Mask')||isequal(ObjectData.ListVarName{ilist},'MaskX')||isequal(ObjectData.ListVarName{ilist},'MaskY')
2545                    ind_off=[ind_off ilist];
2546                end
2547            end
2548            ObjectData.ListVarName(ind_off)=[];
2549            VarDimIndex(ind_off)=[];
2550            ind_off=[];
2551            for ilist=1:length(ListDimName)
2552                if isequal(ListDimName{ilist},'MaskX') || isequal(ListDimName{ilist},'MaskY')
2553                    ind_off=[ind_off ilist];
2554                end
2555            end
2556            ListDimName(ind_off)=[];
2557            DimValue(ind_off)=[];
2558        end
2559    end
2560    if ~isempty(ObjectData)
2561       
2562        PlotType='none'; %default
2563        if imap==2 && isempty(view_field_handle)
2564            view_field(ObjectData)
2565        else
2566            [PlotType,PlotParamOut]=plot_field(ObjectData,haxes(imap),PlotParam{imap},PosColorbar{imap});
2567            write_plot_param(plot_handles{imap},PlotParamOut) %update the auto plot parameters
2568            if isfield(Field,'Mesh')&&~isempty(Field.Mesh)
2569                ObjectData.Mesh=Field.Mesh; % gives an estimated mesh size (useful for mouse action on the plot)
2570            end
2571        end
2572        if isequal(PlotType,'none')
2573            hget_field=findobj(allchild(0),'name','get_field');
2574            if isempty(hget_field)
2575                get_field(filename)% the projected field cannot be automatically plotted: use get_field to specify the variablesdelete(hget_field)
2576            end
2577            errormsg='The field defined by get_field cannot be plotted';
2578            return
2579        end
2580    end
2581end
2582
2583%% update the mask
2584if isequal(get(handles.CheckMask,'Value'),1)%if the mask option is on
2585   update_mask(handles,num_i1,num_i2);
2586end
2587
2588%% prepare the menus of histograms and plot them (histogram of the whole volume in 3D case)
2589menu_histo=(UvData.Field.ListVarName)';%list of field variables to be displayed for the menu of histogram display
2590ind_bad=[];
2591nb_histo=1;
2592
2593% suppress coordinates from the histogram menu
2594for ivar=1:numel(menu_histo)%l loop on field variables:
2595    if isfield(UvData.Field,'VarAttribute') && numel(UvData.Field.VarAttribute)>=ivar && isfield(UvData.Field.VarAttribute{ivar},'Role')
2596        Role=UvData.Field.VarAttribute{ivar}.Role;
2597        switch Role
2598            case {'coord_x','coord_y','coord_z','dimvar'}
2599                ind_bad=[ind_bad ivar];
2600            case {'vector_y'}
2601                nb_histo=nb_histo+1;
2602        end
2603    end
2604    DimCell=UvData.Field.VarDimName{ivar};
2605    DimName='';
2606    if ischar(DimCell)
2607        DimName=DimCell;
2608    elseif iscell(DimCell)&& numel(DimCell)==1
2609        DimName=DimCell{1};
2610    end
2611    if strcmp(DimName,menu_histo{ivar})
2612        ind_bad=[ind_bad ivar];
2613    end
2614end
2615menu_histo(ind_bad)=[];
2616
2617%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2618% display menus and plot histograms
2619test_v=0;
2620if ~isempty(menu_histo)
2621    set(handles.histo1_menu,'Value',1)
2622    set(handles.histo1_menu,'String',menu_histo)
2623    histo1_menu_Callback(handles.histo1_menu, [], handles)% plot first histogram
2624    % case of more than one variables (eg vector components)
2625    if nb_histo > 1
2626        test_v=1;
2627        set(handles.histo2_menu,'Visible','on')
2628        set(handles.histo_v,'Visible','on')
2629        set(handles.histo2_menu,'String',menu_histo)
2630        set(handles.histo2_menu,'Value',2)
2631        histo2_menu_Callback(handles.histo2_menu,[], handles)% plot second histogram
2632    end
2633end
2634if ~test_v
2635    set(handles.histo2_menu,'Visible','off')
2636    set(handles.histo_v,'Visible','off')
2637    cla(handles.histo_v)
2638    set(handles.histo2_menu,'Value',1)
2639end
2640%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2641
2642%% display time
2643testimedoc=0;
2644TimeUnit='';
2645if isfield(UvData.Field,'Time')
2646    abstime=UvData.Field.Time;%time read from the netcdf input file
2647end
2648if isfield(UvData,'Field_1') && isfield(UvData.Field_1,'Time')
2649    abstime_1=UvData.Field_1.Time;%time read from the netcdf input file
2650end
2651if isfield(UvData.Field,'dt')
2652    dt=UvData.Field.dt;%dt read from the netcdf input file
2653    if isfield(UvData.Field,'TimeUnit')
2654       TimeUnit=UvData.Field.TimeUnit;
2655    end
2656elseif isfield(UvData,'Field_1') && isfield(UvData.Field_1,'dt')%dt obtained from the second field if not defined in the first
2657    dt=UvData.Field_1.dt;%dt read from the netcdf input file
2658    if isfield(UvData.Field_1,'TimeUnit')
2659       TimeUnit=UvData.Field_1.TimeUnit;
2660    end
2661end
2662% time from xml file overset previous result
2663if isfield(UvData,'XmlData') && isfield(UvData.XmlData{1},'Time')
2664    if isempty(num_i2)||isnan(num_i2)
2665        num_i2=num_i1;
2666    end
2667    if isempty(num_j1)||isnan(num_j1)
2668        num_j1=1;
2669    end
2670    if isempty(num_j2)||isnan(num_j2)
2671        num_j2=num_j1;
2672    end
2673    siz=size(UvData.XmlData{1}.Time);
2674    if siz(1)>=max(num_i1,num_i2) && siz(2)>=max(num_j1,num_j2)
2675        abstime=(UvData.XmlData{1}.Time(num_i1,num_j1)+UvData.XmlData{1}.Time(num_i2,num_j2))/2;%overset the time read from files
2676        dt=(UvData.XmlData{1}.Time(num_i2,num_j2)-UvData.XmlData{1}.Time(num_i1,num_j1));
2677        testimedoc=1;
2678        if isfield(UvData.XmlData{1},'TimeUnit')
2679            TimeUnit=UvData.XmlData{1}.TimeUnit;
2680        end
2681    end
2682    if numel(UvData.XmlData)==2
2683        [tild,tild,tild,num_i1,num_i2,num_j1,num_j2]=fileparts_uvmat(['xx' get(handles.FileIndex_1,'String') get(handles.FileExt_1,'String')]);
2684        %  [P,F,str1,str2,str_a,str_b,E]=name2display(['xx' get(handles.FileIndex_1,'String') get(handles.FileExt_1,'String')]);
2685        if isempty(num_i2)
2686            num_i2=num_i1;
2687        end
2688        if isempty(num_j1)
2689            num_j1=1;
2690        end
2691        if isempty(num_j2)
2692            num_j2=num_j1;
2693        end
2694        siz=size(UvData.XmlData{2}.Time);
2695        if ~isempty(num_i1) && siz(1)>=max(num_i1,num_i2) && siz(2)>=max(num_j1,num_j2)
2696            abstime_1=(UvData.XmlData{2}.Time(num_i1,num_j1)+UvData.XmlData{2}.Time(num_i2,num_j2))/2;%overset the time read from files
2697        end
2698    end
2699end
2700
2701if ~isequal(numel(abstime),1)
2702    abstime=[];
2703end
2704if ~isequal(numel(abstime_1),1)
2705      abstime_1=[];
2706end 
2707set(handles.abs_time,'String',num2str(abstime,4))
2708set(handles.abs_time_1,'String',num2str(abstime_1,4))
2709if testimedoc && isfield(UvData,'dt')
2710    dt=UvData.dt;
2711end
2712if isempty(dt)||isequal(dt,0)
2713    set(handles.Dt_txt,'String','')
2714else
2715    if  isempty(TimeUnit)
2716        set(handles.Dt_txt,'String',['Dt=' num2str(1000*dt,3) '  10^(-3)'] )
2717    else
2718        set(handles.Dt_txt,'String',['Dt=' num2str(1000*dt,3) '  m' TimeUnit] )
2719    end
2720end
2721
2722
2723%-------------------------------------------------------------------
2724% --- translate coordinate to matrix index
2725%-------------------------------------------------------------------
2726function [indx,indy]=pos2ind(x0,rangx0,nxy)
2727indx=1+round((nxy(2)-1)*(x0-rangx0(1))/(rangx0(2)-rangx0(1)));% index x of pixel 
2728indy=1+round((nxy(1)-1)*(y12-rangy0(1))/(rangy0(2)-rangy0(1)));% index y of pixel
2729
2730%-------------------------------------------------------------------
2731% --- Executes on button press in 'CheckFixLimits'.
2732%-------------------------------------------------------------------
2733function CheckFixLimits_Callback(hObject, eventdata, handles)
2734test=get(handles.CheckFixLimits,'Value');
2735if test
2736    set(handles.CheckFixLimits,'BackgroundColor',[1 1 0])
2737else
2738    set(handles.CheckFixLimits,'BackgroundColor',[0.7 0.7 0.7])
2739    update_plot(handles);
2740end
2741
2742%-------------------------------------------------------------------
2743% --- Executes on button press in CheckFixEqual.
2744function CheckFixEqual_Callback(hObject, eventdata, handles)
2745test=get(handles.CheckFixEqual,'Value');
2746if test
2747    set(handles.CheckFixEqual,'BackgroundColor',[1 1 0])
2748    cla(handles.axes3)
2749    update_plot(handles);
2750else
2751    set(handles.CheckFixEqual,'BackgroundColor',[0.7 0.7 0.7])
2752    update_plot(handles);
2753%     axis(handles.axes3,'image')
2754end
2755
2756%-------------------------------------------------------------------
2757
2758%-------------------------------------------------------------------
2759% --- Executes on button press in 'CheckZoom'.
2760%-------------------------------------------------------------------
2761function CheckZoom_Callback(hObject, eventdata, handles)
2762
2763if (get(handles.CheckZoom,'Value') == 1);
2764    set(handles.CheckZoom,'BackgroundColor',[1 1 0])
2765    set(handles.CheckFixLimits,'Value',1)% propose by default fixed limits for the plotting axes
2766    set(handles.CheckFixLimits,'BackgroundColor',[1 1 0])
2767else
2768    set(handles.CheckZoom,'BackgroundColor',[0.7 0.7 0.7])
2769end
2770
2771%-------------------------------------------------------------------
2772%----Executes on button press in 'record': records the current flags of manual correction.
2773%-------------------------------------------------------------------
2774function record_Callback(hObject, eventdata, handles)
2775% [filebase,num_i1,num_j1,num_i2,num_j2,Ext,NomType,SubDir]=read_input_file(handles);
2776[RootPath,SubDir,RootFile,FileIndices,FileExt]=read_file_boxes(handles);
2777filename=[fullfile(RootPath,SubDir,RootFile) FileIndices FileExt];
2778%filename=read_file_boxes(handles);
2779[erread,message]=fileattrib(filename);
2780if ~isempty(message) && ~isequal(message.UserWrite,1)
2781     msgbox_uvmat('ERROR',['no writting access to ' filename])
2782     return
2783end
2784test_civ2=isequal(get(handles.civ2,'BackgroundColor'),[1 1 0]);
2785test_civ1=isequal(get(handles.VelType,'BackgroundColor'),[1 1 0]);
2786if ~test_civ2 && ~test_civ1
2787    msgbox_uvmat('ERROR','manual correction only possible for CIV1 or CIV2 velocity fields')
2788end
2789if test_civ2
2790    nbname='nb_vectors2';
2791   flagname='vec2_FixFlag';
2792   attrname='fix2';
2793end
2794if test_civ1
2795    nbname='nb_vectors';
2796   flagname='vec_FixFlag';
2797   attrname='fix';
2798end
2799%write fix flags in the netcdf file
2800UvData=get(handles.uvmat,'UserData');
2801hhh=which('netcdf.open');% look for built-in matlab netcdf library
2802if ~isequal(hhh,'')% case of new builtin Matlab netcdf library
2803    nc=netcdf.open(filename,'NC_WRITE');
2804    netcdf.reDef(nc);
2805    netcdf.putAtt(nc,netcdf.getConstant('NC_GLOBAL'),attrname,1);
2806    dimid = netcdf.inqDimID(nc,nbname);
2807    try
2808        varid = netcdf.inqVarID(nc,flagname);% look for already existing fixflag variable
2809    catch
2810        varid=netcdf.defVar(nc,flagname,'double',dimid);%create fixflag variable if it does not exist
2811    end
2812    netcdf.endDef(nc);
2813    netcdf.putVar(nc,varid,UvData.axes3.FF);
2814    netcdf.close(nc); 
2815else %old netcdf library
2816    netcdf_toolbox(filename,AxeData,attrname,nbname,flagname)
2817end
2818
2819%-------------------------------------------------------------------
2820%----Correct the netcdf file, using toolbox (old versions of Matlab).
2821%-------------------------------------------------------------------
2822function netcdf_toolbox(filename,AxeData,attrname,nbname,flagname)
2823nc=netcdf(filename,'write'); %open netcdf file
2824result=redef(nc);
2825eval(['nc.' attrname '=1;']);
2826theDim=nc(nbname) ;% get the number of velocity vectors
2827nb_vectors=size(theDim);
2828var_FixFlag=ncvar(flagname,nc);% var_FixFlag will be written as the netcdf variable vec_FixFlag
2829var_FixFlag(1:nb_vectors)=AxeData.FF;%
2830fin=close(nc);
2831
2832%---------------------------------------------------
2833% --- Executes on button press in SubField
2834function SubField_Callback(hObject, eventdata, handles)
2835% huvmat=get(handles.run0,'parent');
2836UvData=get(handles.uvmat,'UserData');
2837if get(handles.SubField,'Value')==0% if the subfield button is desactivated   
2838    set(handles.RootPath_1,'String','')
2839    set(handles.RootFile_1,'String','')
2840    set(handles.SubDir_1,'String','');
2841    set(handles.FileIndex_1,'String','');
2842    set(handles.FileExt_1,'String','');
2843    set(handles.RootPath_1,'Visible','off')
2844    set(handles.RootFile_1,'Visible','off')
2845    set(handles.SubDir_1,'Visible','off');
2846    set(handles.NomType_1,'Visible','off');
2847    set(handles.abs_time_1,'Visible','off')
2848    set(handles.FileIndex_1,'Visible','off');
2849    set(handles.FileExt_1,'Visible','off');
2850    set(handles.Fields_1,'Value',1);%set to blank state
2851    set(handles.VelType_1,'Value',1);%set to blank state
2852    if ~strcmp(get(handles.VelType,'Visible'),'on')
2853        set(handles.VelType_1,'Visible','off')
2854    end
2855    if isfield(UvData,'XmlData_1')
2856        UvData=rmfield(UvData,'XmlData_1');
2857    end
2858    set(handles.uvmat,'UserData',UvData);
2859    run0_Callback(hObject, eventdata, handles); %run
2860else
2861    MenuBrowse_1_Callback(hObject, eventdata, handles)
2862end
2863
2864%------------------------------------------------------------------------
2865% --- read the data displayed for the input rootfile windows (new): TODO use read_GUI
2866
2867function [RootPath,SubDir,RootFile,FileIndices,FileExt]=read_file_boxes(handles)
2868%------------------------------------------------------------------------
2869InputFile=read_GUI(handles.InputFile);
2870RootPath=InputFile.RootPath;
2871SubDir=regexprep(InputFile.SubDir,'/|\','');
2872RootFile=regexprep(InputFile.RootFile,'/|\','');
2873FileIndices=InputFile.FileIndex;
2874FileExt=InputFile.FileExt;
2875
2876
2877%------------------------------------------------------------------------
2878% ---- read the data displayed for the second input rootfile windows
2879function [RootPath_1,SubDir_1,RootFile_1,FileIndex_1,FileExt_1,NomType_1]=read_file_boxes_1(handles)
2880%------------------------------------------------------------------------
2881RootPath_1=get(handles.RootPath_1,'String'); % read the data from the file1_input window
2882if isequal(get(handles.RootPath_1,'Visible'),'off') || isequal(RootPath_1,'"')
2883    RootPath_1=get(handles.RootPath,'String');
2884end;
2885SubDir_1=get(handles.SubDir_1,'String');
2886if isequal(get(handles.SubDir_1,'Visible'),'off')|| isequal(SubDir_1,'"')
2887    SubDir_1=get(handles.SubDir,'String');
2888end
2889SubDir_1=regexprep(SubDir_1,'\<[\\/]|[\\/]\>','');%suppress possible / or \ separator at the beginning or the end of the string
2890RootFile_1=get(handles.RootFile_1,'String');
2891if isequal(get(handles.RootFile_1,'Visible'),'off') || isequal(RootFile_1,'"')
2892    RootFile_1=get(handles.RootFile,'String');
2893end
2894RootFile_1=regexprep(RootFile_1,'\<[\\/]|[\\/]\>','');%suppress possible / or \ separator at the beginning or the end of the string
2895FileIndex_1=get(handles.FileIndex_1,'String');
2896if isequal(get(handles.FileIndex_1,'Visible'),'off')|| isequal(FileIndex_1,'"')
2897    FileIndex_1=get(handles.FileIndex,'String');
2898end
2899FileExt_1=get(handles.FileExt_1,'String');
2900if isequal(get(handles.FileExt_1,'Visible'),'off') || isequal(FileExt_1,'"')
2901    FileExt_1=get(handles.FileExt,'String');%read FileExt by default
2902end
2903NomType_1=get(handles.NomType_1,'String');
2904if isequal(get(handles.NomType_1,'Visible'),'off') || isequal(NomType_1,'"')
2905    NomType_1=get(handles.NomType,'String');%read FileExt by default
2906end
2907%------------------------------------------------------------------------
2908% --- Executes on menu selection Fields
2909function Fields_Callback(hObject, eventdata, handles)
2910%------------------------------------------------------------------------
2911list_fields=get(handles.Fields,'String');% list menu fields
2912index_fields=get(handles.Fields,'Value');% selected string index
2913field= list_fields{index_fields(1)}; % selected string
2914if isequal(field,'get_field...')
2915    set(handles.FixVelType,'visible','off')
2916    set(handles.VelType,'visible','off')
2917    set(handles.VelType_1,'visible','off')
2918    [RootPath,SubDir,RootFile,FileIndices,FileExt]=read_file_boxes(handles);
2919    filename=[fullfile(RootPath,SubDir,RootFile) FileIndices FileExt];
2920    %filename=read_file_boxes(handles);
2921    hget_field=findobj(allchild(0),'name','get_field');
2922    if ~isempty(hget_field)
2923        delete(hget_field)
2924    end
2925    hget_field=get_field(filename);
2926    set(hget_field,'Name','get_field')
2927    hhget_field=guidata(hget_field);
2928    set(hhget_field.list_fig,'Value',1)
2929    set(hhget_field.list_fig,'String',{'uvmat'})
2930    set(handles.transform_fct,'Value',1)% no transform by default
2931    set(handles.path_transform,'String','')
2932    return %no action
2933end
2934list_fields=get(handles.Fields_1,'String');% list menu fields
2935index_fields=get(handles.Fields_1,'Value');% selected string index
2936field_1= list_fields{index_fields(1)}; % selected string
2937UvData=get(handles.uvmat,'UserData');
2938
2939%read the rootfile input display
2940[RootPath,SubDir,RootFile,FileIndices,FileExt]=read_file_boxes(handles);
2941FileName=[fullfile(RootPath,SubDir,RootFile) FileIndices FileExt];
2942[tild,tild,tild,i1,i2,j1,j2,tild,NomType]=fileparts_uvmat(['xxx' get(handles.FileIndex,'String') FileExt]);
2943% NomTypeNew=NomType;%default
2944if isequal(field,'image')
2945%     if isequal(NomType,'_1-2_1')||isequal(NomType,'_1_1-2')
2946%         NomTypeNew='_1_1';
2947%     elseif isequal(NomType,'#_ab')
2948%         NomTypeNew='#a';
2949%     elseif isequal(NomType,'_1-2')
2950%         NomTypeNew='_1';
2951%     end
2952    imagename=fullfile_uvmat(RootPath,SubDir,RootFile,'.png',NomType,i1,[],j1,[]);
2953    if ~exist(imagename,'file')
2954        [FileName,PathName] = uigetfile( ...
2955            {'*.png;*.jpg;*.tif;*.avi;*.AVI;*.vol', ' (*.png, .tif, *.avi,*.vol)';
2956            '*.jpg',' jpeg image files'; ...
2957            '*.png','.png image files'; ...
2958            '*.tif','.tif image files'; ...
2959            '*.avi;*.AVI','.avi movie files'; ...
2960            '*.vol','.volume images (png)'; ...
2961            '*.*',  'All Files (*.*)'}, ...
2962            'Pick an image',imagename);   
2963        imagename=[PathName FileName];
2964    end
2965     % display the selected field and related information
2966    display_file_name(handles,imagename)%display the image
2967    return
2968else
2969    ext=get(handles.FileExt,'String');
2970    if ~isequal(ext,'.nc') %find the new NomType if the previous display was not already a netcdf file
2971        [FileName,PathName] = uigetfile( ...
2972            {'*.nc', ' (*.nc)';
2973            '*.nc',' netcdf files'; ...
2974            '*.*',  'All Files (*.*)'}, ...
2975            'Pick a netcdf file',FileBase);
2976        filename=[PathName FileName];
2977        % display the selected field and related information
2978        display_file_name( handles,filename)
2979        return
2980    end
2981end
2982indices=fullfile_uvmat('','','','',NomType,i1,i2,j1,j2);
2983set(handles.FileIndex,'String',indices)
2984% set(handles.NomType,'String',NomType)
2985
2986%common to Fields_1_Callback
2987if isequal(field,'image')||isequal(field_1,'image')
2988    set(handles.TitleNpx,'Visible','on')% visible npx,pxcm... buttons
2989    set(handles.TitleNpy,'Visible','on')
2990    set(handles.num_Npx,'Visible','on')
2991    set(handles.num_Npy,'Visible','on')
2992else
2993    set(handles.TitleNpx,'Visible','off')% visible npx,pxcm... buttons
2994    set(handles.TitleNpy,'Visible','off')
2995    set(handles.num_Npx,'Visible','off')
2996    set(handles.num_Npy,'Visible','off')
2997end
2998if ~(isfield(UvData,'NewSeries')&&isequal(UvData.NewSeries,1))
2999    run0_Callback(hObject, eventdata, handles)
3000end
3001
3002%---------------------------------------------------
3003% --- Executes on menu selection Fields
3004function Fields_1_Callback(hObject, eventdata, handles)
3005%-------------------------------------------------
3006%% read input data
3007check_new=~get(handles.SubField,'Value'); %check_new=1 if a second field was not previously entered
3008UvData=get(handles.uvmat,'UserData');
3009if check_new && isfield(UvData,'XmlData')
3010    UvData.XmlData{2}=UvData.XmlData{1};
3011end
3012if isfield(UvData,'Field_1')
3013    UvData=rmfield(UvData,'Field_1');% remove the stored second field (a new one needs to be read)
3014end
3015UvData.filename_1='';% desactivate the use of a constant second file
3016list_fields=get(handles.Fields,'String');% list menu fields
3017field= list_fields{get(handles.Fields,'Value')}; % selected string
3018list_fields=get(handles.Fields_1,'String');% list menu fields
3019field_1= list_fields{get(handles.Fields_1,'Value')}; % selected string for the second field
3020if isempty(field_1)%||(numel(UvData.FileType)>=2 && strcmp(UvData.FileType{2},'image'))
3021    set(handles.SubField,'Value',0)
3022%     check_new=1;
3023    SubField_Callback(hObject, eventdata, handles)
3024%     if isempty(field_1)%remove second field if 'blank' field is selected
3025        return
3026%     end
3027else
3028    set(handles.SubField,'Value',1)%state that a second field is now entered
3029end
3030
3031%% read the rootfile input display
3032[RootPath_1,SubDir_1,RootFile_1,FileIndex_1,FileExt_1]=read_file_boxes_1(handles);
3033filename_1=[fullfile(RootPath_1,SubDir_1,RootFile_1) FileIndex_1 FileExt_1];
3034[tild,tild,tild,i1,i2,j1,j2]=fileparts_uvmat(get(handles.FileIndex,'String'));
3035% set(handles.FileIndex_1,'Visible','on')
3036% set(handles.FileExt_1,'Visible','on')
3037switch field_1
3038    case 'get_field...'
3039        set_veltype_display(0) % no veltype display
3040        hget_field=findobj(allchild(0),'name','get_field_1');
3041        if ~isempty(hget_field)
3042            delete(hget_field)
3043        end
3044        hget_field=get_field(filename_1);
3045        set(hget_field,'name','get_field_1')
3046        hhget_field=guidata(hget_field);
3047        set(hhget_field.list_fig,'Value',1)
3048        set(hhget_field.list_fig,'String',{'uvmat'})
3049        set(handles.transform_fct,'Value',1)% no transform by default
3050        set(handles.path_transform,'String','')
3051    case 'image'
3052        % guess the image name corresponding to the current netcdf name (no unique correspondance)
3053        imagename=fullfile_uvmat(RootPath_1,'',RootFile_1,'.png',get(handles.NomType,'String'),i1,[],j1);
3054        if ~exist(imagename,'file') % browse for images if it is not found
3055            [FileName,PathName] = uigetfile( ...
3056                {'*.png;*.jpg;*.tif;*.avi;*.AVI;*.vol', ' (*.png, .tif, *.avi,*.vol)';
3057                '*.jpg',' jpeg image files'; ...
3058                '*.png','.png image files'; ...
3059                '*.tif','.tif image files'; ...
3060                '*.avi;*.AVI','.avi movie files'; ...
3061                '*.vol','.volume images (png)'; ...
3062                '*.*',  'All Files (*.*)'}, ...
3063                'Pick an image',imagename);
3064            imagename=[PathName FileName];
3065        end
3066        if ~ischar(imagename)% quit if the browser has  been closed
3067            set(handles.SubField,'Value',0)
3068        else %valid browser input:  display the selected image
3069            set(handles.TitleNpx,'Visible','on')% visible npx,pxcm... buttons
3070            set(handles.TitleNpy,'Visible','on')
3071            set(handles.num_Npx,'Visible','on')
3072            set(handles.num_Npy,'Visible','on')
3073            display_file_name(handles,imagename,2)%display the imag
3074        end
3075    otherwise
3076        if check_new
3077            UvData.FileType{2}=UvData.FileType{1};
3078            set(handles.FileIndex_1,'String',get(handles.FileIndex,'String'))
3079            set(handles.FileExt_1,'String',get(handles.FileExt,'String'))
3080        end
3081        if ~isequal(field,'image')
3082            set(handles.TitleNpx,'Visible','off')% visible npx,pxcm... buttons
3083            set(handles.TitleNpy,'Visible','off')
3084            set(handles.num_Npx,'Visible','off')
3085            set(handles.num_Npy,'Visible','off')
3086        end
3087        set(handles.uvmat,'UserData',UvData)
3088        if ~(isfield(UvData,'NewSeries')&&isequal(UvData.NewSeries,1))
3089            run0_Callback(hObject, eventdata, handles)
3090        end
3091end
3092
3093%------------------------------------------------------------------------
3094% --- set the visibility of relevant velocity type menus:
3095function menu=set_veltype_display(Civ,FileType)
3096%------------------------------------------------------------------------
3097if ~exist('FileType','var')
3098    FileType='civx';
3099end
3100switch FileType
3101    case 'civx'
3102        menu={'civ1';'interp1';'filter1';'civ2';'interp2';'filter2'};
3103        if isequal(Civ,0)
3104            imax=0;
3105        elseif isequal(Civ,1) || isequal(Civ,2)
3106            imax=1;
3107        elseif isequal(Civ,3)
3108            imax=3;
3109        elseif isequal(Civ,4) || isequal(Civ,5)
3110            imax=4;
3111        elseif isequal(Civ,6) %patch2
3112            imax=6;
3113        end
3114    case 'civdata'
3115        menu={'civ1';'filter1';'civ2';'filter2'};
3116        if isequal(Civ,0)
3117            imax=0;
3118        elseif isequal(Civ,1) || isequal(Civ,2)
3119            imax=1;
3120        elseif isequal(Civ,3)
3121            imax=2;
3122        elseif isequal(Civ,4) || isequal(Civ,5)
3123            imax=3;
3124        elseif isequal(Civ,6) %patch2
3125            imax=4;
3126        end
3127end
3128menu=menu(1:imax);
3129
3130%------------------------------------------------------------------------
3131% --- Executes on button press in FixVelType.
3132function FixVelType_Callback(hObject, eventdata, handles)
3133%------------------------------------------------------------------------
3134% refresh the current plot if the fixed  veltype is unselected
3135if ~get(handles.FixVelType,'Value')
3136    run0_Callback(hObject, eventdata, handles)
3137end
3138
3139%------------------------------------------------------------------------
3140% --- Executes on button press in VelType.
3141function VelType_Callback(hObject, eventdata, handles)
3142%------------------------------------------------------------------------
3143set(handles.FixVelType,'Value',1)
3144run0_Callback(hObject, eventdata, handles)
3145
3146%------------------------------------------------------------------------
3147% --- Executes on button press in VelType_1.
3148function VelType_1_Callback(hObject, eventdata, handles)
3149%------------------------------------------------------------------------
3150set(handles.FixVelType,'Value',1)% the velocity type is now imposed by the GUI (not automatic)
3151UvData=get(handles.uvmat,'UserData');
3152%refresh field with a second filename=first file name
3153set(handles.run0,'BackgroundColor',[1 1 0])%paint the command button in yellow
3154drawnow   
3155InputFile=read_GUI(handles.InputFile);
3156[RootPath,SubDir,RootFile,FileIndices,FileExt]=read_file_boxes(handles);
3157filename=[fullfile(RootPath,SubDir,RootFile) FileIndices FileExt];
3158
3159if isempty(InputFile.VelType_1)
3160        filename_1='';% we plot the current field without the second field
3161        set(handles.SubField,'Value',0)
3162        SubField_Callback(hObject, eventdata, handles)
3163elseif get(handles.SubField,'Value')% if subfield is already 'on'
3164      [RootPath_1,SubDir_1,RootFile_1,FileIndices_1,FileExt_1]=read_file_boxes_1(handles);
3165     filename_1=[fullfile(RootPath_1,SubDir_1,RootFile_1) FileIndices_1 FileExt_1];
3166else
3167     filename_1=filename;% we compare two fields in the same file by default
3168     UvData.FileType{2}=UvData.FileType{1};
3169     set(handles.SubField,'Value',1)
3170end
3171if isfield(UvData,'Field_1')
3172    UvData=rmfield(UvData,'Field_1');% removes the stored second field if it exists
3173end
3174UvData.filename_1='';% desactivate the use of a constant second file
3175set(handles.uvmat,'UserData',UvData)
3176num_i1=stra2num(get(handles.i1,'String'));
3177num_i2=stra2num(get(handles.i2,'String'));
3178num_j1=stra2num(get(handles.j1,'String'));
3179num_j2=stra2num(get(handles.j2,'String'));
3180
3181errormsg=refresh_field(handles,filename,filename_1,num_i1,num_i2,num_j1,num_j2);
3182
3183if ~isempty(errormsg)
3184    msgbox_uvmat('ERROR',errormsg);
3185else
3186    set(handles.i1,'BackgroundColor',[1 1 1])
3187    set(handles.i2,'BackgroundColor',[1 1 1])
3188    set(handles.j1,'BackgroundColor',[1 1 1])
3189    set(handles.j2,'BackgroundColor',[1 1 1])
3190    set(handles.FileIndex,'BackgroundColor',[1 1 1])
3191    set(handles.FileIndex_1,'BackgroundColor',[1 1 1])
3192end
3193set(handles.run0,'BackgroundColor',[1 0 0])
3194
3195%-----------------------------------------------
3196% --- reset civ buttons
3197function reset_vel_type(handles_civ0,handle1)
3198for ibutton=1:length(handles_civ0)
3199    set(handles_civ0(ibutton),'BackgroundColor',[0.831 0.816 0.784])
3200    set(handles_civ0(ibutton),'Value',0)
3201end
3202if exist('handle1','var')%handles of selected button
3203        set(handle1,'BackgroundColor',[1 1 0]) 
3204end
3205
3206%-------------------------------------------------------
3207% --- Executes on button press in MENUVOLUME.
3208%-------------------------------------------------------
3209function VOLUME_Callback(hObject, eventdata, handles)
3210%errordlg('command VOL not implemented yet')
3211if ishandle(handles.UVMAT_title)
3212    delete(handles.UVMAT_title)
3213end
3214UvData=get(handles.uvmat,'UserData');%read UvData properties stored on the uvmat interface
3215if isequal(get(handles.VOLUME,'Value'),1)
3216    set(handles.CheckZoom,'Value',0)
3217    set(handles.CheckZoom,'BackgroundColor',[0.7 0.7 0.7])
3218    set(handles.edit_vect,'Value',0)
3219    edit_vect_Callback(hObject, eventdata, handles)
3220    set(handles.edit_object,'Value',0)
3221    set(handles.edit_object,'BackgroundColor',[0.7 0.7 0.7])
3222%     set(handles.cal,'Value',0)
3223%     set(handles.cal,'BackgroundColor',[0 1 0])
3224    set(handles.edit_vect,'Value',0)
3225    edit_vect_Callback(hObject, eventdata, handles)
3226    %initiate set_object GUI
3227    data.Name='VOLUME';
3228    if isfield(UvData,'CoordType')
3229        data.CoordType=UvData.CoordType;
3230    end
3231    if isfield(UvData.Field,'Mesh')&~isempty(UvData.Field.Mesh)
3232        data.RangeX=[UvData.Field.XMin UvData.Field.XMax];
3233        data.RangeY=[UvData.Field.YMin UvData.Field.YMax];
3234        data.DX=UvData.Field.Mesh;
3235        data.DY=UvData.Field.Mesh;
3236    elseif isfield(UvData.Field,'AX')&isfield(UvData.Field,'AY')& isfield(UvData.Field,'A')%only image
3237        np=size(UvData.Field.A);
3238        meshx=(UvData.Field.AX(end)-UvData.Field.AX(1))/np(2);
3239        meshy=abs(UvData.Field.AY(end)-UvData.Field.AY(1))/np(1);
3240        data.RangeY=max(meshx,meshy);
3241        data.RangeX=max(meshx,meshy);
3242        data.DX=max(meshx,meshy);
3243    end
3244    data.ParentButton=handles.VOLUME;
3245    PlotHandles=get_plot_handles(handles);%get the handles of the interface elements setting the plotting parameters
3246    [hset_object,UvData.sethandles]=set_object(data,PlotHandles);% call the set_object interface with action on haxes,
3247                                                      % associate the set_object interface handle to the plotting axes
3248    if isfield(UvData.OpenParam,'SetObjectOrigin')                                               
3249    pos_uvmat=get(handles.uvmat,'Position');
3250    pos_set_object(1:2)=UvData.OpenParam.SetObjectOrigin + pos_uvmat(1:2);
3251    pos_set_object(3:4)=UvData.OpenParam.SetObjectSize .* pos_uvmat(3:4); 
3252    set(hset_object,'Position',pos_set_object)
3253    end
3254    UvData.MouseAction='create_object';
3255else
3256    set(handles.VOLUME,'BackgroundColor',[0 1 0])
3257    UvData.MouseAction='none';
3258end
3259set(handles.uvmat,'UserData',UvData)
3260
3261%-------------------------------------------------------
3262function edit_vect_Callback(hObject, eventdata, handles)
3263%-------------------------------------------------------
3264%
3265% UvData=get(handles.uvmat,'UserData');%read UvData properties stored on the uvmat interface
3266if isequal(get(handles.edit_vect,'Value'),1)
3267    test_civ2=isequal(get(handles.civ2,'BackgroundColor'),[1 1 0]);
3268    test_civ1=isequal(get(handles.VelType,'BackgroundColor'),[1 1 0]);
3269    if ~test_civ2 && ~test_civ1
3270        msgbox_uvmat('ERROR','manual correction only possible for CIV1 or CIV2 velocity fields')
3271    end
3272    set(handles.record,'Visible','on')
3273    set(handles.edit_vect,'BackgroundColor',[1 1 0])
3274    set(handles.edit_object,'Value',0)
3275    set(handles.CheckZoom,'Value',0)
3276    set(handles.CheckZoom,'BackgroundColor',[0.7 0.7 0.7])
3277%     set(handles.create,'Value',0)
3278%     set(handles.create,'BackgroundColor',[0 1 0])
3279    set(handles.edit_object,'BackgroundColor',[0.7 0.7 0.7])
3280    set(gcf,'Pointer','arrow')
3281%     UvData.MouseAction='edit_vect';
3282else
3283    set(handles.record,'Visible','off')
3284    set(handles.edit_vect,'BackgroundColor',[0.7 0.7 0.7])
3285%     UvData.MouseAction='none';
3286end
3287% set(handles.uvmat,'UserData',UvData)
3288
3289%----------------------------------------------
3290function save_mask_Callback(hObject, eventdata, handles)
3291%-----------------------------------------------------------------------
3292UvData=get(handles.uvmat,'UserData');
3293
3294flag=1;
3295npx=size(UvData.Field.A,2);
3296npy=size(UvData.Field.A,1);
3297xi=0.5:npx-0.5;
3298yi=0.5:npy-0.5;
3299[Xi,Yi]=meshgrid(xi,yi);
3300if isfield(UvData,'Object')
3301    for iobj=1:length(UvData.Object)
3302        ObjectData=UvData.Object{iobj};
3303        if isfield(ObjectData,'ProjMode') &&(isequal(ObjectData.ProjMode,'mask_inside')||isequal(ObjectData.ProjMode,'mask_outside'));
3304            flagobj=1;
3305            testphys=0; %coordinates in pixels by default
3306            if isfield(ObjectData,'CoordType') && isequal(ObjectData.CoordType,'phys')
3307                if isfield(UvData,'XmlData')&& isfield(UvData.XmlData{1},'GeometryCalib')
3308                    Calib=UvData.XmlData{1}.GeometryCalib;
3309                    testphys=1;
3310                end
3311            end
3312            if isfield(ObjectData,'Coord')& isfield(ObjectData,'Style')
3313                if isequal(ObjectData.Type,'polygon')
3314                    X=ObjectData.Coord(:,1);
3315                    Y=ObjectData.Coord(:,2);
3316                    if testphys
3317                        [X,Y]=px_XYZ(Calib,X,Y,0);% to generalise with 3D cases
3318                    end
3319                    flagobj=~inpolygon(Xi,Yi,X',Y');%=0 inside the polygon, 1 outside                 
3320                elseif isequal(ObjectData.Type,'ellipse')
3321                    if testphys
3322                        %[X,Y]=px_XYZ(Calib,X,Y,0);% TODO:create a polygon boundary and transform to phys
3323                    end
3324                    RangeX=max(ObjectData.RangeX);
3325                    RangeY=max(ObjectData.RangeY);
3326                    X2Max=RangeX*RangeX;
3327                    Y2Max=RangeY*RangeY;
3328                    distX=(Xi-ObjectData.Coord(1,1));
3329                    distY=(Yi-ObjectData.Coord(1,2));
3330                    flagobj=(distX.*distX/X2Max+distY.*distY/Y2Max)>1;
3331                elseif isequal(ObjectData.Type,'rectangle')
3332                    if testphys
3333                        %[X,Y]=px_XYZ(Calib,X,Y,0);% TODO:create a polygon boundary and transform to phys
3334                    end
3335                    distX=abs(Xi-ObjectData.Coord(1,1));
3336                    distY=abs(Yi-ObjectData.Coord(1,2));
3337                    flagobj=distX>max(ObjectData.RangeX) | distY>max(ObjectData.RangeY);
3338                end
3339                if isequal(ObjectData.ProjMode,'mask_outside')
3340                    flagobj=~flagobj;
3341                end
3342                flag=flag & flagobj;
3343            end
3344        end
3345    end
3346end
3347% flag=~flag;
3348%mask name
3349RootPath=get(handles.RootPath,'String');
3350RootFile=get(handles.RootFile,'String');
3351RootFile=regexprep(RootFile,'\<[\\/]|[\\/]\>','');%suppress possible / or \ separator at the beginning or the end of the string
3352filebase=fullfile(RootPath,RootFile);
3353list=get(handles.masklevel,'String');
3354masknumber=num2str(length(list));
3355maskindex=get(handles.masklevel,'Value');
3356mask_name=fullfile_uvmat(RootPath,SubDir,[RootFile '_' masknumber 'mask'],'.png','_1',maskindex);
3357%mask_name=name_generator([filebase '_' masknumber 'mask'],maskindex,1,'.png','_i');
3358imflag=uint8(255*(0.392+0.608*flag));% =100 for flag=0 (vectors not computed when 20<imflag<200)
3359imflag=flipdim(imflag,1);
3360% imflag=uint8(255*flag);% =0 for flag=0 (vectors=0 when 20<imflag<200)
3361msgbox_uvmat('CONFIRMATION',[mask_name ' saved'])
3362imwrite(imflag,mask_name,'BitDepth',8);
3363
3364%display the mask
3365figure;
3366vec=linspace(0,1,256);%define a linear greyscale colormap
3367map=[vec' vec' vec'];
3368colormap(map)
3369
3370image(imflag);
3371
3372%-------------------------------------------------------------------
3373%-------------------------------------------------------------------
3374%  - FUNCTIONS FOR SETTING PLOTTING PARAMETERS
3375
3376%------------------------------------------------------------------
3377
3378
3379
3380%-------------------------------------------------------------
3381% --- Executes on selection change in transform_fct.
3382function transform_fct_Callback(hObject, eventdata, handles)
3383%-------------------------------------------------------------
3384UvData=get(handles.uvmat,'UserData');
3385menu=get(handles.transform_fct,'String');
3386ind_coord=get(handles.transform_fct,'Value');
3387coord_option=menu{ind_coord};
3388list_transform=get(handles.transform_fct,'UserData');
3389ff=functions(list_transform{end}); 
3390if isequal(coord_option,'more...');
3391    coord_fct='';
3392    prompt = {'Enter the name of the transform function'};
3393    dlg_title = 'user defined transform';
3394    num_lines= 1;
3395    [FileName, PathName] = uigetfile( ...
3396       {'*.m', ' (*.m)';
3397        '*.m',  '.m files '; ...
3398        '*.*', 'All Files (*.*)'}, ...
3399        'Pick a file', ff.file);
3400    if isequal(PathName(end),'/')||isequal(PathName(end),'\')
3401        PathName(end)=[];
3402    end
3403    transform_selected =fullfile(PathName,FileName);
3404    if ~exist(transform_selected,'file')
3405           return
3406    end
3407   [ppp,transform,ext_fct]=fileparts(FileName);% removes extension .m
3408   if ~isequal(ext_fct,'.m')
3409        msgbox_uvmat('ERROR','a Matlab function .m must be introduced');
3410        return
3411   end
3412   menu=update_menu(handles.transform_fct,transform);%add the selected fct to the menu
3413   ind_coord=get(handles.transform_fct,'Value');
3414   addpath(PathName)
3415   list_transform{ind_coord}=str2func(transform);% create the function handle corresponding to the newly seleced function
3416   set(handles.transform_fct,'UserData',list_transform)
3417   rmpath(PathName)
3418   % save the new menu in the personal file 'uvmat_perso.mat'
3419   dir_perso=prefdir;%personal Matalb directory
3420   profil_perso=fullfile(dir_perso,'uvmat_perso.mat');
3421   if exist(profil_perso,'file')
3422       nb_builtin=UvData.OpenParam.NbBuiltin;
3423       for ilist=nb_builtin+1:numel(list_transform)
3424           ff=functions(list_transform{ilist});
3425           transform_fct{ilist-nb_builtin}=ff.file;
3426       end
3427        save (profil_perso,'transform_fct','-append'); %store the root name for future opening of uvmat
3428   end   
3429end
3430
3431%check the current path to the selected function
3432if isa(list_transform{ind_coord},'function_handle')
3433    func=functions(list_transform{ind_coord});
3434    set(handles.path_transform,'String',fileparts(func.file)); %show the path to the senlected function
3435else
3436    set(handles.path_transform,'String','')
3437end
3438
3439set(handles.CheckFixLimits,'Value',0)
3440set(handles.CheckFixLimits,'BackgroundColor',[0.7 0.7 0.7])
3441
3442%delete drawn objects
3443hother=findobj('Tag','proj_object');%find all the proj objects
3444for iobj=1:length(hother)
3445    delete_object(hother(iobj))
3446end
3447hother=findobj('Tag','DeformPoint');%find all the proj objects
3448for iobj=1:length(hother)
3449    delete_object(hother(iobj))
3450end
3451hh=findobj('Tag','calib_points');
3452if ~isempty(hh)
3453    delete(hh)
3454end
3455hhh=findobj('Tag','calib_marker');
3456if ~isempty(hhh)
3457    delete(hhh)
3458end
3459if isfield(UvData,'Object')
3460     UvData.Object=UvData.Object(1);
3461end
3462set(handles.ListObject,'Value',1)
3463set(handles.ListObject,'String',{''})
3464
3465%delete mask if it is displayed
3466if isequal(get(handles.CheckMask,'Value'),1)%if the mask option is on
3467   UvData=rmfield(UvData,'MaskName'); %will impose mask refresh 
3468end
3469set(handles.uvmat,'UserData',UvData)
3470run0_Callback(hObject, eventdata, handles)
3471
3472%------------------------------------------------------------------------
3473function histo1_menu_Callback(hObject, eventdata, handles)
3474%--------------------------------------------
3475%plot first histo
3476%huvmat=get(handles.histo1_menu,'parent');
3477histo_menu=get(handles.histo1_menu,'String');
3478histo_value=get(handles.histo1_menu,'Value');
3479FieldName=histo_menu{histo_value};
3480update_histo(handles.histo_u,handles.uvmat,FieldName)
3481
3482%------------------------------------------------------------------------
3483function histo2_menu_Callback(hObject, eventdata, handles)
3484%------------------------------------------------------------------------
3485%plot second histo
3486%huvmat=get(handles.histo2_menu,'parent');
3487histo_menu=get(handles.histo2_menu,'String');
3488histo_value=get(handles.histo2_menu,'Value');
3489FieldName=histo_menu{histo_value};
3490update_histo(handles.histo_v,handles.uvmat,FieldName)
3491
3492%------------------------------------------------------------------------
3493%read the field .Fieldname stored in UvData and plot its histogram
3494function update_histo(haxes,huvmat,FieldName)
3495%------------------------------------------------------------------------
3496UvData=get(huvmat,'UserData');
3497if ~isfield(UvData.Field,FieldName)
3498    msgbox_uvmat('ERROR',['no field  ' FieldName ' for histogram'])
3499    return
3500end
3501Field=UvData.Field;
3502FieldHisto=eval(['Field.' FieldName]);
3503if isfield(Field,'FF') && ~isempty(Field.FF) && isequal(size(Field.FF),size(FieldHisto))
3504    indsel=find(Field.FF==0);%find values marked as false
3505    if ~isempty(indsel)
3506        FieldHisto=FieldHisto(indsel);
3507    end
3508end
3509if isempty(Field)
3510    msgbox_uvmat('ERROR',['empty field ' FieldName])
3511else
3512    nxy=size(FieldHisto);
3513    Amin=double(min(min(min(FieldHisto))));%min of image
3514    Amax=double(max(max(max(FieldHisto))));%max of image
3515    if isequal(Amin,Amax)
3516        %msgbox_uvmat('WARNING',['uniform field =' num2str(Amin)]);
3517        cla(haxes)
3518    else
3519        Histo.ListVarName={FieldName,'histo'};
3520        if isfield(Field,'NbDim') && isequal(Field.NbDim,3)
3521            Histo.VarDimName={FieldName,FieldName}; %dimensions for the histogram
3522        else
3523            if numel(nxy)==2
3524                Histo.VarDimName={FieldName,FieldName}; %dimensions for the histogram
3525            else
3526                Histo.VarDimName={FieldName,{FieldName,'rgb'}}; %dimensions for the histogram
3527            end
3528        end
3529        %unit
3530        units=[]; %default
3531        for ivar=1:numel(Field.ListVarName)
3532            if strcmp(Field.ListVarName{ivar},FieldName)
3533                if isfield(Field,'VarAttribute') && numel(Field.VarAttribute)>=ivar && isfield(Field.VarAttribute{ivar},'units')
3534                    units=Field.VarAttribute{ivar}.units;
3535                    break
3536                end
3537            end
3538        end
3539        if ~isempty(units)
3540            Histo.VarAttribute{1}.units=units;
3541        end
3542        eval(['Histo.' FieldName '=linspace(Amin,Amax,50);'])%absissa values for histo
3543        if isfield(Field,'NbDim') && isequal(Field.NbDim,3)
3544            C=reshape(double(FieldHisto),1,[]);% reshape in a vector
3545            eval(['Histo.histo(:,1)=hist(C, Histo.' FieldName ');']);  %calculate histogram
3546        else
3547            for col=1:size(FieldHisto,3)
3548                B=FieldHisto(:,:,col);
3549                C=reshape(double(B),1,nxy(1)*nxy(2));% reshape in a vector
3550                eval(['Histo.histo(:,col)=hist(C, Histo.' FieldName ');']);  %calculate histogram
3551            end
3552        end
3553        plot_field(Histo,haxes);
3554    end
3555end
3556
3557%------------------------------------------------
3558%CALLBACKS FOR PLOTTING PARAMETERS
3559%-------------------------------------------------
3560%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3561% Plot coordinates
3562%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3563%------------------------------------------------------------------------
3564function num_MinX_Callback(hObject, eventdata, handles)
3565%------------------------------------------------------------------------
3566set(handles.CheckFixLimits,'Value',1) %suppress auto mode
3567set(handles.CheckFixLimits,'BackgroundColor',[1 1 0])
3568update_plot(handles);
3569
3570%------------------------------------------------------------------------
3571function num_MaxX_Callback(hObject, eventdata, handles)
3572%------------------------------------------------------------------------
3573set(handles.CheckFixLimits,'Value',1) %suppress auto mode
3574set(handles.CheckFixLimits,'BackgroundColor',[1 1 0])
3575update_plot(handles);
3576
3577%------------------------------------------------------------------------
3578function num_MinY_Callback(hObject, eventdata, handles)
3579%------------------------------------------
3580set(handles.CheckFixLimits,'Value',1) %suppress auto mode
3581set(handles.CheckFixLimits,'BackgroundColor',[1 1 0])
3582update_plot(handles);
3583
3584%------------------------------------------------------------------------
3585function num_MaxY_Callback(hObject, eventdata, handles)
3586%------------------------------------------------------------------------
3587set(handles.CheckFixLimits,'Value',1) %suppress auto mode
3588set(handles.CheckFixLimits,'BackgroundColor',[1 1 0])
3589update_plot(handles);
3590
3591%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3592% Scalar or image representation
3593%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3594%------------------------------------------------------------------------
3595function num_MinA_Callback(hObject, eventdata, handles)
3596%------------------------------------------
3597set(handles.CheckFixScalar,'Value',1) %suppress auto mode
3598set(handles.CheckFixScalar,'BackgroundColor',[1 1 0])
3599MinA=str2double(get(handles.num_MinA,'String'));
3600MaxA=str2double(get(handles.num_MaxA,'String'));
3601if MinA>MaxA% switch minA and maxA in case of error
3602    MinA_old=MinA;
3603    MinA=MaxA;
3604    MaxA=MinA_old;
3605    set(handles.num_MinA,'String',num2str(MinA,5));
3606    set(handles.num_MaxA,'String',num2str(MaxA,5));
3607end
3608update_plot(handles);
3609
3610%------------------------------------------------------------------------
3611function num_MaxA_Callback(hObject, eventdata, handles)
3612%------------------------------------------------------------------------
3613set(handles.CheckFixScalar,'Value',1) %suppress auto mode
3614set(handles.CheckFixScalar,'BackgroundColor',[1 1 0])
3615MinA=str2double(get(handles.num_MinA,'String'));
3616MaxA=str2double(get(handles.num_MaxA,'String'));
3617if MinA>MaxA% switch minA and maxA in case of error
3618        MinA_old=MinA;
3619    MinA=MaxA;
3620    MaxA=MinA_old;
3621    set(handles.num_MinA,'String',num2str(MinA,5));
3622    set(handles.num_MaxA,'String',num2str(MaxA,5));
3623end
3624update_plot(handles);
3625
3626%------------------------------------------------------------------------
3627function CheckFixScalar_Callback(hObject, eventdata, handles)
3628%------------------------------------------------------------------------
3629test=get(handles.CheckFixScalar,'Value');
3630if test
3631    set(handles.CheckFixScalar,'BackgroundColor',[1 1 0])
3632else
3633    set(handles.CheckFixScalar,'BackgroundColor',[0.7 0.7 0.7])
3634    update_plot(handles);
3635end
3636
3637%-------------------------------------------------------------------
3638function CheckBW_Callback(hObject, eventdata, handles)
3639%-------------------------------------------------------------------
3640update_plot(handles);
3641
3642%-------------------------------------------------------------------
3643function ListContour_Callback(hObject, eventdata, handles)
3644%-------------------------------------------------------------------
3645val=get(handles.ListContour,'Value');
3646if val==2
3647    set(handles.interval_txt,'Visible','on')
3648    set(handles.num_IncrA,'Visible','on')
3649else
3650    set(handles.interval_txt,'Visible','off')
3651    set(handles.num_IncrA,'Visible','off')
3652end
3653update_plot(handles);
3654
3655%-------------------------------------------------------------------
3656function num_IncrA_Callback(hObject, eventdata, handles)
3657%-------------------------------------------------------------------
3658update_plot(handles);
3659
3660
3661%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3662% Vector representation
3663%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3664%-------------------------------------------------------------------
3665function CheckHideWarning_Callback(hObject, eventdata, handles)
3666%-------------------------------------------------------------------
3667update_plot(handles);
3668
3669%-------------------------------------------------------------------
3670function CheckHideFalse_Callback(hObject, eventdata, handles)
3671%-------------------------------------------------------------------
3672update_plot(handles);
3673
3674%-------------------------------------------------------------------
3675function num_VecScale_Callback(hObject, eventdata, handles)
3676%-------------------------------------------------------------------
3677set(handles.CheckFixVectors,'Value',1);
3678set(handles.CheckFixVectors,'BackgroundColor',[1 1 0])
3679update_plot(handles);
3680
3681%-------------------------------------------------------------------
3682function CheckFixVectors_Callback(hObject, eventdata, handles)
3683%-------------------------------------------------------------------
3684test=get(handles.CheckFixVectors,'Value');
3685if test
3686    set(handles.CheckFixVectors,'BackgroundColor',[1 1 0])
3687else
3688    update_plot(handles);
3689    %set(handles.num_VecScale,'String',num2str(ScalOut.num_VecScale,3))
3690    set(handles.CheckFixVectors,'BackgroundColor',[0.7 0.7 0.7])
3691end
3692
3693%------------------------------------------------------------------------
3694% --- Executes on selection change in CheckDecimate4 (nb_vec/4).
3695function CheckDecimate4_Callback(hObject, eventdata, handles)
3696%------------------------------------------------------------------------
3697update_plot(handles);
3698
3699%------------------------------------------------------------------------
3700% --- Executes on selection change in ColorCode menu
3701function ColorCode_Callback(hObject, eventdata, handles)
3702%------------------------------------------------------------------------
3703% edit the choice for color code
3704update_color_code_boxes(handles);
3705update_plot(handles);
3706
3707%------------------------------------------------------------------------
3708function update_color_code_boxes(handles)
3709%------------------------------------------------------------------------
3710list_code=get(handles.ColorCode,'String');% list menu fields
3711colcode= list_code{get(handles.ColorCode,'Value')}; % selected field
3712enable_slider='off';%default
3713enable_bounds='off';%default
3714enable_scalar='off';%default
3715switch colcode
3716    case {'rgb','bgr'}
3717        enable_slider='on';
3718        enable_bounds='on';
3719        enable_scalar='on';
3720    case '64 colors'
3721        enable_bounds='on';
3722        enable_scalar='on';
3723end
3724set(handles.Slider1,'Visible',enable_slider)
3725set(handles.Slider2,'Visible', enable_slider)
3726set(handles.num_ColCode1,'Visible',enable_slider)
3727set(handles.num_ColCode2,'Visible',enable_slider)
3728set(handles.TitleColCode1,'Visible',enable_slider)
3729set(handles.TitleColCode2,'Visible',enable_slider)
3730set(handles.CheckFixVecColor,'Visible',enable_bounds)
3731set(handles.num_MinVec,'Visible',enable_bounds)
3732set(handles.num_MaxVec,'Visible',enable_bounds)
3733set(handles.ColorScalar,'Visible',enable_scalar)
3734set_vec_col_bar(handles)
3735
3736%------------------------------------------------------------------
3737% --- Executes on selection change in ColorScalar: choice of the color code.
3738function ColorScalar_Callback(hObject, eventdata, handles)
3739%------------------------------------------------------------------
3740% edit the choice for color code
3741list_scalar=get(handles.ColorScalar,'String');% list menu fields
3742col_scalar= list_scalar{get(handles.ColorScalar,'Value')}; % selected field
3743if isequal(col_scalar,'ima_cor')
3744    set(handles.CheckFixVecColor,'Value',1)%fixed scale by default
3745    ColorCode='rgb';
3746    set(handles.num_MinVec,'String','0')
3747    set(handles.num_MaxVec,'String','1')
3748    set(handles.num_ColCode1,'String','0.333')
3749    set(handles.num_ColCode2,'String','0.666')
3750else
3751    set(handles.CheckFixVecColor,'Value',0)%auto scale between min,max by default
3752    ColorCode='64 colors';
3753end
3754ColorCodeList=get(handles.ColorCode,'String');
3755ichoice=find(strcmp(ColorCode,ColorCodeList),1);
3756set(handles.ColorCode,'Value',ichoice)% set color code in the menu
3757
3758update_color_code_boxes(handles);
3759%replot the current graph
3760run0_Callback(hObject, eventdata, handles)
3761
3762%----------------------------------------------------------------
3763% -- Executes on slider movement to set the color code
3764%
3765function Slider1_Callback(hObject, eventdata, handles)
3766%------------------------------------------------------------------
3767slider1=get(handles.Slider1,'Value');
3768min_val=str2num(get(handles.num_MinVec,'String'));
3769max_val=str2num(get(handles.num_MaxVec,'String'));
3770col=min_val+(max_val-min_val)*slider1;
3771set(handles.num_ColCode1,'String',num2str(col))
3772if(get(handles.Slider2,'Value') < col)%move also the second slider at the same value if needed
3773    set(handles.Slider2,'Value',col)
3774    set(handles.num_ColCode2,'String',num2str(col))
3775end
3776set_vec_col_bar(handles)
3777update_plot(handles);
3778
3779%----------------------------------------------------------------
3780% Executes on slider movement to set the color code
3781%----------------------------------------------------------------
3782function Slider2_Callback(hObject, eventdata, handles)
3783slider2=get(handles.Slider2,'Value');
3784min_val=str2num(get(handles.num_MinVec,'String'));
3785max_val=str2num(get(handles.num_MaxVec,'String'));
3786col=min_val+(max_val-min_val)*slider2;
3787set(handles.num_ColCode2,'String',num2str(col))
3788if(get(handles.Slider1,'Value') > col)%move also the first slider at the same value if needed
3789    set(handles.Slider1,'Value',col)
3790    set(handles.num_ColCode1,'String',num2str(col))
3791end
3792set_vec_col_bar(handles)
3793update_plot(handles);
3794
3795%----------------------------------------------------------------
3796% --- Execute on return carriage on the edit box corresponding to slider 1
3797%----------------------------------------------------------------
3798function num_ColCode1_Callback(hObject, eventdata, handles)
3799set_vec_col_bar(handles)
3800update_plot(handles);
3801
3802%----------------------------------------------------------------
3803% --- Execute on return carriage on the edit box corresponding to slider 2
3804%----------------------------------------------------------------
3805function num_ColCode2_Callback(hObject, eventdata, handles)
3806set_vec_col_bar(handles)
3807update_plot(handles);
3808%------------------------------------------------------------------------
3809%-------------------------------------------------------
3810% --- Executes on button press in CheckFixVecColor.
3811%-------------------------------------------------------
3812function VecColBar_Callback(hObject, eventdata, handles)
3813set_vec_col_bar(handles)
3814
3815%------------------------------------------------------------------------
3816% --- Executes on button press in CheckFixVecColor.
3817function CheckFixVecColor_Callback(hObject, eventdata, handles)
3818%------------------------------------------------------------------------
3819if ~get(handles.CheckFixVecColor,'Value')
3820    update_plot(handles);
3821end
3822
3823%------------------------------------------------------------------------
3824% --- Executes on selection change in num_MaxVec.
3825function num_MinVec_Callback(hObject, eventdata, handles)
3826%------------------------------------------------------------------------
3827max_vec_Callback(hObject, eventdata, handles)
3828
3829%------------------------------------------------------------------------
3830% --- Executes on selection change in num_MaxVec.
3831function num_MaxVec_Callback(hObject, eventdata, handles)
3832%------------------------------------------------------------------------
3833set(handles.CheckFixVecColor,'Value',1)
3834CheckFixVecColor_Callback(hObject, eventdata, handles)
3835min_val=str2num(get(handles.num_MinVec,'String'));
3836max_val=str2num(get(handles.num_MaxVec,'String'));
3837slider1=get(handles.Slider1,'Value');
3838slider2=get(handles.Slider2,'Value');
3839colcode1=min_val+(max_val-min_val)*slider1;
3840colcode2=min_val+(max_val-min_val)*slider2;
3841set(handles.num_ColCode1,'String',num2str(colcode1))
3842set(handles.num_ColCode2,'String',num2str(colcode2))
3843update_plot(handles);
3844
3845%------------------------------------------------------------------------
3846% --- update the display of color code for vectors (on vecColBar)
3847function set_vec_col_bar(handles)
3848%------------------------------------------------------------------------
3849%get the image of the color display button 'VecColBar' in pixels
3850set(handles.VecColBar,'Unit','pixel');
3851pos_vert=get(handles.VecColBar,'Position');
3852set(handles.VecColBar,'Unit','Normalized');
3853width=ceil(pos_vert(3));
3854height=ceil(pos_vert(4));
3855
3856%get slider indications
3857list=get(handles.ColorCode,'String');
3858ichoice=get(handles.ColorCode,'Value');
3859colcode.ColorCode=list{ichoice};
3860colcode.MinVec=str2num(get(handles.num_MinVec,'String'));
3861colcode.MaxVec=str2num(get(handles.num_MaxVec,'String'));
3862test3color=strcmp(colcode.ColorCode,'rgb') || strcmp(colcode.ColorCode,'bgr');
3863if test3color
3864    colcode.ColCode1=str2num(get(handles.num_ColCode1,'String'));
3865    colcode.ColCode2=str2num(get(handles.num_ColCode2,'String'));
3866end
3867vec_C=colcode.MinVec+(colcode.MaxVec-colcode.MinVec)*(0.5:width-0.5)/width;%sample of vec_C values from min to max
3868[colorlist,col_vec]=set_col_vec(colcode,vec_C);
3869oneheight=ones(1,height);
3870A1=colorlist(col_vec,1)*oneheight;
3871A2=colorlist(col_vec,2)*oneheight;
3872A3=colorlist(col_vec,3)*oneheight;
3873A(:,:,1)=A1';
3874A(:,:,2)=A2';
3875A(:,:,3)=A3';
3876set(handles.VecColBar,'Cdata',A)
3877
3878%-------------------------------------------------------------------
3879function update_plot(handles)
3880%-------------------------------------------------------------------
3881UvData=get(handles.uvmat,'UserData');
3882AxeData=UvData.axes3;% retrieve the current plotted data
3883PlotParam=read_GUI(handles.uvmat);
3884[tild,PlotParamOut]= plot_field(AxeData,handles.axes3,PlotParam);
3885write_plot_param(handles,PlotParamOut); %update the auto plot parameters
3886
3887%------------------------------------------------------------------------
3888%------------------------------------------------------------------------
3889%   SELECTION AND EDITION OF PROJECTION OBJECTS
3890%------------------------------------------------------------------------
3891%------------------------------------------------------------------------
3892
3893%------------------------------------------------------------------------
3894% --- Executes on selection change in ListObject.
3895function ListObject_Callback(hObject, eventdata, handles)
3896%------------------------------------------------------------------------
3897list_str=get(handles.ListObject,'String');
3898IndexObj_old=get(handles.ListObject,'UserData');%retrieve previous selection
3899IndexObj=get(handles.ListObject,'Value');%present object selection
3900
3901%% we select two objects or more at once (using the Ctrl key), keep only the last two items: the first is projected on uvmat, the second on view_field
3902if length(IndexObj)>2
3903     IndexObj=[IndexObj(end-1) IndexObj(end)];%keeps only the last two selected items at most
3904end
3905
3906%% we select one object
3907if length(IndexObj)==1%
3908    if length(IndexObj_old)>=2 && isequal(IndexObj_old(1),IndexObj)   % we select the first previously selected object->
3909        IndexObj=[1 IndexObj_old(2)];% it desactivates this object and selects the first object for uvmat
3910    elseif length(IndexObj_old)>=2 && isequal(IndexObj_old(2),IndexObj) % we select the second previously selected object->
3911        IndexObj=IndexObj_old(1);% it desactivates this object  and keeps only the first previously selected object (uvmat)
3912    else %
3913        IndexObj=[IndexObj_old(1) IndexObj];% activates a second object while keeping the first previously selected one
3914    end
3915end
3916 
3917%% The second object is selected, it is displayed in set_object if this GUI is already opened
3918UvData=get(handles.uvmat,'UserData');
3919ObjectData=UvData.Object{IndexObj(end)};
3920hset_object=findobj(allchild(0),'tag','set_object');
3921if ~isempty(hset_object)
3922    delete(hset_object)% delete to refesh the content
3923    ZBounds=0; % default
3924    if isfield(UvData.Field,'ZMin') && isfield(UvData.Field,'ZMax')
3925        ZBounds(1)=UvData.Field.ZMin; %minimum for the Z slider
3926        ZBounds(2)=UvData.Field.ZMax;%maximum for the Z slider
3927    end
3928    ObjectData.Name=list_str{IndexObj(end)};
3929    set_object(ObjectData,[],ZBounds);
3930end
3931%  desactivate the edit object mode
3932set(handles.edit_object,'Value',0)
3933set(handles.edit_object,'BackgroundColor',[0.7,0.7,0.7])
3934
3935%% update the object value stored in ListObject
3936set(handles.ListObject,'Value',IndexObj); % marks the selected objects in the list
3937set(handles.ListObject,'UserData',IndexObj)% keep the current object selection in memory for next time
3938%
3939% UvData=get(handles.uvmat,'UserData');%read UvData properties stored on the uvmat interface
3940% if numel(UvData.Object)<max(IndexObj);
3941%     msgbox_uvmat('ERROR','invalid object list')
3942%     return
3943% end
3944% if numel(IndexObj)>=2
3945%     UvData.Object=update_obj(UvData,IndexObj(1),IndexObj(2));%update the current object graphic representation
3946%     set(handles.uvmat,'UserData',UvData)
3947% end
3948
3949%% update the main plot (on uvmat) if the first selected object has been changed
3950if ~isequal(IndexObj(1),IndexObj_old(1))
3951    ProjData= proj_field(UvData.Field,ObjectData);%project the current interface field on UvData.Object{IndexObj(1)}
3952    plot_field(ProjData,handles.axes3,read_GUI(handles.uvmat));%read plotting parameters on the uvmat interfacPlotHandles);
3953end
3954
3955%% update the second plot (on view_field) if view_field is already openened
3956axes_view_field=[];%default
3957if length(IndexObj)==2 && (length(IndexObj_old)==1 || ~isequal(IndexObj(2),IndexObj_old(2)))
3958    hview_field=findobj(allchild(0),'tag','view_field');
3959    if ~isempty(hview_field)
3960        PlotHandles=guidata(hview_field);     
3961        ProjData= proj_field(UvData.Field,ObjectData);%project the current interface field on ObjectData
3962        axes_view_field=PlotHandles.axes3;
3963        plot_field(ProjData,axes_view_field,read_GUI(hview_field));%read plotting parameters on the uvmat interfacPlotHandles);
3964    end
3965end
3966
3967%% update the color of the graphic object representation: the selected object in magenta, others in blue
3968update_object_color(handles.axes3,axes_view_field,UvData.Object{IndexObj(end)}.DisplayHandle_uvmat)
3969
3970%------------------------------------------------------------------------
3971%--- update the color representation of objects (indicating the selected ones)
3972function update_object_color(axes_uvmat,axes_view_field,DisplayHandle)
3973%------------------------------------------------------------------------
3974if isempty(axes_view_field)% case with no view_field plot
3975hother=[findobj(axes_uvmat,'Tag','proj_object');findobj(axes_uvmat,'Tag','DeformPoint')];%find all the proj object and deform point representations
3976else
3977hother=[findobj(axes_uvmat,'Tag','proj_object') ;findobj(axes_view_field,'Tag','proj_object');... %find all the proj object representations
3978findobj(axes_uvmat,'Tag','DeformPoint'); findobj(axes_view_field,'Tag','DeformPoint')];%find all the deform point representations
3979end
3980for iobj=1:length(hother)
3981    if isequal(get(hother(iobj),'Type'),'rectangle')||isequal(get(hother(iobj),'Type'),'patch')
3982        set(hother(iobj),'EdgeColor','b')
3983        if isequal(get(hother(iobj),'FaceColor'),'m')
3984            set(hother(iobj),'FaceColor','b')
3985        end
3986    elseif isequal(get(hother(iobj),'Type'),'image')
3987        Acolor=get(hother(iobj),'CData');
3988        Acolor(:,:,1)=zeros(size(Acolor,1),size(Acolor,2));
3989        set(hother(iobj),'CData',Acolor);
3990    else
3991        set(hother(iobj),'Color','b')
3992    end
3993    set(hother(iobj),'Selected','off')
3994end
3995if ~isempty(DisplayHandle)
3996    linetype=get(DisplayHandle,'Type');
3997    if isequal(linetype,'line')
3998        set(DisplayHandle,'Color','m'); %set the selected object to magenta color
3999    elseif isequal(linetype,'rectangle')
4000        set(DisplayHandle,'EdgeColor','m'); %set the selected object to magenta color
4001    elseif isequal(linetype,'patch')
4002        set(DisplayHandle,'FaceColor','m'); %set the selected object to magenta color
4003    end
4004    SubObjectData=get(DisplayHandle,'UserData');
4005    if isfield(SubObjectData,'SubObject') & ishandle(SubObjectData.SubObject)
4006        for iobj=1:length(SubObjectData.SubObject)
4007            hsub=SubObjectData.SubObject(iobj);
4008            if isequal(get(hsub,'Type'),'rectangle')
4009                set(hsub,'EdgeColor','m'); %set the selected object to magenta color
4010            elseif isequal(get(hsub,'Type'),'image')
4011                Acolor=get(hsub,'CData');
4012                Acolor(:,:,1)=Acolor(:,:,3);
4013                set(hsub,'CData',Acolor);
4014            else
4015                set(hsub,'Color','m')
4016            end
4017        end
4018    end
4019    if isfield(SubObjectData,'DeformPoint') & ishandle(SubObjectData.DeformPoint)
4020        set(SubObjectData.DeformPoint,'Color','m')
4021    end
4022end
4023
4024%------------------------------------------------------------------------
4025% --- Executes on button press in ViewObject.
4026function ViewObject_Callback(hObject, eventdata, handles)
4027%------------------------------------------------------------------------
4028IndexObj=get(handles.ListObject,'Value');
4029% IndexObj=IndexObj(end); %keeps only the second value
4030UvData=get(handles.uvmat,'UserData');%read UvData properties stored on the uvmat interface
4031if numel(UvData.Object)<IndexObj(end);% error in UvData
4032    msgbox_uvmat('ERROR','invalid object list')
4033    return
4034end
4035% ObjectData=UvData.Object{IndexObj(};
4036ZBounds=0; % default
4037if isfield(UvData.Field,'ZMin') && isfield(UvData.Field,'ZMax')
4038    ZBounds(1)=UvData.Field.ZMin; %minimum for the Z slider
4039    ZBounds(2)=UvData.Field.ZMax;%maximum for the Z slider
4040end
4041hset_object=findobj(allchild(0),'tag','set_object');
4042if ~isempty(hset_object)
4043    delete(hset_object)% delete existing version of set_object
4044end
4045set(handles.ListObject,'Value',IndexObj);%restore ListObject selection after set_object deletion
4046if ~isfield(UvData.Object{IndexObj(1)},'Type')% default plane
4047    UvData.Object{IndexObj(1)}.Type='plane';
4048end
4049list_object=get(handles.ListObject,'String');
4050UvData.Object{IndexObj(end)}.Name=list_object{IndexObj(end)};
4051hset_object=set_object(UvData.Object{IndexObj(end)},[],ZBounds);
4052hhset_object=guidata(hset_object);
4053if get(handles.edit_object,'Value')% edit mode
4054    set(hhset_object.PLOT,'Enable','on')
4055else
4056    set(hhset_object.PLOT,'Enable','off')
4057end
4058
4059%% show the second plot (on view_field)
4060if length(IndexObj)==2
4061    ProjData= proj_field(UvData.Field,UvData.Object{IndexObj(2)});%project the current field on ObjectData
4062    hview_field=findobj(allchild(0),'tag','view_field');
4063    if isempty(hview_field)
4064        hview_field=view_field;
4065    end
4066    PlotHandles=guidata(hview_field);     
4067    plot_field(ProjData,PlotHandles.axes3,read_GUI(hview_field));%read plotting parameters on the uvmat interfacPlotHandles);
4068end
4069
4070%-------------------------------------------------------------------
4071% --- Executes on selection change in edit_object.
4072function edit_object_Callback(hObject, eventdata, handles)
4073%-------------------------------------------------------------------
4074UvData=get(handles.uvmat,'UserData');%read UvData properties stored on the uvmat interface
4075if get(handles.edit_object,'Value')
4076    set(handles.edit_object,'BackgroundColor',[1,1,0]) 
4077    %suppress the other options
4078    set(handles.CheckZoom,'Value',0)
4079    CheckZoom_Callback(hObject, eventdata, handles)
4080    hgeometry_calib=findobj(allchild(0),'tag','geometry_calib');
4081    if ishandle(hgeometry_calib)
4082        hhgeometry_calib=guidata(hgeometry_calib);
4083        set(hhgeometry_calib.edit_append,'Value',0)% desactivate mouse action in geometry_calib
4084        set(hhgeometry_calib.edit_append,'BackgroundColor',[0.7 0.7 0.7])
4085    end
4086    hset_object=findobj(allchild(0),'Tag','set_object');
4087    if isempty(hset_object)% open the GUI set_object with data of the currently selected object
4088        ViewObject_Callback(hObject, eventdata, handles)
4089        hset_object=findobj(allchild(0),'Tag','set_object');
4090    end
4091    hhset_object=guidata(hset_object);
4092    set(hhset_object.PLOT,'enable','on');
4093else
4094    set(handles.edit_object,'BackgroundColor',[0.7,0.7,0.7]) 
4095    hset_object=findobj(allchild(0),'Tag','set_object');
4096    if ~isempty(hset_object)% open the
4097        hhset_object=guidata(hset_object);
4098        set(hhset_object.PLOT,'enable','off');
4099    end
4100end
4101
4102%------------------------------------------------------------------------
4103% --- Executes on button press in delete_object.
4104function delete_object_Callback(hObject, eventdata, handles)
4105%------------------------------------------------------------------------
4106IndexObj=get(handles.ListObject,'Value');
4107if IndexObj(end)>1
4108    delete_object(IndexObj(end))
4109end
4110
4111%------------------------------------------------------------------------
4112%------------------------------------------------------------------------
4113%  II - TOOLS FROM THE UPPER MENU BAR
4114%------------------------------------------------------------------------
4115%------------------------------------------------------------------------
4116
4117%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4118% Export  Menu Callbacks
4119%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4120%------------------------------------------------------------------------
4121% --- Executes on button press in Menu/Export/field in workspace.
4122function MenuExportField_Callback(hObject, eventdata, handles)
4123%------------------------------------------------------------------------
4124global Data_uvmat
4125Data_uvmat=get(handles.uvmat,'UserData');
4126evalin('base','global Data_uvmat')%make CurData global in the workspace
4127display('current field :')
4128evalin('base','Data_uvmat') %display CurData in the workspace
4129commandwindow; %brings the Matlab command window to the front
4130
4131%------------------------------------------------------------------------
4132% --- Executes on button press in Menu/Export/extract figure.
4133function MenuExportFigure_Callback(hObject, eventdata, handles)
4134%------------------------------------------------------------------------
4135% huvmat=get(handles.MenuExport,'parent');
4136hfig=figure;
4137copyobj(handles.axes3,hfig);
4138map=colormap(handles.axes3);
4139colormap(map);%transmit the current colormap to the zoom fig
4140colorbar
4141
4142% %------------------------------------------------------
4143% % --- Executes on button press in Menu/Export/extract figure.
4144% %------------------------------------------------------
4145% function MenuExport_plot_Callback(hObject, eventdata, handles)
4146% huvmat=get(handles.MenuExport_plot,'parent');
4147% UvData=get(huvmat,'UserData');
4148% hfig=figure;
4149% newaxes=copyobj(handles.axes3,hfig);
4150% map=colormap(handles.axes3);
4151% colormap(map);%transmit the current colormap to the zoom fig
4152% colorbar
4153
4154%
4155% % --------------------------------------------------------------------
4156% function Insert_Callback(hObject, eventdata, handles)
4157%
4158
4159%------------------------------------------------------------------------
4160% --------------------------------------------------------------------
4161function MenuExportMovie_Callback(hObject, eventdata, handles)
4162% --------------------------------------------------------------------
4163set(handles.MenuExportMovie,'BusyAction','queue')% activate the button
4164huvmat=get(handles.run0,'parent');
4165UvData=get(huvmat,'UserData');
4166%[xx,xx,FileBase]=read_file_boxes(handles);
4167[RootPath,SubDir,RootFile,FileIndices,FileExt]=read_file_boxes(handles);
4168FileBase=fullfile(RootPath,RootFile);
4169 %read the current input file name
4170prompt = {'movie file name';'frames per second';'frame resolution (*[512x384] pixels)';'axis position relative to the frame';'total frame number (starting from the current uvmat display)'};
4171dlg_title = 'select properties of the output avi movie';
4172num_lines= 1;
4173% nbfield_cell=get(handles.last_i,'String');
4174def     = {[FileBase '_out.avi'];'10';'1';'[0.03 0.05 0.95 0.92]';'10'};
4175answer = inputdlg(prompt,dlg_title,num_lines,def,'on');
4176aviname=answer{1};
4177fps=str2double(answer{2});
4178% check for existing file with output name aviname
4179if exist(aviname,'file')
4180    backup=aviname;
4181    testexist=2;
4182    while testexist==2
4183        backup=[backup '~'];
4184        testexist=exist(backup,'file');     
4185    end
4186    [success,message]=copyfile(aviname,backup);%make backup of the existing file
4187    if isequal(success,1)
4188        delete(aviname)%delete existing file
4189    else
4190        msgbox_uvmat('ERROR',message)
4191        return
4192    end
4193end
4194%create avi open
4195aviobj=avifile(aviname,'Compression','None','fps',fps);
4196
4197%display first view for tests
4198newfig=figure;
4199newaxes=copyobj(handles.axes3,newfig);%new plotting axes in the new figure
4200set(newaxes,'Tag','movieaxes')
4201nbpix=[512 384]*str2double(answer{3});
4202set(gcf,'Position',[1 1 nbpix])% resolution XVGA
4203set(newaxes,'Position',eval(answer{4}));
4204map=colormap(handles.axes3);
4205colormap(map);%transmit the current colormap to the zoom fig
4206msgbox_uvmat('INPUT_Y-N',{['adjust figure ' num2str(newfig) ' with its matlab edit menu '] ;...
4207        ['then press OK to get the avi movie as a copy of figure ' num2str(newfig) ' display']});
4208UvData.plotaxes=newaxes;% the axis in the new figure becomes the current main plotting axes
4209set(huvmat,'UserData',UvData);
4210increment=str2num(get(handles.increment_scan,'String')); %get the field increment d
4211set(handles.STOP,'Visible','on')
4212set(handles.speed,'Visible','on')
4213set(handles.speed_txt,'Visible','on')
4214set(handles.Movie,'BusyAction','queue')
4215
4216%imin=str2double(get(handles.i1,'String'));
4217imax=str2double(answer{5});
4218% if isfield(UvData,'Time')
4219htitle=get(newaxes,'Title');
4220xlim=get(newaxes,'XLim');
4221ylim=get(newaxes,'YLim');
4222set(htitle,'Position',[xlim(2)+0.07*(xlim(2)-xlim(1)) ylim(2)-0.05*(ylim(2)-ylim(1)) 0])
4223time_str=get(handles.abs_time,'String');
4224set(htitle,'String',['t=' time_str])
4225set(handles.speed,'Value',1)
4226for i=1:imax
4227    if get(handles.speed,'Value')~=0 && isequal(get(handles.MenuExportMovie,'BusyAction'),'queue') % enable STOP command
4228            runpm(hObject,eventdata,handles,increment)% run plus
4229            drawnow
4230            time_str=get(handles.abs_time,'String');
4231            if ishandle(htitle)
4232             set(htitle,'String',['t=' time_str])
4233            end
4234            mov=getframe(newfig);
4235            aviobj=addframe(aviobj,mov);
4236    end
4237end
4238aviobj=close(aviobj);
4239UvData=rmfield(UvData,'plotaxes');
4240%UvData.Object{1}.plotaxes=handles.axes3;
4241set(huvmat,'UserData',UvData);
4242msgbox_uvmat('CONFIRMATION',{['movie ' aviname ' created '];['with ' num2str(imax) ' frames']})
4243
4244
4245%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4246% Projection Objects Menu Callbacks
4247%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4248
4249% -----------------------------------------------------------------------
4250function Menupoints_Callback(hObject, eventdata, handles)
4251%------------------------------------------------------------------------
4252data.Type='points';
4253data.ProjMode='projection';%default
4254create_object(data,handles)
4255
4256% -----------------------------------------------------------------------
4257function Menuline_Callback(hObject, eventdata, handles)
4258%------------------------------------------------------------------------
4259data.Type='line';
4260data.ProjMode='projection';%default
4261create_object(data,handles)
4262
4263%------------------------------------------------------------------------
4264function Menupolyline_Callback(hObject, eventdata, handles)
4265%------------------------------------------------------------------------
4266data.Type='polyline';
4267data.ProjMode='projection';%default
4268create_object(data,handles)
4269
4270%------------------------------------------------------------------------
4271function Menupolygon_Callback(hObject, eventdata, handles)
4272%------------------------------------------------------------------------
4273data.Type='polygon';
4274data.ProjMode='inside';%default
4275create_object(data,handles)
4276
4277%------------------------------------------------------------------------
4278function Menurectangle_Callback(hObject, eventdata, handles)
4279%------------------------------------------------------------------------
4280data.Type='rectangle';
4281data.ProjMode='inside';%default
4282create_object(data,handles)
4283
4284%------------------------------------------------------------------------
4285function Menuellipse_Callback(hObject, eventdata, handles)
4286%------------------------------------------------------------------------
4287data.Type='ellipse';
4288data.ProjMode='inside';%default
4289create_object(data,handles)
4290
4291%------------------------------------------------------------------------
4292function MenuMaskObject_Callback(hObject, eventdata, handles)
4293%------------------------------------------------------------------------
4294data.Type='polygon';
4295data.TypeMenu={'polygon'};
4296data.ProjMode='mask_inside';%default
4297data.ProjModeMenu={'mask_inside';'mask_outside'};
4298create_object(data,handles)
4299
4300%------------------------------------------------------------------------
4301function Menuplane_Callback(hObject, eventdata, handles)
4302%------------------------------------------------------------------------
4303data.Type='plane';
4304data.ProjMode='projection';%default
4305create_object(data,handles)
4306
4307%------------------------------------------------------------------------
4308function Menuvolume_Callback(hObject, eventdata, handles)
4309%------------------------------------------------------------------------
4310data.Type='volume';
4311data.ProjMode='interp';%default
4312% set(handles.create,'Visible','on')
4313% set(handles.create,'Value',1)
4314% VOLUME_Callback(hObject,eventdata,handles)
4315create_object(data,handles)
4316
4317%------------------------------------------------------------------------
4318% --- generic function used for the creation of a projection object
4319function create_object(data,handles)
4320%------------------------------------------------------------------------
4321
4322hgeometry_calib=findobj(allchild(0),'tag','geometry_calib');
4323if ishandle(hgeometry_calib)
4324    hhgeometry_calib=guidata(hgeometry_calib);
4325    set(hhgeometry_calib.edit_append,'Value',0)% desactivate mouse action in geometry_calib
4326    set(hhgeometry_calib.edit_append,'BackgroundColor',[0.7 0.7 0.7])
4327end
4328UvData=get(handles.uvmat,'UserData');
4329set(handles.edit_object,'Value',0); %suppress the object edit mode
4330set(handles.edit_object,'BackgroundColor',[0.7,0.7,0.7]) 
4331% data.enable_plot=1;
4332data.Coord=[0 0]; %default
4333if isfield(UvData,'Field')
4334    Field=UvData.Field;
4335    if isfield(UvData.Field,'Mesh')&&~isempty(UvData.Field.Mesh)
4336        data.RangeX=[UvData.Field.XMin UvData.Field.XMax];
4337        if strcmp(data.Type,'line')||strcmp(data.Type,'polyline')
4338            data.RangeY=UvData.Field.Mesh;
4339        else
4340            data.RangeY=[UvData.Field.YMin UvData.Field.YMax];
4341        end
4342        data.DX=UvData.Field.Mesh;
4343        data.DY=UvData.Field.Mesh;
4344       
4345%     elseif isfield(Field,'AX')&& isfield(Field,'AY')&& isfield(Field,'A')%only image
4346%         np=size(Field.A);
4347%         meshx=(Field.AX(end)-Field.AX(1))/np(2);
4348%         meshy=abs(Field.AY(end)-Field.AY(1))/np(1);
4349%         data.RangeY=max(meshx,meshy);
4350%         data.RangeX=max(meshx,meshy);
4351%         data.DX=max(meshx,meshy);
4352    end
4353    if isfield(Field,'NbDim')&& isequal(Field.NbDim,3)
4354         data.Coord=[0 0 0]; %default
4355    end
4356    if isfield(Field,'CoordUnit')
4357        data.CoordUnit=Field.CoordUnit;
4358    end
4359end
4360if ishandle(handles.UVMAT_title)
4361    delete(handles.UVMAT_title)%delete the initial display of uvmat if no field has been entered
4362end
4363hset_object=findobj(allchild(0),'tag','set_object');
4364IndexObj=get(handles.ListObject,'Value');
4365if ~isempty(hset_object)
4366    delete(hset_object)% delete existing version of set_object
4367end
4368set(handles.ListObject,'Value',IndexObj)
4369hset_object=set_object(data,handles);% call the set_object interface
4370hhset_object=guidata(hset_object);
4371set(hhset_object.PLOT,'enable','on')% activate the refresh button
4372set(handles.MenuObject,'checked','on')
4373set(handles.uvmat,'UserData',UvData)
4374set(handles.CheckZoom,'Value',0)
4375CheckZoom_Callback(handles.uvmat, [], handles)
4376set(handles.delete_object,'Visible','on')
4377
4378
4379%------------------------------------------------------------------------
4380function MenuBrowseObject_Callback(hObject, eventdata, handles)
4381%------------------------------------------------------------------------
4382%get the object file
4383[FileName, PathName, filterindex] = uigetfile( ...
4384       {'*.xml;*.mat', ' (*.xml,*.mat)';
4385       '*.xml',  '.xml files '; ...
4386        '*.mat',  '.mat matlab files '}, ...
4387        'Pick an xml Object file',get(handles.RootPath,'String'));
4388fileinput=[PathName FileName];%complete file name
4389sizf=size(fileinput);
4390if (~ischar(fileinput)||~isequal(sizf(1),1)),return;end
4391
4392%read the file
4393data=xml2struct(fileinput);
4394data.enable_plot=1;
4395[tild,data.Name]=fileparts(FileName);
4396hset_object=findobj(allchild(0),'tag','set_object');
4397if ~isempty(hset_object)
4398    delete(hset_object)% delete existing version of set_object
4399end
4400set_object(data);% call the set_object interface
4401set(handles.edit_object,'Value',0); %suppress the object edit mode
4402set(handles.edit_object,'BackgroundColor',[0.7,0.7,0.7]) 
4403set(handles.MenuObject,'checked','on')
4404set(handles.delete_object,'Visible','on')
4405
4406%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4407% MenuEdit Callbacks
4408%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4409%------------------------------------------------------------------------
4410function MenuEditObject_Callback(hObject, eventdata, handles)
4411%------------------------------------------------------------------------
4412set(handles.edit_object,'Value',1)
4413edit_Callback(hObject, eventdata, handles)
4414
4415% %------------------------------------------------------------------------
4416% function enable_transform(handles,state)
4417% %------------------------------------------------------------------------
4418% set(handles.transform_fct,'Visible',state)
4419% set(handles.TRANSFORM_txt,'Visible',state)   
4420% set(handles.transform_fct,'Visible',state) 
4421% set(handles.path_transform,'Visible',state)
4422% set(handles.pxcmx_txt,'Visible',state)
4423% set(handles.pxcmy_txt,'Visible',state)
4424% set(handles.pxcm,'Visible',state)
4425% set(handles.pycm,'Visible',state)
4426
4427%------------------------------------------------------------------------
4428function MenuEditVectors_Callback(hObject, eventdata, handles)
4429%------------------------------------------------------------------------
4430set(handles.edit_vect,'Visible','on')
4431set(handles.edit_vect,'Value',1)
4432edit_vect_Callback(hObject, eventdata, handles)
4433
4434%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4435% MenuTools Callbacks
4436%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4437%------------------------------------------------------------------------
4438function MenuCalib_Callback(hObject, eventdata, handles)
4439%------------------------------------------------------------------------
4440
4441UvData=get(handles.uvmat,'UserData');%read UvData properties stored on the uvmat interface
4442
4443%suppress competing options
4444set(handles.CheckZoom,'Value',0)
4445set(handles.CheckZoom,'BackgroundColor',[0.7 0.7 0.7])
4446set(handles.ListObject,'Value',1)     
4447% initiate display of GUI geometry_calib
4448data=[]; %default
4449if isfield(UvData,'CoordType')
4450    data.CoordType=UvData.CoordType;
4451end
4452pos=get(handles.uvmat,'Position');
4453pos(1)=pos(1)+pos(3)-0.311+0.04; %0.311= width of the geometry_calib interface (units relative to the srcreen)
4454pos(2)=pos(2)-0.02;
4455[RootPath,SubDir,RootFile,FileIndices,FileExt]=read_file_boxes(handles);
4456FileName=[fullfile(RootPath,SubDir,RootFile) FileIndices FileExt];
4457set(handles.view_xml,'Backgroundcolor',[1 1 0])%indicate the reading of the current xml file by geometry_calib
4458if isfield(UvData.OpenParam,'CalOrigin')
4459    pos_uvmat=get(handles.uvmat,'Position');
4460    pos_cal(1)=pos_uvmat(1)+UvData.OpenParam.CalOrigin(1)*pos_uvmat(3);
4461    pos_cal(2)=pos_uvmat(2)+UvData.OpenParam.CalOrigin(2)*pos_uvmat(4);
4462    pos_cal(3:4)=UvData.OpenParam.CalSize .* pos_uvmat(3:4);
4463end
4464geometry_calib(FileName,pos_cal);% call the geometry_calib interface   
4465set(handles.view_xml,'Backgroundcolor',[1 1 1])%indicate the end of reading of the current xml file by geometry_calib
4466set(handles.MenuCalib,'checked','on')% indicate that MenuCalib is activated, test used by mouse action
4467
4468%------------------------------------------------------------------------
4469function MenuMask_Callback(hObject, eventdata, handles)
4470%------------------------------------------------------------------------
4471UvData=get(handles.uvmat,'UserData');%read UvData properties stored on the uvmat interface
4472ListObj=UvData.Object;
4473select=zeros(1,numel(ListObj));
4474for iobj=1:numel(ListObj);
4475    if strcmp(ListObj{iobj}.ProjMode,'mask_inside')||strcmp(ListObj{iobj}.ProjMode,'mask_outside')
4476        select(iobj)=1;
4477    end
4478end
4479val=find(select);
4480if isempty(val)
4481    msgbox_uvmat('ERROR','polygons must be first created by Projection object/mask polygon in the menu bar');
4482    return
4483else
4484    set(handles.ListObject,'Max',2);%allow multiple selection
4485    set(handles.ListObject,'Value',val);
4486    flag=1;
4487    npx=size(UvData.Field.A,2);
4488    npy=size(UvData.Field.A,1);
4489    xi=0.5:npx-0.5;
4490    yi=0.5:npy-0.5;
4491    [Xi,Yi]=meshgrid(xi,yi);
4492    if isfield(UvData,'Object')
4493        for iobj=1:length(UvData.Object)
4494            ObjectData=UvData.Object{iobj};
4495            if isfield(ObjectData,'ProjMode') &&(isequal(ObjectData.ProjMode,'mask_inside')||isequal(ObjectData.ProjMode,'mask_outside'));
4496                flagobj=1;
4497                testphys=0; %coordinates in pixels by default
4498                if isfield(ObjectData,'CoordUnit') && ~isequal(ObjectData.CoordUnit,'pixel')
4499                    if isfield(UvData,'XmlData')&& isfield(UvData.XmlData{1},'GeometryCalib')
4500                        Calib=UvData.XmlData{1}.GeometryCalib;
4501                        testphys=1;
4502                    end
4503                end
4504                if isfield(ObjectData,'Coord')&& isfield(ObjectData,'Type')
4505                    if isequal(ObjectData.Type,'polygon')
4506                        X=ObjectData.Coord(:,1);
4507                        Y=ObjectData.Coord(:,2);
4508                        if testphys
4509                            [X,Y]=px_XYZ(Calib,X,Y,0);% to generalise with 3D cases
4510                        end
4511                        flagobj=~inpolygon(Xi,Yi,X',Y');%=0 inside the polygon, 1 outside
4512                    elseif isequal(ObjectData.Type,'ellipse')
4513                        if testphys
4514                            %[X,Y]=px_XYZ(Calib,X,Y,0);% TODO:create a polygon boundary and transform to phys
4515                        end
4516                        RangeX=max(ObjectData.RangeX);
4517                        RangeY=max(ObjectData.RangeY);
4518                        X2Max=RangeX*RangeX;
4519                        Y2Max=RangeY*RangeY;
4520                        distX=(Xi-ObjectData.Coord(1,1));
4521                        distY=(Yi-ObjectData.Coord(1,2));
4522                        flagobj=(distX.*distX/X2Max+distY.*distY/Y2Max)>1;
4523                    elseif isequal(ObjectData.Type,'rectangle')
4524                        if testphys
4525                            %[X,Y]=px_XYZ(Calib,X,Y,0);% TODO:create a polygon boundary and transform to phys
4526                        end
4527                        distX=abs(Xi-ObjectData.Coord(1,1));
4528                        distY=abs(Yi-ObjectData.Coord(1,2));
4529                        flagobj=distX>max(ObjectData.RangeX) | distY>max(ObjectData.RangeY);
4530                    end
4531                    if isequal(ObjectData.ProjMode,'mask_outside')
4532                        flagobj=~flagobj;
4533                    end
4534                    flag=flag & flagobj;
4535                end
4536            end
4537        end
4538    end
4539    %mask name
4540    RootPath=get(handles.RootPath,'String');
4541    RootFile=get(handles.RootFile,'String');
4542    if ~isempty(RootFile)&&(isequal(RootFile(1),'/')|| isequal(RootFile(1),'\'))
4543        RootFile(1)=[];
4544    end
4545    filebase=fullfile(RootPath,RootFile);
4546    list=get(handles.masklevel,'String');
4547    masknumber=num2str(length(list));
4548    maskindex=get(handles.masklevel,'Value');
4549    mask_name=fullfile_uvmat(RootPath,'',[RootFile '_' masknumber 'mask'],'.png','_1',maskindex);
4550    %mask_name=name_generator([filebase '_' masknumber 'mask'],maskindex,1,'.png','_i');
4551    imflag=uint8(255*(0.392+0.608*flag));% =100 for flag=0 (vectors not computed when 20<imflag<200)
4552    imflag=flipdim(imflag,1);
4553
4554    %display the mask
4555    hfigmask=figure;
4556    set(hfigmask,'Name','mask image')
4557    vec=linspace(0,1,256);%define a linear greyscale colormap
4558    map=[vec' vec' vec'];
4559    colormap(map)
4560    image(imflag);
4561    answer=msgbox_uvmat('INPUT_TXT','mask file name:', mask_name);
4562    if ~strcmp(answer,'Cancel')
4563        mask_dir=fileparts(answer);
4564        if ~exist(mask_dir,'dir')
4565            msgbox_uvmat('ERROR',['directory ' mask_dir ' does not exist'])
4566            return
4567        end
4568        imwrite(imflag,answer,'BitDepth',8);
4569    end
4570    set(handles.ListObject,'Value',1)
4571%     set(handles.ListObject,'Max',1)
4572end
4573
4574%------------------------------------------------------------------------
4575%-- open the GUI set_grid.fig to create grid
4576function MenuGrid_Callback(hObject, eventdata, handles)
4577%------------------------------------------------------------------------
4578%suppress the other options if grid is chosen
4579set(handles.edit_vect,'Value',0)
4580edit_vect_Callback(hObject, eventdata, handles)
4581set(handles.edit_object,'BackgroundColor',[0.7 0.7 0.7])
4582set(handles.ListObject,'Value',1)     
4583
4584%prepare display of the set_grid GUI
4585[RootPath,SubDir,RootFile,FileIndices,FileExt]=read_file_boxes(handles);
4586FileName=[fullfile(RootPath,SubDir,RootFile) FileIndices FileExt];
4587CoordList=get(handles.transform_fct,'String');
4588val=get(handles.transform_fct,'Value');
4589set_grid(FileName,CoordList{val});% call the set_object interface
4590
4591
4592%------------------------------------------------------------------------
4593function MenuRuler_Callback(hObject, eventdata, handles)
4594%------------------------------------------------------------------------
4595set(handles.CheckZoom,'Value',0)
4596CheckZoom_Callback(handles.uvmat, [], handles)
4597set(handles.MenuRuler,'checked','on')
4598UvData=get(handles.uvmat,'UserData');
4599UvData.MouseAction='ruler';
4600set(handles.uvmat,'UserData',UvData);
4601
4602%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4603% MenuRun Callbacks
4604%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4605
4606%------------------------------------------------------------------------
4607% open the GUI 'series'
4608function MenuSeries_Callback(hObject, eventdata, handles)
4609%------------------------------------------------------------------------
4610series; %first display of the GUI to fill waiting time
4611[RootPath,SubDir,RootFile,FileIndices,FileExt]=read_file_boxes(handles);
4612param.FileName=[fullfile(RootPath,SubDir,RootFile) FileIndices FileExt];
4613if isequal(get(handles.SubField,'Value'),1)
4614    [RootPath_1,SubDir_1,RootFile_1,FileIndices_1,FileExt_1]=read_file_boxes_1(handles);
4615    FileName_1=[fullfile(RootPath_1,SubDir_1,RootFile_1) FileIndices_1 FileExt_1];
4616    if ~isequal(FileName_1,param.FileName)
4617        param.FileName_1=FileName_1;
4618    end
4619end
4620param.NomType=get(handles.NomType,'String');
4621param.NomType_1=get(handles.NomType_1,'String');
4622param.CheckFixPair=get(handles.CheckFixPair,'Value');
4623huvmat=get(handles.MenuSeries,'parent');
4624UvData=get(huvmat,'UserData');
4625if isfield(UvData,'XmlData')&& isfield(UvData.XmlData{1},'Time')
4626    param.Time=UvData.XmlData{1}.Time;
4627end
4628if isequal(get(handles.scan_i,'Value'),1)
4629    param.incr_i=str2num(get(handles.increment_scan,'String'));
4630elseif isequal(get(handles.scan_j,'Value'),1)
4631    param.incr_j=str2num(get(handles.increment_scan,'String'));
4632end
4633param.list_fields=get(handles.Fields,'String');% list menu fields
4634param.list_fields(1)=[]; %suppress  'image' option
4635param.index_fields=get(handles.Fields,'Value');% selected string index
4636if param.index_fields>1
4637    param.index_fields=param.index_fields-1;
4638end
4639param.list_fields_1=get(handles.Fields_1,'String');% list menu fields
4640param.list_fields_1(1)=[]; %suppress  'image' option
4641param.index_fields_1=get(handles.Fields_1,'Value')-1;% selected string index
4642if param.index_fields_1>1
4643    param.index_fields_1=param.index_fields_1-1;
4644end
4645param.menu_coord_str=get(handles.transform_fct,'String');
4646param.menu_coord_val=get(handles.transform_fct,'Value');
4647series(param); %run the series interface
4648
4649%------------------------------------------------------------------------
4650% -- open the GUI civ.fig for PIV
4651function MenuPIV_Callback(hObject, eventdata, handles)
4652%------------------------------------------------------------------------
4653 [RootPath,SubDir,RootFile,FileIndices,FileExt]=read_file_boxes(handles);
4654 FileName=[fullfile(RootPath,SubDir,RootFile) FileIndices FileExt];
4655civ(FileName);% interface de civ(not in the uvmat file)
4656
4657% --------------------------------------------------------------------
4658function MenuHelp_Callback(hObject, eventdata, handles)
4659% --------------------------------------------------------------------
4660path_to_uvmat=which ('uvmat');% check the path of uvmat
4661pathelp=fileparts(path_to_uvmat);
4662helpfile=fullfile(pathelp,'uvmat_doc','uvmat_doc.html');
4663if isempty(dir(helpfile)), msgbox_uvmat('ERROR','Please put the help file uvmat_doc.html in the sub-directory /uvmat_doc of the UVMAT package')
4664else
4665    addpath (fullfile(pathelp,'uvmat_doc'))
4666    web(helpfile);
4667end
Note: See TracBrowser for help on using the repository browser.