source: trunk/src/uvmat.m @ 407

Last change on this file since 407 was 407, checked in by sommeria, 9 years ago

bugs corrected for object projection in uvmat

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