source: trunk/src/plot_field.m @ 193

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

improves the use of get_field, no projection object by default in uvmat( allows usual 1D plots),

various bug repairs, elimination of get_plot_handles.m (not useful)

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