source: trunk/src/uvmat.m @ 469

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

several bugs corrected
introductyion of functions find_imadoc and read_multimadoc which simplifiy the functions under series

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