source: trunk/src/plot_field.m @ 71

Last change on this file since 71 was 71, checked in by sommeria, 11 years ago

civ3D updated: introduction of image size
imadoc2struct: reding of image size from the xml file
set_object, view_field and related functions: improvement of projection object editing
mouse: possibility of adjusting the calibrations points with the mouse

File size: 47.4 KB
Line 
1%'plot_field': plot any field with the structure defined in the uvmat package
2%------------------------------------------------------------------------
3%
4%  This function is used by uvmat to plot fields. It automatically chooses the representation
5% appropriate to the input field structure:
6%     2D vector fields are represented by arrows, 2D scalar fiedlds by grey scale images or contour plots, 1D fields are represented by usual plot with (abscissa, ordinate).
7%  The input field structure is first tested by check_field_structure.m,
8%  then split into blocks of related variables  by find_field_indices.m.
9%  The dimensionality of each block is obtained  by this fuction
10%  considering the presence of variables with the attribute .Role='coord_x'
11%  and/or coord_y and/or coord_z (case of unstructured coordinates), or
12%  dimension variables (case of matrices).
13%
14% function [PlotType,PlotParamOut,haxes]= plot_field(Data,haxes,PlotParam,KeepLim,PosColorbar)
15%
16% OUPUT:
17% PlotType: type of plot: 'text','line'(curve plot),'plane':2D view,'volume'
18% PlotParamOut: structure, representing the updated  plotting parameters, in case of automatic scaling
19% haxes: handle of the plotting axis, when a new figure is created.
20%
21%INPUT
22%    Data:   structure describing the field to plot
23%         (optional) .ListGlobalAttribute: cell listing the names of the global attributes
24%                    .Att_1,Att_2... : values of the global attributes
25%         (requested)  .ListVarName: list of variable names to select (cell array of  char strings {'VarName1', 'VarName2',...} )
26%         (requested)  .VarDimName: list of dimension names for each element of .ListVarName (cell array of string cells)
27%                      .VarAttribute: cell of attributes for each element of .ListVarName (cell array of structures of the form VarAtt.key=value)
28%         (requested) .Var1, .Var2....: variables (Matlab arrays) with names listed in .ListVarName
29
30%            Variable attribute .Role :
31%    The only variable attribute used for plotting purpose is .Role which can take
32%    the values
33        %       Role = 'scalar':  (default) represents a scalar field
34        %            = 'coord_x', 'coord_y',  'coord_z': represents a separate set of
35        %                        unstructured coordinate x, y  or z
36        %            = 'vector': represents a vector field whose number of components
37        %                is given by the last dimension (called 'nb_dim')
38        %            = 'vector_x', 'vector_y', 'vector_z'  :represents the x, y or z  component of a vector 
39        %            = 'warnflag' : provides a warning flag about the quality of data in a 'Field', default=0, no warning
40        %            = 'errorflag': provides an error flag marking false data,
41        %                   default=0, no error. Different non zero values can represent different criteria of elimination.
42
43
44%         additional elements characterizing the projection object (should not be necessary)--
45%            Data.Style : style of projection object
46%            Data.XObject,.YObject: set of coordinates defining the object position;
47%            Data.ProjMode=type of projection ;
48%            Data.ProjAngle=angle of projection;
49%            Data.DX,.DY,.DZ=increments;
50%            Data.MaxY,MinY: min and max Y
51
52%   haxes: handle of the plotting axes to update with the new plot. If this input is absent or not a valid axes handle, a new figure is created.
53%
54%   PlotParam: parameters for plotting, as read on the uvmat interface (by function 'read_plot_param.m')
55%            --scalars--
56%    .Scalar.MaxA: upper bound (saturation color) for the scalar representation, max(field) by default
57%    .Scalar.MinA: lower bound (saturation) for the scalar representation, min(field) by default
58%    .Scalar.AutoScal: =1 (default) lower and upper bounds of the scalar representation set to the min and max of the field
59%               =0 lower and upper bound imposed by .AMax and .MinA
60%    .Scalar.BW= 1 black and white representation imposed, =0 by default.
61%    .Scalar.Contours= 1: represent scalars by contour plots (Matlab function 'contour'); =0 by default
62%    .IncrA : contour interval
63%            -- vectors--
64%    .Vectors.VecScale: scale for the vector representation
65%    .Vectors.AutoVec: =0 (default) automatic length for vector representation, =1: length set by .VecScale
66%    .Vectors.HideFalse= 0 (default) false vectors represented in magenta, =1: false vectors not represented;
67%    .Vectors.HideWarning= 0 (default) vectors marked by warnflag~=0 marked in black, 1: no warning representation;
68%    .Vectors.decimate4 = 0 (default) all vectors reprtesented, =1: half of  the vectors represented along each coordinate
69%         -- vector color--
70%    .Vectors.ColorCode= 'black','white': imposed color  (default ='blue')
71%                        'rgb', : three colors red, blue, green depending
72%                        on thresholds .colcode1 and .colcode2 on the input  scalar value (C)
73%                        'brg': like rgb but reversed color order (blue, green, red)
74%                        '64 colors': continuous color from blue to red (multijet)
75%    .Vectors.colcode1 : first threshold for rgb, first value for'continuous'
76%    .Vectors.colcode2 : second threshold for rgb, last value (saturation) for 'continuous'
77%    .Vectors.FixedCbounds;  =0 (default): the bounds on C representation are min and max, =1: they are fixed by .Minc and .MaxC
78%    .Vectors.MinC = imposed minimum of the scalar field used for vector color;
79%    .Vectors.MaxC = imposed maximum of the scalar field used for vector color;
80%
81% KeepLim:=0 (default) adjust axes limit to the X,Y data, =1: preserves the previous axes limits
82% PosColorbar: if not empty, display a colorbar for B&W images
83%               imposed position of the colorbar (ex [0.821 0.471 0.019 0.445])
84%
85%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
86%  Copyright Joel Sommeria, 2008, LEGI / CNRS-UJF-INPG, sommeria@coriolis-legi.org.
87%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
88%     This file is part of the toolbox UVMAT.
89%
90%     UVMAT is free software; you can redistribute it and/or modify
91%     it under the terms of the GNU General Public License as published by
92%     the Free Software Foundation; either version 2 of the License, or
93%     (at your option) any later version.
94%
95%     UVMAT is distributed in the hope that it will be useful,
96%     but WITHOUT ANY WARRANTY; without even the implied warranty of
97%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
98%     GNU General Public License (file UVMAT/COPYING.txt) for more details.
99%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
100
101function [PlotType,PlotParamOut,haxes]= plot_field(Data,haxes,PlotParam,KeepLim,PosColorbar)
102
103%default output
104if ~exist('PlotParam','var'),PlotParam=[];end;
105if ~exist('KeepLim','var'),KeepLim=0;end;
106if ~exist('PosColorbar','var'),PosColorbar=[];end;
107PlotType='text'; %default
108PlotParamOut=PlotParam;%default
109
110% check input structure
111[Data,errormsg]=check_field_structure(Data);
112
113if ~isempty(errormsg)
114    msgbox_uvmat('ERROR',['input of plot_field/check_field_structure: ' errormsg])
115    display(['input of plot_field/check_field_structure:: ' errormsg])
116    return
117end
118
119testnewfig=1;%test to create a new figure (default)
120testzoomaxes=0;%test for the existence of a zoom secondary figure attached to the plotting axes
121if exist('haxes','var')
122    if ishandle(haxes)
123        if isequal(get(haxes,'Type'),'axes')
124            axes(haxes)
125            testnewfig=0;
126            AxeData=get(haxes,'UserData');
127            if isfield(AxeData,'ZoomAxes')&& ishandle(AxeData.ZoomAxes)
128                if isequal(get(AxeData.ZoomAxes,'Type'),'axes')
129                    testzoomaxes=1;
130                    zoomaxes=AxeData.ZoomAxes;
131                end
132            end
133        end
134    end
135end
136if testnewfig% create a new figure and axes if the plotting axes does not exist
137    hfig=figure;
138    if isfield(Data,'IndexObj')&isfield(Data,'Style')&isfield(Data,'ProjMode')
139        figname=[num2str(Data.IndexObj) '-' Data.Style];
140        set(hfig,'Name',figname)
141    end
142    testhandle=0;
143    if isfield(PlotParam,'text_display_1')& ishandle(PlotParam.text_display_1)
144        set(hfig,'UserData',PlotParam)
145        testhandle=1;
146    end
147    set(hfig,'Units','normalized')
148    set(hfig,'WindowButtonDownFcn','mouse_down')
149    %set(hfig,'WindowButtonMotionFcn',{'mouse_motion',PlotParam})%set mouse action function
150    set(hfig,'WindowButtonMotionFcn','mouse_motion')%set mouse action function
151    set(hfig,'WindowButtonUpFcn','mouse_up')%set mouse action function
152    haxes=axes;
153    set(haxes,'position',[0.13,0.2,0.775,0.73])
154else
155    hstack=findobj(allchild(0),'Type','figure');%current stack order of figures in matlab
156end
157if isfield(PlotParam,'text_display_1')& ishandle(PlotParam.text_display_1)
158    PlotParam=read_plot_param(PlotParam);   
159end
160if testnewfig
161  PlotParam.NextPlot='add'; %parameter for plot_profile and plot_hist
162end
163if isfield(PlotParam,'Auto_xy') && isequal(PlotParam.Auto_xy,1)
164    set(haxes,'DataAspectRatioMode','auto')%automatic aspect ratio
165end
166
167% check the cells of fields :
168testnbdim=1;
169[CellVarIndex,NbDim,VarType,errormsg]=find_field_indices(Data);
170
171if ~isempty(errormsg)
172    msgbox_uvmat('ERROR',['input of plot_field/find_field_indices: ' errormsg])
173    display(['input of plot_field: ' errormsg])
174    return
175end
176if ~isfield(Data,'NbDim') %& ~isfield(Data,'Style')%determine the space dimensionb if not defined: choose the kind of plot
177    [Data.NbDim,imax]=max(NbDim);
178end
179if isequal(Data.NbDim,0) % TODO: chech whether this function is still used, replace by plot_profile ?
180%     if isfield(Data,'Style') & isequal(Data.Style,'points')
181        AxeData=plot_text(Data,haxes);
182        PlotType='text';
183%     else
184%         [AxeData,haxes]=plot_hist(Data,haxes,PlotParam);
185%     end
186elseif isequal(Data.NbDim,1)
187    [AxeData,haxes]=plot_profile(Data,CellVarIndex,VarType,haxes,PlotParam);%
188    if testzoomaxes
189        [AxeData,zoomaxes,PlotParamOut]=plot_profile(Data,CellVarIndex,VarType,zoomaxes,PlotParam);
190        AxeData.ZoomAxes=zoomaxes;
191    end
192    PlotType='line';
193elseif isequal(Data.NbDim,2)
194    ind_select=find(NbDim>=2);
195    if numel(ind_select)>2
196        msgbox_uvmat('ERROR',['more than two fields to map'])
197        display(['more than two fields to map'])
198        return
199    end
200    [AxeData,haxes,PlotParamOut,PlotType]=plot_plane(Data,CellVarIndex(ind_select),VarType(ind_select),haxes,PlotParam,KeepLim,PosColorbar);
201    if testzoomaxes
202        [AxeData,zoomaxes,PlotParamOut]=plot_plane(Data,CellVarIndex(ind_select),VarType(ind_select),zoomaxes,PlotParam,1,PosColorbar);
203        AxeData.ZoomAxes=zoomaxes;
204    end
205elseif isequal(Data.NbDim,3)
206    msgbox_uvmat('ERROR','volume plot not implemented yet')
207    return
208else
209    testnbdim=0;
210end
211
212%display (or delete) error message
213htext=findobj(haxes,'Tag','hTxt');
214if isfield(Data,'Txt')
215    if isempty(htext)
216        Xlim=get(haxes,'XLim');
217        Ylim=get(haxes,'YLim');
218        htext=text(Xlim(1),(Ylim(1)+Ylim(2))/2,Data.Txt,'Tag','hTxt','Color','r');
219        set(htext,'Interpreter','none')
220    else
221        set(htext,'String',Data.Txt)
222    end
223elseif ~isempty(htext)
224    delete(htext)
225end
226
227% set graph aspect ratio
228
229
230set(haxes,'UserData',AxeData)
231if ~testnewfig
232set(0,'Children',hstack);%put back the initial figure stack after plot creation
233%set(haxes,'Tag','uvmat');
234end
235
236
237%----------------------------------------------------------
238function [AxeData,haxes]=plot_profile(data,CellVarIndex,VarType,haxes,PlotParam)
239%-----------------------------------------------------------
240%axes(haxes)
241hfig=get(haxes,'parent');
242AxeData=data;
243
244ColorOrder=[1 0 0;0 0.5 0;0 0 1;0 0.75 0.75;0.75 0 0.75;0.75 0.75 0;0.25 0.25 0.25];
245set(haxes,'ColorOrder',ColorOrder)
246if isfield(PlotParam,'NextPlot')
247    set(haxes,'NextPlot',PlotParam.NextPlot)
248end
249% adjust the size of the plot to include the whole field, except if KeepLim=1
250if isfield(PlotParam,'FixedLimits') && isequal(PlotParam.FixedLimits,1)  %adjust the graph limits*
251    set(haxes,'XLimMode', 'manual')
252    set(haxes,'YLimMode', 'manual')
253else
254    set(haxes,'XLimMode', 'auto')
255    set(haxes,'YLimMode', 'auto')
256end
257legend_str={};
258
259%initiates string of the plot command
260plotstr='hhh=plot(';
261textmean={};
262abscissa_name='';
263coord_x_index=[];
264test_newplot=1;
265for icell=1:length(CellVarIndex)
266    testfalse=0;
267    VarIndex=CellVarIndex{icell};%  indices of the selected variables in the list data.ListVarName
268    DimCell=data.VarDimName{VarIndex(1)};
269    if ischar(DimCell)
270        DimCell={DimCell};
271    end
272    XName=DimCell{1}; %first dimension considered as abscissa
273    if ~isempty(VarType{icell}.coord_x)
274        coord_x_index=VarType{icell}.coord_x;
275    else
276        coord_x_index_cell=VarType{icell}.coord(1);
277        if isequal(coord_x_index_cell,0)
278            continue  % the cell has no abscissa, skip it
279        end
280        if ~isempty(coord_x_index)&&~isequal(coord_x_index_cell,coord_x_index)
281            continue %all the selected variables must have the same first dimension
282        else
283            coord_x_index=coord_x_index_cell;
284        end
285    end
286    testplot=ones(size(data.ListVarName));%default test for plotted variables
287    testcoordvar=0;
288    charplot_0='''-''';%default
289    if isfield(data,'ObjectProjMode')& isequal(data.ObjectProjMode,'projection')
290        charplot_0='+';
291    end
292    xtitle=''; 
293   
294    xtitle=data.ListVarName{coord_x_index};
295    eval(['coord_x{icell}=data.' data.ListVarName{coord_x_index} ';']);%coordinate variable set as coord_x
296    if isfield(data,'VarAttribute')&& numel(data.VarAttribute)>=coord_x_index && isfield(data.VarAttribute{coord_x_index},'units')
297         xtitle=[xtitle '(' data.VarAttribute{coord_x_index}.units ')'];
298    end
299    eval(['coord_x{icell}=data.' data.ListVarName{coord_x_index} ';']);%coordinate variable set as coord_x
300    testcoordvar=1;
301    testplot(coord_x_index)=0;
302    if ~isempty(VarType{icell}.ancillary')
303            testplot(VarType{icell}.ancillary)=0;
304    end
305    if ~isempty(VarType{icell}.warnflag')
306            testplot(VarType{icell}.warnflag)=0;
307    end
308 
309    if isfield(data,'VarAttribute')
310        VarAttribute=data.VarAttribute;
311        for ivar=1:length(VarIndex)
312             if length(VarAttribute)>=VarIndex(ivar) & isfield(VarAttribute{VarIndex(ivar)},'long_name')
313                 plotname{VarIndex(ivar)}=VarAttribute{VarIndex(ivar)}.long_name;
314             else
315                 plotname{VarIndex(ivar)}=data.ListVarName{VarIndex(ivar)};%name for display in plot A METTRE
316             end
317        end
318    end
319    hh=findobj(haxes,'tag','plot_line');
320    if isequal(numel(hh),numel(find(testplot(VarIndex))))%update existing curves
321        icurve=0;
322        test_newplot=0;
323        if ~isempty(VarType{icell}.discrete')
324            charplot_0='+';
325            LineStyle='none';
326        else
327            charplot_0='none';
328            LineStyle='-';
329        end
330        for ivar=1:length(VarIndex)
331            if testplot(VarIndex(ivar))
332                icurve=icurve+1;
333                VarName=data.ListVarName{VarIndex(ivar)};
334                eval(['data.' VarName '=squeeze(data.' VarName ');'])
335                set(hh(icurve),'LineStyle',LineStyle)
336                set(hh(icurve),'Marker',charplot_0)
337%                 if isequal(VarName,'A')
338%                     set(hh(icurve),'LineStyle','-');
339%                 else
340%                     set(hh(icurve),'LineStyle',charplot_0);
341%                 end
342                set(hh(icurve),'XData',coord_x{icell})
343                eval(['yy=data.' VarName ';'])
344                set(hh(icurve),'YData',yy);
345%                 eval(['nbcomponent2=size(data.' VarName ',2);']);
346%                 eval(['nbcomponent1=size(data.' VarName ',1);']);
347%                 if numel(coord_x{icell})==2
348%                     coord_x{icell}=linspace(coord_x{icell}(1),coord_x{icell}(2),nbcomponent1);
349%                 end
350%                 eval(['varmean=mean(double(data.' VarName '));']);%mean value
351%                 textmean=[textmean; {[VarName 'mean= ' num2str(varmean,4)]}];
352%                 if nbcomponent1==1| nbcomponent2==1
353%                     legend_str=[legend_str {VarName}]; %variable with one component
354%                 else  %variable with severals  components
355%                     for ic=1:min(nbcomponent1,nbcomponent2)
356%                         legend_str=[legend_str [VarName '_' num2str(ic)]]; %variable with severals  components
357%                     end                                                   % labeled by their index (e.g. color component)
358%                 end
359            end
360        end
361    else% new plot
362        if ~isempty(VarType{icell}.discrete')
363            charplot_0='''+''';
364        else
365            charplot_0='''-''';
366        end
367        for ivar=1:length(VarIndex)
368            if testplot(VarIndex(ivar))
369                VarName=data.ListVarName{VarIndex(ivar)};
370                eval(['data.' VarName '=squeeze(data.' VarName ');'])
371%                 if isequal(VarName,'A')
372%                     charplot='''-''';
373%                 else
374%                     charplot=charplot_0;
375%                 end
376                plotstr=[plotstr 'coord_x{' num2str(icell) '},data.' VarName ',' charplot_0 ','];
377                eval(['nbcomponent2=size(data.' VarName ',2);']);
378                eval(['nbcomponent1=size(data.' VarName ',1);']);
379                if numel(coord_x{icell})==2
380                    coord_x{icell}=linspace(coord_x{icell}(1),coord_x{icell}(2),nbcomponent1);
381                end
382                eval(['varmean=mean(double(data.' VarName '));']);%mean value
383                textmean=[textmean; {[VarName 'mean= ' num2str(varmean,4)]}];
384                if nbcomponent1==1| nbcomponent2==1
385                    legend_str=[legend_str {VarName}]; %variable with one component
386                else  %variable with severals  components
387                    for ic=1:min(nbcomponent1,nbcomponent2)
388                        legend_str=[legend_str [VarName '_' num2str(ic)]]; %variable with severals  components
389                    end                                                   % labeled by their index (e.g. color component)
390                end
391            end
392        end
393    end
394end
395if test_newplot && ~isequal(plotstr,'plot(')
396    plotstr=[plotstr '''tag'',''plot_line'')']
397                %execute plot (instruction  plotstr) 
398    eval(plotstr)
399   
400                %%%%%
401    grid(haxes, 'on')
402    hxlabel=xlabel(xtitle);
403    set(hxlabel,'Interpreter','none')% desable tex interpreter
404    if length(legend_str)>=1
405        hylabel=ylabel(legend_str{end});
406        set(hylabel,'Interpreter','none')% desable tex interpreter
407    end
408    if ~isempty(legend_str)
409        hlegend=findobj(hfig,'Tag','legend');
410        if isempty(hlegend)
411            hlegend=legend(legend_str);
412            txt=ver;
413            Release=txt(1).Release;
414            relnumb=str2num(Release(3:4));
415            if relnumb >= 14
416                set(hlegend,'Interpreter','none')% desable tex interpreter
417            end
418        else
419            legend_old=get(hlegend,'String');
420            if isequal(size(legend_old,1),size(legend_str,1))&~isequal(legend_old,legend_str)
421                set(hlegend,'String',[legend_old legend_str]);
422            end
423        end
424    end
425    title_str='';
426    if isfield(data,'filename')
427       [Path, title_str, ext]=fileparts(data.filename);
428       title_str=[title_str ext];
429    end
430    if isfield(data,'Action')
431        if ~isequal(title_str,'')
432            title_str=[title_str ', '];
433        end
434        title_str=[title_str data.Action];
435    end
436    htitle=title(title_str);
437    txt=ver;
438    Release=txt(1).Release;
439    relnumb=str2num(Release(3:4));
440    if relnumb >= 14
441        set(htitle,'Interpreter','none')% desable tex interpreter
442    end
443    % A REPRENDRE Mean
444%         hlist=findobj(gcf,'Style','listbox','Tag','liststat');
445%         if isempty(hlist)
446%             'text'
447%             textmean
448%             set(gca,'position',[0.13,0.2,0.775,0.73])
449%             uicontrol('Style','popupmenu','Position',[20 20 200 20],'String',textmean,'Tag','liststat');
450%         else
451%             set(hlist(1),'String',textmean)
452%         end
453end
454
455
456%---------------------------------------
457% plot_plane
458%----------------------------------------
459function [AxeData,haxes,PlotParamOut,PlotType]=plot_plane(Data,CellVarIndex,VarTypeCell,haxes,PlotParam,KeepLim,PosColorbar)
460grid(haxes, 'off')
461%default plotting parameters
462PlotType='plane';%default
463if ~exist('PlotParam','var')
464    PlotParam=[];
465end
466if ~isfield(PlotParam,'Scalar')
467    PlotParam.Scalar=[];
468end
469if ~isfield(PlotParam,'Vectors')
470    PlotParam.Vectors=[];
471end
472PlotParamOut=PlotParam;%default
473
474%plotting axes
475hfig=get(haxes,'parent');
476hcol=findobj(hfig,'Tag','Colorbar'); %look for colorbar axes
477hima=findobj(haxes,'Tag','ima');% search existing image in the current axes
478AxeData=get(haxes,'UserData'); %default
479if ~isstruct(AxeData)% AxeData must be a structure
480    AxeData=[];
481end
482AxeData.NbDim=2;
483if isfield(Data,'ObjectCoord')
484    AxeData.ObjectCoord=Data.ObjectCoord;
485end
486
487test_ima=0; %default: test for image or map plot
488test_vec=0; %default: test for vector plots
489test_black=0;
490test_false=0;
491test_C=0;
492XName='';
493x_units='';
494YName='';
495y_units='';
496for icell=1:length(CellVarIndex) % length(CellVarIndex) =1 or 2 (from the calling function)
497%     VarIndex=CellVarIndex{icell};
498    VarType=VarTypeCell{icell};
499    ivar_X=VarType.coord_x; % defines (unique) index for the variable representing unstructured x coordinate (default =[])
500    ivar_Y=VarType.coord_y; % defines (unique)index for the variable representing unstructured y coordinate (default =[])
501    ivar_U=VarType.vector_x; % defines (unique) index for the variable representing x vector component (default =[])
502    ivar_V=VarType.vector_y; % defines (unique) index for the variable representing y vector component (default =[])
503    ivar_C=[VarType.scalar VarType.image VarType.color VarType.ancillary]; %defines index (indices) for the scalar or ancillary fields
504    if numel(ivar_C)>1
505        msgbox_uvmat('ERROR','error in plot_field: too many scalar inputs')
506        return
507    end
508    ivar_F=VarType.warnflag; %defines index (unique) for warning flag variable
509    ivar_FF=VarType.errorflag; %defines index (unique) for error flag variable
510    ind_coord=find(VarType.coord);
511    if numel(ind_coord)==2
512        VarType.coord=VarType.coord(ind_coord);
513    end
514    idim_Y=[]; 
515    test_grid=0;
516    if ~isempty(ivar_U) && ~isempty(ivar_V)% vector components detected
517        if test_vec
518            msgbox_uvmat('ERROR','error in plot_field: attempt to plot two vector fields')
519            return
520        else
521            test_vec=1;
522            eval(['vec_U=Data.' Data.ListVarName{ivar_U} ';'])
523            eval(['vec_V=Data.' Data.ListVarName{ivar_V} ';'])
524            if ~isempty(ivar_X) && ~isempty(ivar_Y)% 2D field (with unstructured coordinates or structured ones (then ivar_X and ivar_Y empty)     
525                eval(['vec_X=Data.' Data.ListVarName{ivar_X} ';'])
526                eval(['vec_Y=Data.' Data.ListVarName{ivar_Y} ';'])
527            elseif numel(VarType.coord)==2 & VarType.coord~=[0 0];%coordinates defines by dimension variables
528                eval(['y=Data.' Data.ListVarName{VarType.coord(1)} ';'])
529                eval(['x=Data.' Data.ListVarName{VarType.coord(2)} ';'])
530                if numel(y)==2 % y defined by first and last values on aregular mesh
531                    y=linspace(y(1),y(2),size(vec_U,1));
532                end
533                if numel(x)==2 % y defined by first and last values on aregular mesh
534                    x=linspace(x(1),x(2),size(vec_U,2));
535                end
536                [vec_X,vec_Y]=meshgrid(x,y); 
537            else
538                msgbox_uvmat('ERROR','error in plot_field: invalid coordinate definition for vector field')
539                return
540            end
541            if ~isempty(ivar_C)
542                 eval(['vec_C=Data.' Data.ListVarName{ivar_C} ';']) ;
543                 vec_C=reshape(vec_C,1,numel(vec_C));
544                 test_C=1;
545            end
546            if ~isempty(ivar_F)%~(isfield(PlotParam.Vectors,'HideWarning')&& isequal(PlotParam.Vectors.HideWarning,1))
547                if test_vec
548                    eval(['vec_F=Data.' Data.ListVarName{ivar_F} ';']) % warning flags for  dubious vectors
549                    if  ~(isfield(PlotParam.Vectors,'HideWarning') && isequal(PlotParam.Vectors.HideWarning,1))
550                        test_black=1;
551                    end
552                end
553            end
554            if ~isempty(ivar_FF) %&& ~test_false
555                if test_vec% TODO: deal with FF for structured coordinates
556                    eval(['vec_FF=Data.' Data.ListVarName{ivar_FF} ';']) % flags for false vectors
557                end
558            end
559        end
560    elseif ~isempty(ivar_C) %scalar or image
561        if test_ima
562             msgbox_uvmat('ERROR','attempt to plot two scalar fields or images')
563            return
564        end
565        eval(['A=squeeze(Data.' Data.ListVarName{ivar_C} ');']) ;% scalar represented as color image
566        test_ima=1;
567        if ~isempty(ivar_X) && ~isempty(ivar_Y)% 2D field (with unstructured coordinates or structured ones (then ivar_X and ivar_Y empty)
568            XName=Data.ListVarName{ivar_X};
569            YName=Data.ListVarName{ivar_Y};
570            eval(['AX=Data.' XName ';'])
571            eval(['AY=Data.' YName ';'])
572            [A,AX,AY]=proj_grid(AX',AY',A',[],[],'np>256');  % interpolate on a grid 
573            if isfield(Data,'VarAttribute')
574                if numel(Data.VarAttribute)>=ivar_X & isfield(Data.VarAttribute{ivar_X},'units')
575                    x_units=['(' Data.VarAttribute{ivar_X}.units ')'];
576                end
577                if numel(Data.VarAttribute)>=ivar_Y & isfield(Data.VarAttribute{ivar_Y},'units')
578                    y_units=['(' Data.VarAttribute{ivar_Y}.units ')'];
579                end
580            end       
581        elseif numel(VarType.coord)==2 %structured coordinates
582            XName=Data.ListVarName{VarType.coord(2)};
583            YName=Data.ListVarName{VarType.coord(1)};
584            eval(['AY=Data.' Data.ListVarName{VarType.coord(1)} ';'])
585            eval(['AX=Data.' Data.ListVarName{VarType.coord(2)} ';'])
586            test_interp_X=0; %default, regularly meshed X coordinate
587            test_interp_Y=0; %default, regularly meshed Y coordinate
588            if isfield(Data,'VarAttribute')
589                if numel(Data.VarAttribute)>=VarType.coord(2) & isfield(Data.VarAttribute{VarType.coord(2)},'units')
590                    x_units=['(' Data.VarAttribute{VarType.coord(2)}.units ')'];
591                end
592                if numel(Data.VarAttribute)>=VarType.coord(1) & isfield(Data.VarAttribute{VarType.coord(1)},'units')
593                    y_units=['(' Data.VarAttribute{VarType.coord(1)}.units ')'];
594                end
595            end 
596            if numel(AY)>2
597                DAY=diff(AY);
598                DAY_min=min(DAY);
599                DAY_max=max(DAY);
600                if sign(DAY_min)~=sign(DAY_max);% =1 for increasing values, 0 otherwise
601                     errormsg=['errror in plot_field.m: non monotonic dimension variable # ' ListVarName{VarType.coord(1)} ];
602                      return
603                end
604                test_interp_Y=(DAY_max-DAY_min)> 0.0001*abs(DAY_max);
605            end
606            if numel(AX)>2
607                DAX=diff(AX);
608                DAX_min=min(DAX);
609                DAX_max=max(DAX);
610                if sign(DAX_min)~=sign(DAX_max);% =1 for increasing values, 0 otherwise
611                     errormsg=['errror in plot_field.m: non monotonic dimension variable # ' ListVarName{VarType.coord(2)} ];
612                      return
613                end
614                test_interp_X=(DAX_max-DAX_min)> 0.0001*abs(DAX_max);
615            end 
616            if test_interp_Y         
617                npxy(1)=max([256 floor((AY(end)-AY(1))/DAY_min) floor((AY(end)-AY(1))/DAY_max)]);
618                yI=linspace(AY(1),AY(end),npxy(1));
619                if ~test_interp_X
620                    xI=linspace(AX(1),AX(end),size(A,2));%default
621                    AX=xI;
622                end
623            end
624            if test_interp_X 
625                npxy(1)=max([256 floor((AX(end)-AX(1))/DAX_min) floor((AX(end)-AX(1))/DAX_max)]);
626                xI=linspace(AX(1),AX(end),npxy(2));   
627                if ~test_interp_Y
628                   yI=linspace(AY(1),AY(end),size(A,1));
629                   AY=yI;
630                end
631            end
632            if test_interp_X || test_interp_Y               
633                [AX2D,AY2D]=meshgrid(AX,AY);
634                A=interp2(AX2D,AY2D,double(A),xI,yI');
635            end
636            AX=[AX(1) AX(end)];% keep only the lower and upper bounds for image represnetation
637            AY=[AY(1) AY(end)];
638        else
639            msgbox_uvmat('ERROR','error in plot_field: invalid coordinate definition ')
640            return
641        end
642          x_label=[Data.ListVarName{ivar_X} '(' x_units ')'];
643    end       
644%     if isfield(Data,'VarAttribute')
645%         VarAttribute=Data.VarAttribute;
646%     end   
647end
648
649%%%%%%%%%%%%%%%%%%%%%   image or scalar plot %%%%%%%%%%%%%%%%%%%%%%%%%%
650
651if ~isfield(PlotParam.Scalar,'Contours')
652    PlotParam.Scalar.Contours=0; %default
653end
654PlotParamOut=PlotParam; %default
655if test_ima
656    % distinguish B/W and color images
657    np=size(A);%size of image
658    siz=size(np);
659    %set the color map
660    if siz(2)==2 %for black and white images
661        if ~isfield(PlotParam.Scalar,'AutoScal')
662            PlotParam.Scalar.AutoScal=0;%default
663        end
664        if ~isfield(PlotParam.Scalar,'MinA')
665            PlotParam.Scalar.MinA=[];%default
666        end
667        if ~isfield(PlotParam.Scalar,'MaxA')
668            PlotParam.Scalar.MaxA=[];%default
669        end
670        if isequal(PlotParam.Scalar.AutoScal,0)|isempty(PlotParam.Scalar.MinA)|~isa(PlotParam.Scalar.MinA,'double')  %correct if there is no numerical data in edit box
671            MinA=double(min(min(A)));
672        else
673            MinA=PlotParam.Scalar.MinA; 
674        end;
675        if isequal(PlotParam.Scalar.AutoScal,0)|isempty(PlotParam.Scalar.MaxA)|~isa(PlotParam.Scalar.MaxA,'double') %correct if there is no numerical data in edit box
676            MaxA=double(max(max(A)));
677        else
678            MaxA=PlotParam.Scalar.MaxA; 
679        end;
680        PlotParamOut.Scalar.MinA=MinA;
681        PlotParamOut.Scalar.MaxA=MaxA;
682        axes(haxes)
683        if isequal(PlotParam.Scalar.Contours,1)
684            if ~isempty(hima) & ishandle(hima)
685                delete(hima)
686            end
687            if ~isfield(PlotParam.Scalar,'IncrA')
688                PlotParam.Scalar.IncrA=[];
689            end
690            if isempty(PlotParam.Scalar.IncrA)% | PlotParam.Scalar.AutoScal==0
691                cont=colbartick(MinA,MaxA);
692                intercont=cont(2)-cont(1);%default
693                PlotParamOut.Scalar.IncrA=intercont;
694            else
695               intercont=PlotParam.Scalar.IncrA;
696            end
697            B=A;           
698            abscontmin=intercont*floor(MinA/intercont);
699            abscontmax=intercont*ceil(MaxA/intercont);
700            contmin=intercont*floor(min(min(B))/intercont);
701            contmax=intercont*ceil(max(max(B))/intercont);
702            cont_pos_plus=[0:intercont:contmax];
703            cont_pos_min=[double(contmin):intercont:-intercont];
704            cont_pos=[cont_pos_min cont_pos_plus];
705            sizpx=(AX(end)-AX(1))/(np(2)-1);
706            sizpy=(AY(1)-AY(end))/(np(1)-1);
707            x_cont=[AX(1):sizpx:AX(end)]; % pixel x coordinates for image display
708            y_cont=[AY(1):-sizpy:AY(end)]; % pixel x coordinates for image display
709            txt=ver;%version of Matlab
710            Release=txt(1).Release;
711            relnumb=str2num(Release(3:4));
712            if relnumb >= 14
713                    vec=linspace(0,1,(abscontmax-abscontmin)/intercont);%define a greyscale colormap with steps intercont
714                map=[vec' vec' vec'];
715                colormap(map);
716                [var,hcontour]=contour(x_cont,y_cont,B,cont_pos);       
717                set(hcontour,'Fill','on')
718                set(hcontour,'LineStyle','none')
719                hold on
720            end
721            [var_p,hcontour_p]=contour(x_cont,y_cont,B,cont_pos_plus,'k-');
722            hold on
723            [var_m,hcontour_m]=contour(x_cont,y_cont,B,cont_pos_min,':');
724            set(hcontour_m,'LineColor',[1 1 1])
725            hold off
726            caxis([abscontmin abscontmax])
727            colormap(map);
728        end
729        if ~isequal(PlotParam.Scalar.Contours,1)       
730            % rescale the grey levels with min and max, put a grey scale colorbar
731            if (isfield(PlotParam.Scalar,'BW')& (isequal(PlotParam.Scalar.BW,1))|isa(A,'uint8')| isa(A,'uint16'))%images
732                B=A;
733                vec=linspace(0,1,255);%define a linear greyscale colormap
734                map=[vec' vec' vec'];
735                colormap(map);  %grey scale color map
736            else
737                B=A;
738                colormap('default'); % standard faulse colors for div, vort , scalar fields
739            end
740        end
741    elseif siz(2)==3 %color images
742        axes(haxes)
743        B=uint8(A);
744        MinA=0;
745        MaxA=255;
746    end
747    if ~isequal(PlotParam.Scalar.Contours,1)
748        %interpolate to increase resolution
749        test_interp=1;
750        if max(np) <= 64
751            npxy=8*np;% increase the resolution 8 times
752        elseif max(np) <= 128
753            npxy=4*np;% increase the resolution 4 times
754        elseif max(np) <= 256
755            npxy=2*np;% increase the resolution 2 times
756        else
757            npxy=np;
758            test_interp=0; % no interpolation done
759        end
760        if test_interp==1%if we interpolate   
761            x=linspace(AX(1),AX(2),np(2));
762            y=linspace(AY(1),AY(2),np(1));
763            [X,Y]=meshgrid(x,y);
764            xi=linspace(AX(1),AX(2),npxy(2));
765            yi=linspace(AY(1),AY(2),npxy(1));
766            B = interp2(X,Y,double(B),xi,yi');
767        end       
768        if isempty(hima)
769            tag=get(haxes,'Tag');
770            hima=imagesc(AX,AY,B,[MinA MaxA]);
771            set(hima,'Tag','ima','HitTest','off')
772            set(haxes,'Tag',tag);%preserve the axes tag (removed by image fct !!!)       
773        else
774            set(hima,'CData',B);
775            if MinA<MaxA
776                caxis([MinA MaxA])
777            else
778                caxis([MinA MinA+1])
779            end
780            set(hima,'XData',AX);
781            set(hima,'YData',AY);
782        end
783    end
784    if ~isstruct(AxeData)
785        AxeData=[];
786    end
787    AxeData.A=A;
788    AxeData.AX=[AX(1) AX(end)];
789    AxeData.AY=[AY(1) AY(end)];
790    test_ima=1;
791    %display the colorbar code for B/W images if Poscolorbar not empty
792    if siz(2)==2 & exist('PosColorbar','var')& ~isempty(PosColorbar)
793        if isempty(hcol)|~ishandle(hcol)
794             hcol=colorbar;%create new colorbar
795        end
796        if length(PosColorbar)==4
797                 set(hcol,'Position',PosColorbar)           
798        end
799        YTick=0;%default
800        if MaxA>MinA
801            if isequal(PlotParam.Scalar.Contours,1)
802                colbarlim=get(hcol,'YLim');
803                scale_bar=(colbarlim(2)-colbarlim(1))/(abscontmax-abscontmin);               
804                YTick=cont_pos(2:end-1);
805                YTick_scaled=colbarlim(1)+scale_bar*(YTick-abscontmin);
806                set(hcol,'YTick',YTick_scaled);
807            elseif (isfield(PlotParam.Scalar,'BW') & isequal(PlotParam.Scalar.BW,1))|isa(A,'uint8')| isa(A,'uint16')%images
808                hi=get(hcol,'children');
809                if iscell(hi)%multiple images in colorbar
810                    hi=hi{1};
811                end
812                set(hi,'YData',[MinA MaxA])
813                set(hi,'CData',[1:256]')
814                set(hcol,'YLim',[MinA MaxA])
815                YTick=colbartick(MinA,MaxA);
816                set(hcol,'YTick',YTick)               
817            else
818                hi=get(hcol,'children');
819                if iscell(hi)%multiple images in colorbar
820                    hi=hi{1};
821                end
822                set(hi,'YData',[MinA MaxA])
823                set(hi,'CData',[1:64]')
824                YTick=colbartick(MinA,MaxA);
825                set(hcol,'YLim',[MinA MaxA])
826                set(hcol,'YTick',YTick)
827            end
828            set(hcol,'Yticklabel',num2str(YTick'));
829        end
830    elseif ishandle(hcol)
831        delete(hcol); %erase existing colorbar if not needed
832    end
833else%no scalar plot
834    if ~isempty(hima) && ishandle(hima)
835        delete(hima)
836    end
837    if ~isempty(hcol)&& ishandle(hcol)
838       delete(hcol)
839    end
840    AxeData.A=[];
841    AxeData.AX=[];
842    AxeData.AY=[];
843    PlotParamOut=rmfield(PlotParamOut,'Scalar');
844end
845
846%%%%%%%%%%%%%%%%%%%%%   vector plot %%%%%%%%%%%%%%%%%%%%%%%%%%
847if test_vec
848   %vector scale representation
849    if size(vec_U,1)==numel(vec_Y) && size(vec_U,2)==numel(vec_X); % x, y  coordinate variables
850        [vec_X,vec_Y]=meshgrid(vec_X,vec_Y);
851    end   
852    vec_X=reshape(vec_X,1,numel(vec_X));%reshape in matlab vectors
853    vec_Y=reshape(vec_Y,1,numel(vec_Y));
854    vec_U=reshape(vec_U,1,numel(vec_U));
855    vec_V=reshape(vec_V,1,numel(vec_V));
856    MinMaxX=max(vec_X)-min(vec_X);
857    MinMaxY=max(vec_Y)-min(vec_Y);
858    AxeData.Mesh=sqrt((MinMaxX*MinMaxY)/length(vec_X));
859    if  ~isfield(PlotParam.Vectors,'AutoVec') || isequal(PlotParam.Vectors.AutoVec,0)|| ~isfield(PlotParam.Vectors,'VecScale')...
860               ||isempty(PlotParam.Vectors.VecScale)||~isa(PlotParam.Vectors.VecScale,'double') %automatic vector scale
861        scale=[];
862        if test_false %remove false vectors
863            indsel=find(AxeData.FF==0);%indsel =indices of good vectors
864        else     
865            indsel=[1:numel(vec_X)];%
866        end
867        if isempty(vec_U)
868            scale=1;
869        else
870            if isempty(indsel)
871                MaxU=max(abs(vec_U));
872                MaxV=max(abs(vec_V));
873            else
874                MaxU=max(abs(vec_U(indsel)));
875                MaxV=max(abs(vec_V(indsel)));
876            end
877            scale=MinMaxX/(max(MaxU,MaxV)*50);
878            PlotParam.Vectors.VecScale=scale;%update the 'scale' display
879        end
880    else
881        scale=PlotParam.Vectors.VecScale;  %impose the length of vector representation
882    end;
883   
884    %record vectors on the plotting axes
885    if test_C==0
886        vec_C=ones(1,numel(vec_X));
887    end
888    AxeData.X=vec_X';
889    AxeData.Y=vec_Y';
890    AxeData.U=vec_U';
891    AxeData.V=vec_V';
892    AxeData.C=vec_C';
893    if isempty(ivar_F)
894        AxeData.F=[];
895    else
896        AxeData.F=vec_F';
897    end
898    if isempty(ivar_FF)
899        AxeData.FF=[];
900    else
901        AxeData.FF=vec_FF';
902    end
903%     if isfield(Data,'W')
904%         AxeData.W=Data.W;
905%     end
906   
907    %decimate by a factor 2 in vector mesh(4 in nbre of vectors)
908    if isfield(PlotParam.Vectors,'decimate4')&isequal(PlotParam.Vectors.decimate4,1)
909        diffy=diff(vec_Y); %difference dy=vec_Y(i+1)-vec_Y(i)
910        dy_thresh=max(abs(diffy))/2;
911        ind_jump=find(abs(diffy) > dy_thresh); %indices with diff(vec_Y)> max/2, detect change of line
912        ind_sel=[1:ind_jump(1)];%select the first line
913        for i=2:2:length(ind_jump)-1
914            ind_sel=[ind_sel [ind_jump(i)+1:ind_jump(i+1)]];% select the odd lines
915        end
916        nb_sel=length(ind_sel);
917        ind_sel=ind_sel([1:2:nb_sel]);% take half the points on a line
918        vec_X=vec_X(ind_sel);
919        vec_Y=vec_Y(ind_sel);
920        vec_U=vec_U(ind_sel);
921        vec_V=vec_V(ind_sel);
922        vec_C=vec_C(ind_sel);
923        if ~isempty(ivar_F)
924           vec_F=vec_F(ind_sel);
925        end
926        if ~isempty(ivar_FF)
927           vec_FF=vec_FF(ind_sel);
928        end
929    end
930   
931    %get main level color code
932    [colorlist,col_vec,PlotParamOut.Vectors]=set_col_vec(PlotParam.Vectors,vec_C);
933    % take flags into account: add flag colors to the list of colors
934    sizlist=size(colorlist);
935    nbcolor=sizlist(1);
936    if test_black
937       nbcolor=nbcolor+1;
938       colorlist(nbcolor,:)=[0 0 0]; %add black to the list of colors
939       if ~isempty(ivar_FF)
940            ind_flag=find(vec_F~=1 & vec_FF==0);  %flag warning but not false
941       else
942            ind_flag=find(vec_F~=1);
943       end
944       col_vec(ind_flag)=nbcolor;   
945    end
946    nbcolor=nbcolor+1;
947    if ~isempty(ivar_FF)
948        ind_flag=find(vec_FF~=0);
949        if isfield(PlotParam.Vectors,'HideFalse') && PlotParam.Vectors.HideFalse==1
950            colorlist(nbcolor,:)=[NaN NaN NaN];% no plot of false vectors
951        else
952            colorlist(nbcolor,:)=[1 0 1];% magenta color
953        end
954        col_vec(ind_flag)=nbcolor;
955    end
956    %plot vectors:
957    quiresetn(haxes,vec_X,vec_Y,vec_U,vec_V,scale,colorlist,col_vec);   
958else
959    hvec=findobj(haxes,'Tag','vel');
960    if ~isempty(hvec)
961        delete(hvec);
962    end
963    AxeData.X=[];
964    AxeData.Y=[];
965    AxeData.U=[];
966    AxeData.V=[];
967    AxeData.C=[];
968    AxeData.W=[];
969    AxeData.F=[];
970     AxeData.FF=[];
971    AxeData.Mesh=[];
972    PlotParamOut=rmfield(PlotParamOut,'Vectors');
973end
974if isfield(Data,'Z')
975    AxeData.Z=Data.Z;% A REVOIR
976end
977listfields={'AY','AX','A','X','Y','U','V','C','W','F','FF'};
978listdim={'AY','AX',{'AY','AX'},'nb_vectors','nb_vectors','nb_vectors','nb_vectors','nb_vectors','nb_vectors','nb_vectors','nb_vectors'};
979Role={'coord_y','coord_x','scalar','coord_x','coord_y','vector_x','vector_y','scalar','vector_z','warnflag','errorflag'};
980ind_select=[];
981nbvar=0;
982AxeData.ListVarName={};
983AxeData.VarDimName={};
984AxeData.VarAttribute={};
985for ilist=1:numel(listfields)
986    eval(['testvar=isfield(AxeData,listfields{ilist}) && ~isempty(AxeData.' listfields{ilist} ');'])
987    if testvar
988        nbvar=nbvar+1;
989        AxeData.ListVarName{nbvar}=listfields{ilist};
990        AxeData.VarDimName{nbvar}=listdim{ilist};
991        AxeData.VarAttribute{nbvar}.Role=Role{ilist};
992    end
993end
994
995% adjust the size of the plot to include the whole field, except if KeepLim=1
996if ~(exist('KeepLim','var') && isequal(KeepLim,1))  %adjust the graph limits*
997        test_lim=0;
998        if test_vec
999            Xlim=[min(vec_X) max(vec_X)];
1000            Ylim=[min(vec_Y) max(vec_Y)];
1001            test_lim=1;
1002            if test_ima%both background image and vectors coexist, take the wider bound
1003                Xlim(1)=min(AX(1),Xlim(1));
1004                Xlim(2)=max(AX(end),Xlim(2));
1005                Ylim(1)=min(AY(end),Ylim(1));
1006                Ylim(2)=max(AY(1),Ylim(2));
1007            end
1008        elseif test_ima %only image plot
1009            Xlim(1)=min(AX(1),AX(end));
1010            Xlim(2)=max(AX(1),AX(end));
1011            Ylim(1)=min(AY(1),AY(end));
1012            Ylim(2)=max(AY(1),AY(end));
1013            test_lim=1;
1014        end
1015        if test_lim
1016            if Xlim(2)>Xlim(1)
1017                set(haxes,'XLim',Xlim);% set x limits of frame in axes coordinates
1018            end
1019            if Ylim(2)>Ylim(1)
1020                set(haxes,'YLim',Ylim);% set y limits of frame in axes coordinate
1021            end
1022        end
1023end
1024if ~(isfield(PlotParam,'Auto_xy') && isequal(PlotParam.Auto_xy,1))
1025     set(haxes,'DataAspectRatio',[1 1 1])
1026end
1027set(haxes,'YDir','normal')
1028set(get(haxes,'XLabel'),'String',[XName x_units]);
1029set(get(haxes,'YLabel'),'String',[YName y_units]);
1030%---------------------------------------------
1031%function for plotting vectors
1032%------------------------------------------------
1033%INPUT:
1034% haxes: handles of the plotting axes
1035%x,y,u,v: vectors coordinates and vector components to plot, arrays withb the same dimension
1036% scale: scaling factor for vector length representation
1037%colorlist(icolor,:): list of vector colors, dim (nbcolor,3), depending on color #i
1038%col_vec: matlab vector setting the color number #i for each velocity vector
1039function quiresetn(haxes,x,y,u,v,scale,colorlist,col_vec)
1040
1041%define arrows
1042theta=0.5 ;%angle arrow
1043alpha=0.3 ;%length arrow
1044rot=alpha*[cos(theta) -sin(theta); sin(theta) cos(theta)]';
1045%find the existing lines
1046%h=findobj(gca,'Type','Line');% search existing lines in the current axes
1047h=findobj(haxes,'Tag','vel');% search existing lines in the current axes
1048sizh=size(h);
1049set(h,'EraseMode','xor');
1050set(haxes,'NextPlot','replacechildren');
1051%htext=findobj('Tag','scalevec');
1052     
1053%drawnow
1054%create lines (if no lines) or modify them
1055if ~isequal(size(col_vec),size(x))
1056    col_vec=ones(size(x));% case of error in col_vec input
1057end
1058sizlist=size(colorlist);
1059ncolor=sizlist(1);
1060
1061for icolor=1:ncolor
1062    %determine the line positions for each color icolor
1063    ind=find(col_vec==icolor);
1064    xc=x(ind);
1065    yc=y(ind);
1066    uc=u(ind)*scale;
1067    vc=v(ind)*scale;
1068    n=size(xc);
1069    xN=NaN*ones(size(xc));
1070    matx=[xc(:) xc(:)+uc(:) xN(:)]';
1071    matx=reshape(matx,1,3*n(2));
1072    maty=[yc(:) yc(:)+vc(:) xN(:)]';
1073    maty=reshape(maty,1,3*n(2));
1074   
1075    %determine arrow heads
1076    arrowplus=rot*[uc;vc];
1077    arrowmoins=rot'*[uc;vc];
1078    x1=xc+uc-arrowplus(1,:);
1079    x2=xc+uc;
1080    x3=xc+uc-arrowmoins(1,:);
1081    y1=yc+vc-arrowplus(2,:);
1082    y2=yc+vc;
1083    y3=yc+vc-arrowmoins(2,:);
1084    matxar=[x1(:) x2(:) x3(:) xN(:)]';
1085    matxar=reshape(matxar,1,4*n(2));
1086    matyar=[y1(:) y2(:) y3(:) xN(:)]';
1087    matyar=reshape(matyar,1,4*n(2));
1088    %draw the line or modify the existing ones
1089    isn=isnan(colorlist(icolor,:));%test if color NaN
1090    if 2*icolor > sizh(1) %if icolor exceeds the number of existing ones
1091        axes(haxes)
1092        if ~isn(1) %if the vectors are visible color not nan
1093            if n(2)>0
1094                hold on
1095                line(matx,maty,'Color',colorlist(icolor,:),'Tag','vel');% plot new lines
1096                line(matxar,matyar,'Color',colorlist(icolor,:),'Tag','vel');% plot arrows
1097            end
1098        end
1099    else
1100        if isn(1)
1101            delete(h(2*icolor-1))
1102            delete(h(2*icolor))
1103        else
1104            set(h(2*icolor-1),'Xdata',matx,'Ydata',maty);
1105            set(h(2*icolor-1),'Color',colorlist(icolor,:));
1106            set(h(2*icolor-1),'EraseMode','xor');
1107            set(h(2*icolor),'Xdata',matxar,'Ydata',matyar);
1108            set(h(2*icolor),'Color',colorlist(icolor,:));
1109            set(h(2*icolor),'EraseMode','xor');
1110        end
1111     end
1112end
1113if sizh(1) > 2*ncolor
1114    for icolor=ncolor+1 : sizh(1)/2%delete additional objects
1115        delete(h(2*icolor-1))
1116        delete(h(2*icolor))
1117    end
1118end
1119
1120%---------------------------------------
1121%determine tick positions for colorbar
1122%------------------------------------
1123function YTick=colbartick(MinA,MaxA)
1124%determine tick positions with "simple" values between MinA and MaxA
1125YTick=0;%default
1126maxabs=max([abs(MinA) abs(MaxA)]);
1127if maxabs>0
1128ord=10^(floor(log10(maxabs)));%order of magnitude
1129div=1;
1130siz2=1;
1131while siz2<2
1132%     values=[-9:div:9];
1133    values=-10:div:10;
1134    ind=find((ord*values-MaxA)<0 & (ord*values-MinA)>0);%indices of 'values' such that MinA<ord*values<MaxA
1135    siz=size(ind);
1136    if siz(2)<4%if there are less than 4 selected values (4 levels)
1137        values=[-9:0.5*div:9];
1138        ind=find((ord*values-MaxA)<0 & (ord*values-MinA)>0);
1139    end
1140    siz2=size(ind,2);
1141%     siz2=siz(2)
1142    div=div/10;
1143end
1144YTick=ord*values(ind);
1145end
Note: See TracBrowser for help on using the repository browser.