source: trunk/src/uvmat.m @ 150

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

various corrections for plotting and using view_field and get_field. The current field of uvmat or view_field is now stored in the structure userdata.axes3. Introduction of live correlation to test PIV (civ1)

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