source: trunk/src/plot_field.m @ 1022

Last change on this file since 1022 was 1001, checked in by sommeria, 7 years ago

tubcorrelation added, compilation improved to accept tranform fct

File size: 61.1 KB
RevLine 
[201]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:
[292]6%     2D vector fields are represented by arrows, 2D scalar fields by grey scale images or contour plots, 1D fields are represented by usual plot with (abscissa, ordinate).
[201]7%  The input field structure is first tested by check_field_structure.m,
[512]8%  then split into blocks of related variables  by find_field_cells.m.
[292]9%  The dimensionality of each block is obtained  by this function
[201]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%
[625]14% function [PlotType,PlotParamOut,haxes]= plot_field(Data,haxes,PlotParam,PosColorbar)
[201]15%
[822]16% OUPUT:
[866]17% PlotType: type of plot: 'text','line'(curve plot),'plane':2D view,'volume', or errormsg
[201]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%
[822]21%INPUT
[201]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%   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.
44%
[294]45%   PlotParam: structure containing the parameters for plotting, as read on the uvmat or view_field GUI (by function 'read_GUI.m').
46%      Contains three substructures:
[748]47%     .Axes: coordinate parameters:
[294]48%           .CheckFixLimits:=0 (default) adjust axes limit to the X,Y data, =1: preserves the previous axes limits
[748]49%     .Axes.CheckFixAspectRatio: =0 (default):automatic adjustment of the graph, keep 1 to 1 aspect ratio for x and y scales.
50%     .Axes.AspectRatio: imposed aspect ratio y/x of axis unit plots
[201]51%            --scalars--
52%    .Scalar.MaxA: upper bound (saturation color) for the scalar representation, max(field) by default
53%    .Scalar.MinA: lower bound (saturation) for the scalar representation, min(field) by default
[292]54%    .Scalar.CheckFixScal: =0 (default) lower and upper bounds of the scalar representation set to the min and max of the field
[201]55%               =1 lower and upper bound imposed by .AMax and .MinA
[421]56%    .Scalar.CheckBW= 1: black and white representation imposed, =0 color imposed (color scale or rgb),
57%                   =[]: automatic (B/W for integer positive scalars, color  else)
[292]58%    .Scalar.CheckContours= 1: represent scalars by contour plots (Matlab function 'contour'); =0 by default
[201]59%    .IncrA : contour interval
60%            -- vectors--
61%    .Vectors.VecScale: scale for the vector representation
[292]62%    .Vectors.CheckFixVec: =0 (default) automatic length for vector representation, =1: length set by .VecScale
63%    .Vectors.CheckHideFalse= 0 (default) false vectors represented in magenta, =1: false vectors not represented;
64%    .Vectors.CheckHideWarning= 0 (default) vectors marked by warnflag~=0 marked in black, 1: no warning representation;
65%    .Vectors.CheckDecimate4 = 0 (default) all vectors reprtesented, =1: half of  the vectors represented along each coordinate
[201]66%         -- vector color--
67%    .Vectors.ColorCode= 'black','white': imposed color  (default ='blue')
68%                        'rgb', : three colors red, blue, green depending
69%                        on thresholds .colcode1 and .colcode2 on the input  scalar value (C)
70%                        'brg': like rgb but reversed color order (blue, green, red)
71%                        '64 colors': continuous color from blue to red (multijet)
72%    .Vectors.colcode1 : first threshold for rgb, first value for'continuous'
73%    .Vectors.colcode2 : second threshold for rgb, last value (saturation) for 'continuous'
[292]74%    .Vectors.CheckFixedCbounds;  =0 (default): the bounds on C representation are min and max, =1: they are fixed by .Minc and .MaxC
[201]75%    .Vectors.MinC = imposed minimum of the scalar field used for vector color;
76%    .Vectors.MaxC = imposed maximum of the scalar field used for vector color;
77%
[546]78% PosColorbar: % if absent, no action on colorbar
79%              % if empty, suppress any existing colorbar
80%              % if not empty, display a colorbar for B&W images at position PosColorbar
81%                expressed in figure relative unit (ex [0.821 0.471 0.019 0.445])
[201]82
[924]83%=======================================================================
[977]84% Copyright 2008-2017, LEGI UMR 5519 / CNRS UGA G-INP, Grenoble, France
[924]85%   http://www.legi.grenoble-inp.fr
86%   Joel.Sommeria - Joel.Sommeria (A) legi.cnrs.fr
87%
[201]88%     This file is part of the toolbox UVMAT.
[924]89%
[201]90%     UVMAT is free software; you can redistribute it and/or modify
[924]91%     it under the terms of the GNU General Public License as published
92%     by the Free Software Foundation; either version 2 of the license,
93%     or (at your option) any later version.
94%
[201]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
[924]98%     GNU General Public License (see LICENSE.txt) for more details.
99%=======================================================================
[201]100
[781]101function [PlotType,PlotParamOut,haxes]= plot_field(Data,haxes,PlotParam)
[247]102
[388]103%% default input and output
[201]104if ~exist('PlotParam','var'),PlotParam=[];end;
105PlotType='text'; %default
[749]106if ~isfield(PlotParam,'Axes')
[748]107    PlotParam.Axes=[];
[582]108    if isfield(Data,'CoordUnit')
[748]109        PlotParam.Axes.CheckFixAspectRatio=1;
110        PlotParam.Axes.AspectRatio=1; %set axes equal by default if CoordUnit is defined
[582]111    end
[292]112end
[569]113PlotParamOut=PlotParam;%default
[201]114
[388]115%% check input structure
116% check the cells of fields :
[530]117[CellInfo,NbDimArray,errormsg]=find_field_cells(Data);
[388]118if ~isempty(errormsg)
[512]119    msgbox_uvmat('ERROR',['input of plot_field/find_field_cells: ' errormsg]);
[388]120    return
121end
[530]122
123index_3D=find(NbDimArray>2,1);
[388]124if ~isempty(index_3D)
[546]125    msgbox_uvmat('ERROR','volume plot not implemented yet');
126    return
[388]127end
[530]128index_2D=find(NbDimArray==2);%find 2D fields
129index_1D=find(NbDimArray==1);
130index_0D=find(NbDimArray==0);
[294]131
[388]132%% test axes and figure
[429]133testnewfig=1;%test to create a new figure (default)
134testzoomaxes=0;%test for the existence of a zoom secondary figure attached to the plotting axes
135if exist('haxes','var')
136    if ishandle(haxes)
137        if isequal(get(haxes,'Type'),'axes')
138            testnewfig=0;
139            AxeData=get(haxes,'UserData');
140            if isfield(AxeData,'ZoomAxes')&& ishandle(AxeData.ZoomAxes)
141                if isequal(get(AxeData.ZoomAxes,'Type'),'axes')
142                    testzoomaxes=1;
143                    zoomaxes=AxeData.ZoomAxes;
[388]144                end
145            end
146        end
[201]147    end
[429]148end
149
150%% create a new figure and axes if the plotting axes does not exist
151if testnewfig
152    hfig=figure;
153    set(hfig,'Units','normalized')
154    haxes=axes;
155    set(haxes,'position',[0.13,0.2,0.775,0.73])
[569]156    PlotParamOut.NextPlot='add'; %parameter for plot_profile
[429]157else
158    hfig=get(haxes,'parent');
159    set(0,'CurrentFigure',hfig)% the parent of haxes becomes the current figure
160    set(hfig,'CurrentAxes',haxes)%  haxes becomes the current axes of the parent figure
161end
162
163%% set axes properties
[748]164if isfield(PlotParamOut.Axes,'CheckFixLimits') && isequal(PlotParamOut.Axes.CheckFixLimits,1)  %adjust the graph limits
[429]165    set(haxes,'XLimMode', 'manual')
166    set(haxes,'YLimMode', 'manual')
167else
168    set(haxes,'XLimMode', 'auto')
169    set(haxes,'YLimMode', 'auto')
170end
[760]171
172if isfield(PlotParamOut.Axes,'CheckFixAspectRatio') && isequal(PlotParamOut.Axes.CheckFixAspectRatio,1)&&isfield(PlotParamOut.Axes,'AspectRatio')
173    set(haxes,'DataAspectRatioMode','manual')
174    set(haxes,'DataAspectRatio',[PlotParamOut.Axes.AspectRatio 1 1])
175else
176    set(haxes,'DataAspectRatioMode','auto')%automatic aspect ratio
177end
178
[429]179errormsg='';
180AxeData=get(haxes,'UserData');
181
182%% 2D plots
183if isempty(index_2D)
[781]184    plot_plane([],[],haxes,[]);%removes images or vector plots in the absence of 2D field plot
[429]185else  %plot 2D field
[781]186%     if ~exist('PosColorbar','var'),PosColorbar=[];end;
187    [tild,PlotParamOut,PlotType,errormsg]=plot_plane(Data,CellInfo(index_2D),haxes,PlotParamOut);
[429]188    AxeData.NbDim=2;
189    if testzoomaxes && isempty(errormsg)
[781]190        [zoomaxes,PlotParamOut,tild,errormsg]=plot_plane(Data,CellInfo(index_2D),zoomaxes,PlotParamOut);
[429]191        AxeData.ZoomAxes=zoomaxes;
[201]192    end
[429]193end
194
195%% 1D plot (usual graph y vs x)
196if isempty(index_1D)
197    if ~isempty(haxes)
[530]198        plot_profile([],[],haxes);%removes usual praphs y vs x in the absence of 1D field plot
[201]199    end
[429]200else %plot 1D field (usual graph y vs x)
[644]201    CheckHold=0;
202    if isfield(PlotParam,'CheckHold')
203        CheckHold= PlotParam.CheckHold;
204    end       
[874]205    PlotParamOut=plot_profile(Data,CellInfo(index_1D),haxes,PlotParamOut,CheckHold);%
[872]206    if isempty(index_2D)
207        if isfield(PlotParamOut,'Vectors')
208            PlotParamOut=rmfield(PlotParamOut,'Vectors');
209        end
210        if isfield(PlotParamOut,'Scalar')
211            PlotParamOut=rmfield(PlotParamOut,'Scalar');
212        end
[871]213    end
[429]214    if testzoomaxes
[748]215        [zoomaxes,PlotParamOut.Axes]=plot_profile(Data,CellInfo(index_1D),zoomaxes,PlotParamOut.Axes,CheckHold);
[429]216        AxeData.ZoomAxes=zoomaxes;
[201]217    end
[429]218    PlotType='line';
219end
220
[760]221%% aspect ratio
222AspectRatio=get(haxes,'DataAspectRatio');
223PlotParamOut.Axes.AspectRatio=AspectRatio(1)/AspectRatio(2);
224
[429]225%% text display
[873]226if ~(isfield(PlotParamOut,'Axes')&&isfield(PlotParamOut.Axes,'TextDisplay')&&(PlotParamOut.Axes.TextDisplay)) % if text is not already given as statistics
227    htext=findobj(hfig,'Tag','TableDisplay');
[874]228    if ~isempty(htext)%&&~isempty(hchecktable)
[873]229        if isempty(index_0D)
230            %         set(htext,'Data',{})
231            %         set(htext,'visible','off')
232            %         set(hchecktable,'visible','off')
233            %         set(hchecktable,'Value',0)
[434]234        else
[873]235            errormsg=plot_text(Data,CellInfo(index_0D),htext);
236            set(htext,'visible','on')
[874]237%             set(hchecktable,'visible','on')
238%             set(hchecktable,'Value',1)
[434]239        end
[873]240        set(hfig,'Unit','pixels');
241        set(htext,'Unit','pixels')
242        PosFig=get(hfig,'Position');
243        % case of no plot with view_field: only text display
244        if strcmp(get(hfig,'Tag'),'view_field')
245            if isempty(index_1D) && isempty(index_2D)% case of no plot: only text display
246                set(haxes,'Visible','off')
247                PosTable=get(htext,'Position');
248                set(hfig,'Position',[PosFig(1) PosFig(2)  PosTable(3) PosTable(4)])
249            else
250                set(haxes,'Visible','on')
251                set(hfig,'Position',[PosFig(1) PosFig(2)  877 677])%default size for view_field
252            end
253        end
[294]254    end
[201]255end
[388]256%% display error message
[201]257if ~isempty(errormsg)
[866]258    PlotType=errormsg;
[201]259    msgbox_uvmat('ERROR', errormsg)
260end
261
262%% update the parameters stored in AxeData
[429]263if ishandle(haxes)&&( ~isempty(index_2D)|| ~isempty(index_1D))
[388]264    if isfield(PlotParamOut,'MinX')
[569]265        AxeData.RangeX=[PlotParamOut.MinX PlotParamOut.MaxX];
266        AxeData.RangeY=[PlotParamOut.MinY PlotParamOut.MaxY];
[388]267    end
268    set(haxes,'UserData',AxeData)
269end
[201]270
[206]271%% update the plotted field stored in parent figure
[429]272if ~isempty(index_2D)|| ~isempty(index_1D)
273    FigData=get(hfig,'UserData');
[511]274    if strcmp(get(hfig,'tag'),'view_field')||strcmp(get(hfig,'tag'),'uvmat')
275        FigData.(get(haxes,'tag'))=Data;
[429]276        set(hfig,'UserData',FigData)
277    end
[388]278end
[292]279
[201]280%-------------------------------------------------------------------
[690]281% --- plot 0D fields: display data values without plot
282%------------------------------------------------------------------
[530]283function errormsg=plot_text(FieldData,CellInfo,htext)
[690]284
[581]285errormsg='';
[201]286txt_cell={};
[429]287Data={};
[690]288VarIndex=[];
[530]289for icell=1:length(CellInfo)
[535]290   
291    % select types of  variables to be projected
292    ListProj={'VarIndex_scalar','VarIndex_image','VarIndex_color','VarIndex_vector_x','VarIndex_vector_y'};
293    check_proj=false(size(FieldData.ListVarName));
294    for ilist=1:numel(ListProj)
295        if isfield(CellInfo{icell},ListProj{ilist})
296            check_proj(CellInfo{icell}.(ListProj{ilist}))=1;
297        end
298    end
[690]299    VarIndex=[VarIndex find(check_proj)];
300end
301
302% data need to be displayed in a table
[881]303% if strcmp(get(htext,'Type'),'uitable')% display data in a table
304%     VarNameCell=cell(1,numel(VarIndex));% prepare list of variable names to display (titles of columns)
305%     VarLength=zeros(1,numel(VarIndex));  % default number of values for each variable
306%     for ivar=1:numel(VarIndex)
307%         VarNameCell{ivar}=FieldData.ListVarName{VarIndex(ivar)};
308%         VarLength(ivar)=numel(FieldData.(VarNameCell{ivar}));
309%     end
310%     set(htext,'ColumnName',VarNameCell)
311%     Data=cell(max(VarLength),numel(VarIndex));% prepare the table of data display
312%     
313%     for ivar=1:numel(VarIndex)
314%         VarValue=FieldData.(VarNameCell{ivar});
315%         VarValue=reshape(VarValue,[],1);% reshape values array in a column
316%         Data(1:numel(VarValue),ivar)=num2cell(VarValue);
317%     end
318%     set(htext,'Data',Data)
319% end
[690]320%         if numel(VarValue)>1 && numel(VarValue)<10 % case of a variable with several values
321%             for ind=1:numel(VarValue)
322%                 VarNameCell{1,ind}=[VarName '_' num2str(ind)];% indicate each value by an index
323%             end
324%         else
325%             VarNameCell={VarName};
326%         end
327%         if numel(VarValue)<10
328%             if isempty(VarValue)
329%                 VarValueCell={'[]'};
330%             else
331%                 VarValueCell=num2cell(VarValue);
332%             end
333%             if isempty(Data)
334%                 Data =[VarNameCell VarValueCell];
335%             else
336%                 Data =[Data [VarNameCell VarValueCell]];
337%             end
338%         else
339%             if isempty(Data)
340%                 Data =[VarNameCell; num2cell(VarValue)];
341%             else
342%                 Data =[Data [VarNameCell; {['size ' num2str(size(VarValue))]}]];
343%             end
344%         end
345%         if size(VarValue,1)==1
346%             txt=[VarName '=' num2str(VarValue)];
347%             txt_cell=[txt_cell;{txt}];
348%         end
349%     end
350% end
351% if strcmp(get(htext,'Type'),'uitable')% display data in a table
352%
353
354%     set(htext,'Data',Data(2:end,:))
355% else  % display in a text edit box
356%     set(htext,'String',txt_cell)
357%     set(htext,'UserData',txt_cell)% for temporary storage when the edit box is used for mouse display
358% end
[201]359
[429]360
[201]361%-------------------------------------------------------------------
[690]362% --- plot 1D fields (usual x,y plots)
363%-------------------------------------------------------------------
[874]364function PlotParamOut=plot_profile(data,CellInfo,haxes,PlotParam,CheckHold)
[201]365
[644]366%% initialization
[874]367if ~(exist('PlotParam','var')&&~isempty(PlotParam.Axes))
[292]368    Coordinates=[];
[874]369    PlotParamOut.Axes=Coordinates;
370else
371    Coordinates=PlotParam.Axes;
372    PlotParamOut=PlotParam;
[201]373end
374hfig=get(haxes,'parent');
[644]375legend_str={};
376
377%% suppress existing plot if empty data
[201]378if isempty(data)
379    hplot=findobj(haxes,'tag','plot_line');
380    if ~isempty(hplot)
381        delete(hplot)
382    end
383    hlegend=findobj(hfig,'tag','legend');
384    if ~isempty(hlegend)
385        delete(hlegend)
386    end
387    return
388end
389
[644]390%% set the colors of the successive plots (designed to produce rgb for the three components of color images)
[651]391ColorOrder=[1 0 0;0 1 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];
392set(hfig,'DefaultAxesColorOrder',ColorOrder)
[644]393if CheckHold
[690]394    set(haxes,'NextPlot','add')
[644]395else
396    set(haxes,'NextPlot','replace')
[201]397end
398
399%% prepare the string for plot command
400plotstr='hhh=plot(';
401xtitle='';
402ytitle='';
[644]403test_newplot=~CheckHold;
[569]404MinX=[];
405MaxX=[];
406MinY_cell=[];
407MaxY_cell=[];
[871]408testplot=ones(size(data.ListVarName));%default test for plotted variables
[201]409%loop on input  fields
[530]410for icell=1:numel(CellInfo)
411    VarIndex=CellInfo{icell}.VarIndex;%  indices of the selected variables in the list data.ListVarName
412    coord_x_index=CellInfo{icell}.CoordIndex;
[426]413    coord_x_name{icell}=data.ListVarName{coord_x_index};
414    coord_x{icell}=data.(data.ListVarName{coord_x_index});%coordinate variable set as coord_x
[569]415    if isempty(find(strcmp(coord_x_name{icell},coord_x_name(1:end-1)), 1)) %xtitle not already selected
[426]416        xtitle=[xtitle coord_x_name{icell}];
417        if isfield(data,'VarAttribute')&& numel(data.VarAttribute)>=coord_x_index && isfield(data.VarAttribute{coord_x_index},'units')
418            xtitle=[xtitle '(' data.VarAttribute{coord_x_index}.units '), '];
419        else
420            xtitle=[xtitle ', '];
421        end
[201]422    end
[729]423    if ~isempty(coord_x{icell})
424        MinX(icell)=min(coord_x{icell});
425        MaxX(icell)=max(coord_x{icell});
426        testplot(coord_x_index)=0;
427        if isfield(CellInfo{icell},'VarIndex_ancillary')
428            testplot(CellInfo{icell}.VarIndex_ancillary)=0;
429        end
430        if isfield(CellInfo{icell},'VarIndex_warnflag')
431            testplot(CellInfo{icell}.VarIndex_warnflag)=0;
432        end
433        if isfield(data,'VarAttribute')
434            VarAttribute=data.VarAttribute;
435            for ivar=1:length(VarIndex)
436                if length(VarAttribute)>=VarIndex(ivar) && isfield(VarAttribute{VarIndex(ivar)},'long_name')
437                    plotname{VarIndex(ivar)}=VarAttribute{VarIndex(ivar)}.long_name;
438                else
439                    plotname{VarIndex(ivar)}=data.ListVarName{VarIndex(ivar)};%name for display in plot A METTRE
440                end
[201]441            end
442        end
[729]443        if isfield(CellInfo{icell},'VarIndex_discrete')
444            charplot_0='''+''';
445        else
446            charplot_0='''-''';
447        end
448        MinY=[];
449        MaxY=[];%default
450       
451        nbplot=0;
452        for ivar=1:length(VarIndex)
453            if testplot(VarIndex(ivar))
454                VarName=data.ListVarName{VarIndex(ivar)};
455                nbplot=nbplot+1;
456                ytitle=[ytitle VarName];
457                if isfield(data,'VarAttribute')&& numel(data.VarAttribute)>=VarIndex(ivar) && isfield(data.VarAttribute{VarIndex(ivar)},'units')
458                    ytitle=[ytitle '(' data.VarAttribute{VarIndex(ivar)}.units '), '];
459                else
460                    ytitle=[ytitle ', '];
461                end
462                eval(['data.' VarName '=squeeze(data.' VarName ');'])
463                MinY(ivar)=min(min(data.(VarName)));
464                MaxY(ivar)=max(max(data.(VarName)));
465                plotstr=[plotstr 'coord_x{' num2str(icell) '},data.' VarName ',' charplot_0 ','];
466                eval(['nbcomponent2=size(data.' VarName ',2);']);
467                eval(['nbcomponent1=size(data.' VarName ',1);']);
468                if numel(coord_x{icell})==2
469                    coord_x{icell}=linspace(coord_x{icell}(1),coord_x{icell}(2),nbcomponent1);
470                end
471                if nbcomponent1==1|| nbcomponent2==1
472                    legend_str=[legend_str {VarName}]; %variable with one component
473                else  %variable with severals  components
474                    for ic=1:min(nbcomponent1,nbcomponent2)
475                        legend_str=[legend_str [VarName '_' num2str(ic)]]; %variable with severals  components
476                    end                                                   % labeled by their index (e.g. color component)
477                end
[201]478            end
479        end
[729]480        if ~isempty(MinY)
481            MinY_cell(icell)=min(MinY);
482            MaxY_cell(icell)=max(MaxY);
483        end
[201]484    end
485end
486
487%% activate the plot
[644]488if  ~isequal(plotstr,'hhh=plot(') 
[201]489    set(hfig,'CurrentAxes',haxes)
490    tag=get(haxes,'tag');   
491    %%%
492    plotstr=[plotstr '''tag'',''plot_line'');'];   
493    eval(plotstr)                  %execute plot (instruction  plotstr)
494    %%%
[651]495    set(haxes,'tag',tag)% restitute the axes tag (removed by the command plot)
496    set(haxes,'ColorOrder',ColorOrder)% restitute the plot color order (to get red green blue for histograms or cuts of color images)
[201]497    grid(haxes, 'on')
498    hxlabel=xlabel(xtitle(1:end-2));% xlabel (removes ', ' at the end)
499    set(hxlabel,'Interpreter','none')% desable tex interpreter
500    if length(legend_str)>=1
501        hylabel=ylabel(ytitle(1:end-2));% ylabel (removes ', ' at the end)
502        set(hylabel,'Interpreter','none')% desable tex interpreter
503    end
504    if ~isempty(legend_str)
505        hlegend=findobj(hfig,'Tag','legend');
506        if isempty(hlegend)
507            hlegend=legend(legend_str);
508            txt=ver('MATLAB');
509            Release=txt.Release;
510            relnumb=str2double(Release(3:4));% should be changed to Version for better compatibility
511            if relnumb >= 14
512                set(hlegend,'Interpreter','none')% desable tex interpreter
513            end
514        else
515            legend_old=get(hlegend,'String');
516            if isequal(size(legend_old,1),size(legend_str,1))&&~isequal(legend_old,legend_str)
517                set(hlegend,'String',[legend_old legend_str]);
518            end
519        end
520    end
521    title_str='';
522    if isfield(data,'filename')
523       [Path, title_str, ext]=fileparts(data.filename);
524       title_str=[title_str ext];
525    end
[477]526    if isfield(data,'Action')&&isfield(data.Action,'ActionName')
[201]527        if ~isequal(title_str,'')
528            title_str=[title_str ', '];
529        end
[477]530        title_str=[title_str data.Action.ActionName];
[201]531    end
532    htitle=title(title_str);
[428]533    set(htitle,'Interpreter','none')% desable tex interpreter
[201]534end
535
536%% determine axes bounds
[294]537fix_lim=isfield(Coordinates,'CheckFixLimits') && Coordinates.CheckFixLimits;
[569]538check_lim=isfield(Coordinates,'MinX')&&isfield(Coordinates,'MaxX')&&isfield(Coordinates,'MinY')&&isfield(Coordinates,'MaxY');
[201]539if fix_lim
[569]540    if ~check_lim
541        fix_lim=0; %free limits if limits are not set,
[221]542    end
[201]543end
[294]544if fix_lim
545    set(haxes,'XLim',[Coordinates.MinX Coordinates.MaxX])
546    set(haxes,'YLim',[Coordinates.MinY Coordinates.MaxY])
[569]547else
548    if ~isempty(MinX)
549        if check_lim
[874]550            Coordinates.MinX=min(min(MinX),Coordinates.MinX);
551            Coordinates.MaxX=max(max(MaxX),Coordinates.MaxX);
[569]552        else
[874]553            Coordinates.MinX=min(MinX);
554            Coordinates.MaxX=max(MaxX);
[569]555        end
556    end
557    if ~isempty(MinY_cell)
558        if check_lim
[874]559            Coordinates.MinY=min(min(MinY_cell),Coordinates.MinY);
560            Coordinates.MaxY=max(max(MaxY_cell),Coordinates.MaxY);
[569]561        else
[874]562            Coordinates.MinY=min(MinY_cell);
563            Coordinates.MaxY=max(MaxY_cell);
[569]564        end
565    end
[201]566end
567
[428]568%% determine plot aspect ratio
[789]569if isfield(Coordinates,'CheckFixAspectRatio') && isequal(Coordinates.CheckFixAspectRatio,1)&&isfield(Coordinates,'AspectRatio')
570    set(haxes,'DataAspectRatioMode','manual')
571    set(haxes,'DataAspectRatio',[Coordinates.AspectRatio 1 1])
572else
573    set(haxes,'DataAspectRatioMode','auto')%automatic aspect ratio
574    AspectRatio=get(haxes,'DataAspectRatio');
[874]575    Coordinates.AspectRatio=AspectRatio(1)/AspectRatio(2);
[789]576end
[874]577PlotParamOut.Axes= Coordinates;
[428]578
[871]579%% give statistics for pdf
[880]580%ind_var=find(testplot);
[881]581TableData={'Variable';'SampleNbr';'bin size';'Mean';'RMS';'Skewness';'Kurtosis';...
[873]582    'Min';'FirstCentile';'FirstDecile';'Median';'LastDecile';'LastCentile';'Max'};
[880]583
[874]584TextDisplay=0;
[873]585for icell=1:numel(CellInfo)
586    if isfield(CellInfo{icell},'VarIndex_histo')
[874]587        TextDisplay=1;
[873]588        VarName=data.ListVarName{CellInfo{icell}.CoordIndex};
589        pdf_val=data.(data.ListVarName{CellInfo{icell}.VarIndex_histo});
590        x=coord_x{icell};
[881]591        if isrow(x)
592            x=x';
593        end
594        if ~isequal(size(x,1),size(pdf_val,1))
595            pdf_val=pdf_val';
596        end
597        Val=pdf2stat(x,pdf_val);
598        Column=mat2cell(Val,ones(13,1),ones(1,size(Val,2)));
599        if size(Val,2)==1%single component
600            TitleBar={VarName};
601        else
602            TitleBar=cell(1,size(Val,2));
603            for icomp=1:size(Val,2)
604                TitleBar{icomp}=[VarName '_' num2str(icomp)];
605            end
606        end
607        Column=[TitleBar;Column];
[873]608        TableData=[TableData Column];
609    end
610end
[874]611if TextDisplay;
[873]612    disp(TableData);
[874]613    PlotParamOut.TableDisplay=TableData;
614else
615    if isfield(PlotParamOut,'TableDisplay')
616        PlotParamOut=rmfield(PlotParamOut,'TableDisplay');
617    end
[871]618end
619   
[201]620%-------------------------------------------------------------------
[781]621function [haxes,PlotParamOut,PlotType,errormsg]=plot_plane(Data,CellInfo,haxes,PlotParam)
[201]622%-------------------------------------------------------------------
[674]623PlotType='plane';
624grid(haxes, 'off')% remove grid (possibly remaining from other graphs)
[411]625
[201]626%default plotting parameters
627if ~isfield(PlotParam,'Scalar')
628    PlotParam.Scalar=[];
629end
630if ~isfield(PlotParam,'Vectors')
631    PlotParam.Vectors=[];
632end
[674]633PlotParamOut=PlotParam;%default
634errormsg='';%default
[201]635
[674]636hfig=get(haxes,'parent');%handle of the figure containing the plot axes
[781]637PosColorbar=[];
638FigData=get(hfig,'UserData');
639if isfield(FigData,'PosColorbar')
640    PosColorbar=FigData.PosColorbar;
641end
[201]642hcol=findobj(hfig,'Tag','Colorbar'); %look for colorbar axes
643hima=findobj(haxes,'Tag','ima');% search existing image in the current axes
644test_ima=0; %default: test for image or map plot
645test_vec=0; %default: test for vector plots
646test_black=0;
647test_false=0;
648test_C=0;
649XName='';
650x_units='';
651YName='';
652y_units='';
[674]653
654% loop on the input field cells
655for icell=1:numel(CellInfo)
[530]656    if strcmp(CellInfo{icell}.CoordType,'tps') %do not plot directly tps data (used for projection only)
[402]657        continue
658    end
[530]659    ivar_X=CellInfo{icell}.CoordIndex(end); % defines (unique) index for the variable representing unstructured x coordinate (default =[])
660    ivar_Y=CellInfo{icell}.CoordIndex(end-1); % defines (unique)index for the variable representing unstructured y coordinate (default =[])
661    ivar_C=[];
662    if isfield(CellInfo{icell},'VarIndex_scalar')
663        ivar_C=[ivar_C CellInfo{icell}.VarIndex_scalar];
664    end
665    if isfield(CellInfo{icell},'VarIndex_image')
666        ivar_C=[ivar_C CellInfo{icell}.VarIndex_image];
667    end
668    if isfield(CellInfo{icell},'VarIndex_color')
669        ivar_C=[ivar_C CellInfo{icell}.VarIndex_color];
670    end
671    if isfield(CellInfo{icell},'VarIndex_ancillary')
672        ivar_C=[ivar_C CellInfo{icell}.VarIndex_ancillary];
673    end
[201]674    if numel(ivar_C)>1
675        errormsg= 'error in plot_field: too many scalar inputs';
676        return
677    end
[530]678    ivar_F=[];
679    if isfield(CellInfo{icell},'VarIndex_warnflag')
[674]680        ivar_F=CellInfo{icell}.VarIndex_warnflag; %defines index (unique) for warning flag variable
[201]681    end
[674]682    ivar_FF_vec=[];
[530]683    if isfield(CellInfo{icell},'VarIndex_vector_x')&&isfield(CellInfo{icell},'VarIndex_vector_y') % vector components detected
[674]684        if test_vec% a vector field has been already detected
[866]685            errormsg='error in plot_field: attempt to plot two vector fields: to get the difference project on a plane with ProjMode= interp_lin or interp_tps';
[201]686            return
687        else
[864]688            if numel(CellInfo{icell}.VarIndex_vector_x)>1
689                errormsg='error in plot_field: attempt to plot two vector fields';
690            return
691            end
[201]692            test_vec=1;
[674]693            if isfield(CellInfo{icell},'VarIndex_errorflag')
694                ivar_FF_vec=CellInfo{icell}.VarIndex_errorflag; %defines index (unique) for error flag variable
695            end
696            vec_U=Data.(Data.ListVarName{CellInfo{icell}.VarIndex_vector_x});
[530]697            vec_V=Data.(Data.ListVarName{CellInfo{icell}.VarIndex_vector_y});
[1001]698            XName=Data.ListVarName{CellInfo{icell}.CoordIndex(end)};
699            YName=Data.ListVarName{CellInfo{icell}.CoordIndex(end-1)};
[674]700            if strcmp(CellInfo{icell}.CoordType,'scattered')%2D field with unstructured coordinates
[517]701                vec_X=reshape(Data.(XName),[],1); %transform vectors in column matlab vectors
702                vec_Y=reshape(Data.(YName),[],1);
[674]703            elseif strcmp(CellInfo{icell}.CoordType,'grid')%2D field with structured coordinates
[1001]704                y=Data.(YName);
705                x=Data.(XName);
[201]706                if numel(y)==2 % y defined by first and last values on aregular mesh
707                    y=linspace(y(1),y(2),size(vec_U,1));
708                end
709                if numel(x)==2 % y defined by first and last values on aregular mesh
710                    x=linspace(x(1),x(2),size(vec_U,2));
711                end
[674]712                [vec_X,vec_Y]=meshgrid(x,y);
[201]713            end
[517]714            if isfield(PlotParam.Vectors,'ColorScalar') && ~isempty(PlotParam.Vectors.ColorScalar)
715                [VarVal,ListVarName,VarAttribute,errormsg]=calc_field_interp([],Data,PlotParam.Vectors.ColorScalar);
[530]716                if ~isempty(VarVal)
717                    vec_C=reshape(VarVal{1},1,numel(VarVal{1}));
718                    test_C=1;
719                end
[201]720            end
[674]721            if ~isempty(ivar_F)%~(isfield(PlotParam.Vectors,'HideWarning')&& isequal(PlotParam.Vectors.HideWarning,1))
722%                 if test_vec
[313]723                    vec_F=Data.(Data.ListVarName{ivar_F}); % warning flags for  dubious vectors
[674]724                    if  ~(isfield(PlotParam.Vectors,'CheckHideWarning') && isequal(PlotParam.Vectors.CheckHideWarning,1))
[201]725                        test_black=1;
726                    end
[674]727%                 end
[201]728            end
[674]729            if ~isempty(ivar_FF_vec) %&& ~test_false
730%                 if test_vec% TODO: deal with FF for structured coordinates
731                    vec_FF=Data.(Data.ListVarName{ivar_FF_vec}); % flags for false vectors
732%                 end
[201]733            end
734        end
735    elseif ~isempty(ivar_C) %scalar or image
736        if test_ima
[674]737            errormsg='attempt to plot two scalar fields or images';
[201]738            return
739        end
[530]740        A=squeeze(Data.(Data.ListVarName{ivar_C}));% scalar represented as color image
[201]741        test_ima=1;
[674]742        if strcmp(CellInfo{icell}.CoordType,'scattered')%2D field with unstructured coordinates
[227]743            A=reshape(A,1,[]);
[201]744            XName=Data.ListVarName{ivar_X};
745            YName=Data.ListVarName{ivar_Y};
[1001]746            Coord_x=reshape(Data.(XName),1,[]);
747            Coord_y=reshape(Data.(YName),1,[]);
[782]748            [A,Coord_x,Coord_y]=proj_grid(Coord_x',Coord_y',A',[],[],'np>256');  % interpolate on a grid
[201]749            if isfield(Data,'VarAttribute')
750                if numel(Data.VarAttribute)>=ivar_X && isfield(Data.VarAttribute{ivar_X},'units')
751                    x_units=[' (' Data.VarAttribute{ivar_X}.units ')'];
752                end
753                if numel(Data.VarAttribute)>=ivar_Y && isfield(Data.VarAttribute{ivar_Y},'units')
754                    y_units=[' (' Data.VarAttribute{ivar_Y}.units ')'];
755                end
[674]756            end
757        elseif strcmp(CellInfo{icell}.CoordType,'grid')%2D field with structured coordinates
[530]758            YName=Data.ListVarName{CellInfo{icell}.CoordIndex(end-1)};
[1001]759            XName=Data.ListVarName{CellInfo{icell}.CoordIndex(end)};
[782]760            Coord_y=Data.(YName);
[1001]761            Coord_x=Data.(XName);
[201]762            test_interp_X=0; %default, regularly meshed X coordinate
763            test_interp_Y=0; %default, regularly meshed Y coordinate
764            if isfield(Data,'VarAttribute')
[530]765                if numel(Data.VarAttribute)>=CellInfo{icell}.CoordIndex(end) && isfield(Data.VarAttribute{CellInfo{icell}.CoordIndex(end)},'units')
766                    x_units=Data.VarAttribute{CellInfo{icell}.CoordIndex(end)}.units;
[201]767                end
[530]768                if numel(Data.VarAttribute)>=CellInfo{icell}.CoordIndex(end-1) && isfield(Data.VarAttribute{CellInfo{icell}.CoordIndex(end-1)},'units')
769                    y_units=Data.VarAttribute{CellInfo{icell}.CoordIndex(end-1)}.units;
[201]770                end
[674]771            end
[782]772            if numel(Coord_y)>2
773                DCoord_y=diff(Coord_y);
774                DCoord_y_min=min(DCoord_y);
775                DCoord_y_max=max(DCoord_y);
776                if sign(DCoord_y_min)~=sign(DCoord_y_max);% =1 for increasing values, 0 otherwise
[1001]777                    errormsg=['errror in plot_field.m: non monotonic dimension variable ' YName ];
[674]778                    return
779                end
[782]780                test_interp_Y=(DCoord_y_max-DCoord_y_min)> 0.0001*abs(DCoord_y_max);
[201]781            end
[782]782            if numel(Coord_x)>2
783                DCoord_x=diff(Coord_x);
784                DCoord_x_min=min(DCoord_x);
785                DCoord_x_max=max(DCoord_x);
786                if sign(DCoord_x_min)~=sign(DCoord_x_max);% =1 for increasing values, 0 otherwise
[674]787                    errormsg=['errror in plot_field.m: non monotonic dimension variable ' Data.ListVarName{VarRole.coord(2)} ];
788                    return
789                end
[782]790                test_interp_X=(DCoord_x_max-DCoord_x_min)> 0.0001*abs(DCoord_x_max);
[674]791            end
792            if test_interp_Y
[782]793                npxy(1)=max([256 floor((Coord_y(end)-Coord_y(1))/DCoord_y_min) floor((Coord_y(end)-Coord_y(1))/DCoord_y_max)]);
794                yI=linspace(Coord_y(1),Coord_y(end),npxy(1));
[201]795                if ~test_interp_X
[782]796                    xI=linspace(Coord_x(1),Coord_x(end),size(A,2));%default
797                    Coord_x=xI;
[201]798                end
799            end
[674]800            if test_interp_X
[782]801                npxy(2)=max([256 floor((Coord_x(end)-Coord_x(1))/DCoord_x_min) floor((Coord_x(end)-Coord_x(1))/DCoord_x_max)]);
802                xI=linspace(Coord_x(1),Coord_x(end),npxy(2));
[201]803                if ~test_interp_Y
[782]804                    yI=linspace(Coord_y(1),Coord_y(end),size(A,1));
805                    Coord_y=yI;
[201]806                end
807            end
[674]808            if test_interp_X || test_interp_Y
[782]809                [Coord_x2D,Coord_y2D]=meshgrid(Coord_x,Coord_y);
810                A=interp2(Coord_x2D,Coord_y2D,double(A),xI,yI');
[201]811            end
[782]812            Coord_x=[Coord_x(1) Coord_x(end)];% keep only the lower and upper bounds for image represnetation
813            Coord_y=[Coord_y(1) Coord_y(end)];
[201]814        end
815    end
816    %define coordinates as CoordUnits, if not defined as attribute for each variable
817    if isfield(Data,'CoordUnit')
818        if isempty(x_units)
819            x_units=Data.CoordUnit;
820        end
821        if isempty(y_units)
822            y_units=Data.CoordUnit;
823        end
[674]824    end   
825end
[688]826PlotParamOut=PlotParam; % output plot parameters equal to input by default
[201]827
828%%   image or scalar plot %%%%%%%%%%%%%%%%%%%%%%%%%%
829if test_ima
830    % distinguish B/W and color images
831    np=size(A);%size of image
832    siz=numel(np);
833    if siz>3
[428]834        errormsg=['unrecognized scalar type: ' num2str(siz) ' dimensions'];
835        return
[201]836    end
837    if siz==3
838        if np(3)==1
839            siz=2;%B W image
840        elseif np(3)==3
841            siz=3;%color image
842        else
[206]843            errormsg=['unrecognized scalar type in plot_field: considered as 2D field with ' num2str(np(3)) ' color components'];
[201]844            return
845        end
846    end
847   
[822]848    %set for grey scale setting
[421]849    if isfield(PlotParam.Scalar,'CheckBW') && ~isempty(PlotParam.Scalar.CheckBW)
850        BW=PlotParam.Scalar.CheckBW; %BW=0 color imposed, else gray scale imposed.
851    else % BW imposed automatically chosen
[201]852        BW=(siz==2) && (isa(A,'uint8')|| isa(A,'uint16'));% non color images represented in gray scale by default
[421]853        PlotParamOut.Scalar.CheckBW=BW;
[428]854    end
[688]855   
[822]856    % determine the plot option 'image' or 'contours'
857    CheckContour=0; %default
[688]858    if isfield(PlotParam.Scalar,'ListContour')
859        CheckContour=strcmp(PlotParam.Scalar.ListContour,'contours');% =1 for contour plot option
[822]860    end
861   
862    %case of grey level images or contour plot
863    if ~isfield(PlotParam.Scalar,'CheckFixScalar')
864        PlotParam.Scalar.CheckFixScalar=0;% free scalar threshold value scale (from min to max) by default
865    end
866    if ~isfield(PlotParam.Scalar,'MinA')
867        PlotParam.Scalar.MinA=[];%no min scalar threshold value set
868    end
869    if ~isfield(PlotParam.Scalar,'MaxA')
870        PlotParam.Scalar.MaxA=[];%no max scalar threshold value set
871    end
872   
873    % determine the min scalar value
874    if PlotParam.Scalar.CheckFixScalar && ~isempty(PlotParam.Scalar.MinA) && isnumeric(PlotParam.Scalar.MinA)
875        MinA=double(PlotParam.Scalar.MinA); % min value set as input
[688]876    else
[822]877        MinA=double(min(min(min(A)))); % min value set as min of non NaN scalar values
[688]878    end
879   
[822]880    % error if the input scalar is NaN everywhere
881    if isnan(MinA)
882        errormsg='NaN input scalar or image in plot_field';
883        return
884    end
885   
886    % determine the max scalar value
887    CheckFixScalar=0;
888    if PlotParam.Scalar.CheckFixScalar && ~isempty(PlotParam.Scalar.MaxA) && isnumeric(PlotParam.Scalar.MaxA)
889        MaxA=double(PlotParam.Scalar.MaxA); % max value set as input
890        CheckFixScalar=1;
891    else
892        MaxA=double(max(max(max(A)))); % max value set as min of non NaN scalar values
893    end
894   
895    PlotParamOut.Scalar.MinA=MinA;
896    PlotParamOut.Scalar.MaxA=MaxA;
897    PlotParamOut.Scalar.Npx=size(A,2);
898    PlotParamOut.Scalar.Npy=size(A,1);
899    %     if siz==2
900    % case of contour plot
901    if CheckContour
902        if ~isempty(hima) && ishandle(hima)
903            delete(hima) % delete existing image
[201]904        end
[822]905       
906        % set the contour values
907        if ~isfield(PlotParam.Scalar,'IncrA')
908            PlotParam.Scalar.IncrA=[];% automatic contour interval
[201]909        end
[822]910        if ~isempty(PlotParam.Scalar.IncrA) && isnumeric(PlotParam.Scalar.IncrA)
911            interval=PlotParam.Scalar.IncrA;
912        else % automatic contour interval
913            cont=colbartick(MinA,MaxA);
914            interval=cont(2)-cont(1);%default
915            PlotParamOut.Scalar.IncrA=interval;% set the interval as output for display on the GUI
[201]916        end
[822]917        abscontmin=interval*floor(MinA/interval);
918        abscontmax=interval*ceil(MaxA/interval);
919        contmin=interval*floor(min(min(A))/interval);
920        contmax=interval*ceil(max(max(A))/interval);
921        cont_pos_plus=0:interval:contmax;% zero and positive contour values (plotted as solid lines)
922        cont_pos_min=double(contmin):interval:-interval;% negative contour values (plotted as dashed lines)
923        cont_pos=[cont_pos_min cont_pos_plus];% set of all contour values
[688]924       
[822]925        sizpx=(Coord_x(end)-Coord_x(1))/(np(2)-1);
926        sizpy=(Coord_y(1)-Coord_y(end))/(np(1)-1);
927        x_cont=Coord_x(1):sizpx:Coord_x(end); % pixel x coordinates for image display
928        y_cont=Coord_y(1):-sizpy:Coord_y(end); % pixel x coordinates for image display
[688]929       
[822]930        %axes(haxes)% set the input axes handle as current axis
931       
932        % colormap(map);
933        tag_axes=get(haxes,'Tag');% axes tag
934        Opacity=1;
935        if isfield(PlotParam.Scalar,'Opacity')&&~isempty(PlotParam.Scalar.Opacity)
936            Opacity=PlotParam.Scalar.Opacity;
[688]937        end
[822]938        % fill the space between contours if opacity is undefined or =1
939        if isequal(Opacity,1)
940            [var,hcontour]=contour(haxes,x_cont,y_cont,A,cont_pos);% determine all contours
941            set(hcontour,'Fill','on')% fill the space between contours
942            set(hcontour,'LineStyle','none')
943            hold on
944        end
945        [var_p,hcontour_p]=contour(haxes,x_cont,y_cont,A,cont_pos_plus,'k-');% draw the contours for positive values as solid lines
946        hold on
947        [var_m,hcontour_m]=contour(haxes,x_cont,y_cont,A,cont_pos_min,'--');% draw the contours for negative values as dashed lines
948        if isequal(Opacity,1)
949            set(hcontour_m,'LineColor',[1 1 1])% draw negative contours in white (better visibility in dark background)
950        end
951        set(haxes,'Tag',tag_axes);% restore axes tag (removed by the matlab fct contour !)
952        hold off
[688]953       
[822]954        %determine the color scale and map
955        caxis([abscontmin abscontmax])
956        if BW
957            vec=linspace(0,1,(abscontmax-abscontmin)/interval);%define a greyscale colormap with steps interval
958            map=[vec' vec' vec'];
959            colormap(map);
[201]960        else
[943]961            colormap('jet'); % default matlab colormap ('jet')
[822]962        end
[688]963       
[822]964        if isfield(PlotParam.Axes,'CheckFixAspectRatio') && isequal(PlotParam.Axes.CheckFixAspectRatio,1)
965            set(haxes,'DataAspectRatioMode','manual')
966            if isfield(PlotParam.Axes,'AspectRatio')
967                set(haxes,'DataAspectRatio',[PlotParam.Axes.AspectRatio 1 1])
[688]968            else
[822]969                set(haxes,'DataAspectRatio',[1 1 1])
[201]970            end
[822]971        end
972    else %usual images (no contour)
[201]973        % set  colormap for  image display
[822]974        if BW
975            vec=linspace(0,1,255);%define a linear greyscale colormap
976            map=[vec' vec' vec'];
977            colormap(map);  %grey scale color map
978            if siz==3% true color images visualized in BW
979                A=uint16(sum(A,3));%sum the three color components for color images displayed with BW option
[201]980            end
[822]981        else
982            if siz==3 && CheckFixScalar % true color images rescaled by MaxA
983                  A=uint8(255*double(A)/double(MaxA));
984            end
985            colormap('default'); % standard false colors for div, vort , scalar fields
[201]986        end
987       
988        % interpolate field to increase resolution of image display
[652]989        test_interp=0;
[822]990        if size(A,3)==1 % scalar or B/W image
[652]991            test_interp=1;
992            if max(np) <= 64
993                npxy=8*np;% increase the resolution 8 times
994            elseif max(np) <= 128
995                npxy=4*np;% increase the resolution 4 times
996            elseif max(np) <= 256
997                npxy=2*np;% increase the resolution 2 times
998            else
999                npxy=np;
1000                test_interp=0; % no interpolation done
1001            end
[201]1002        end
[652]1003        if test_interp%if we interpolate
[782]1004            x=linspace(Coord_x(1),Coord_x(2),np(2));
1005            y=linspace(Coord_y(1),Coord_y(2),np(1));
[201]1006            [X,Y]=meshgrid(x,y);
[782]1007            xi=linspace(Coord_x(1),Coord_x(2),npxy(2));
1008            yi=linspace(Coord_y(1),Coord_y(2),npxy(1));
[688]1009            A = interp2(X,Y,double(A),xi,yi');
[428]1010        end
[848]1011        % create new image if no image handle is found
[428]1012        if isempty(hima)
[201]1013            tag=get(haxes,'Tag');
1014            if MinA<MaxA
[782]1015                hima=imagesc(Coord_x,Coord_y,A,[MinA MaxA]);
[201]1016            else % to deal with uniform field
[782]1017                hima=imagesc(Coord_x,Coord_y,A,[MaxA-1 MaxA]);
[201]1018            end
[428]1019            % the function imagesc reset the axes 'DataAspectRatioMode'='auto', change if .CheckFixAspectRatio is
[411]1020            % requested:
1021            set(hima,'Tag','ima')
1022            set(hima,'HitTest','off')
[428]1023            set(haxes,'Tag',tag);%preserve the axes tag (removed by image fct !!!)
[201]1024            uistack(hima, 'bottom')
[428]1025            % update an existing image
[201]1026        else
[688]1027            set(hima,'CData',A);
[201]1028            if MinA<MaxA
1029                set(haxes,'CLim',[MinA MaxA])
1030            else
[238]1031                set(haxes,'CLim',[MinA MaxA+1])
[201]1032            end
[782]1033            set(hima,'XData',Coord_x);
1034            set(hima,'YData',Coord_y);
[201]1035        end
[546]1036       
[405]1037        % set the transparency to 0.5 if vectors are also plotted
[428]1038        if isfield(PlotParam.Scalar,'Opacity')&& ~isempty(PlotParam.Scalar.Opacity)
1039            set(hima,'AlphaData',PlotParam.Scalar.Opacity)
[405]1040        else
[428]1041            if test_vec
1042                set(hima,'AlphaData',0.5)%set opacity to 0.5 by default in the presence of vectors
1043                PlotParamOut.Scalar.Opacity=0.5;
1044            else
1045                set(hima,'AlphaData',1)% full opacity (no transparency) by default
1046            end
[405]1047        end
[201]1048    end
1049    test_ima=1;
1050   
1051    %display the colorbar code for B/W images if Poscolorbar not empty
[546]1052    if ~isempty(PosColorbar)
[822]1053        if size(A,3)==1 && exist('PosColorbar','var')
[546]1054            if isempty(hcol)||~ishandle(hcol)
1055                hcol=colorbar;%create new colorbar
1056            end
1057            if length(PosColorbar)==4
1058                set(hcol,'Position',PosColorbar)
1059            end
1060            %YTick=0;%default
1061            if MaxA>MinA
1062                if CheckContour
1063                    colbarlim=get(hcol,'YLim');
1064                    scale_bar=(colbarlim(2)-colbarlim(1))/(abscontmax-abscontmin);
1065                    YTick=cont_pos(2:end-1);
1066                    YTick_scaled=colbarlim(1)+scale_bar*(YTick-abscontmin);
1067                    set(hcol,'YTick',YTick_scaled);
1068                elseif (isfield(PlotParam.Scalar,'CheckBW') && isequal(PlotParam.Scalar.CheckBW,1))||isa(A,'uint8')|| isa(A,'uint16')%images
1069                    hi=get(hcol,'children');
1070                    if iscell(hi)%multiple images in colorbar
1071                        hi=hi{1};
1072                    end
1073                    set(hi,'YData',[MinA MaxA])
1074                    set(hi,'CData',(1:256)')
1075                    set(hcol,'YLim',[MinA MaxA])
1076                    YTick=colbartick(MinA,MaxA);
1077                    set(hcol,'YTick',YTick)
1078                else
1079                    hi=get(hcol,'children');
1080                    if iscell(hi)%multiple images in colorbar
1081                        hi=hi{1};
1082                    end
1083                    set(hi,'YData',[MinA MaxA])
1084                    set(hi,'CData',(1:64)')
1085                    YTick=colbartick(MinA,MaxA);
1086                    set(hcol,'YLim',[MinA MaxA])
1087                    set(hcol,'YTick',YTick)
[201]1088                end
[546]1089                set(hcol,'Yticklabel',num2str(YTick'));
[201]1090            end
[546]1091        elseif ishandle(hcol)
1092            delete(hcol); %erase existing colorbar if not needed
[201]1093        end
1094    end
1095else%no scalar plot
[428]1096    if ~isempty(hima) && ishandle(hima)
[201]1097        delete(hima)
1098    end
[546]1099    if ~isempty(PosColorbar) && ~isempty(hcol)&& ishandle(hcol)
[428]1100        delete(hcol)
[201]1101    end
1102    PlotParamOut=rmfield(PlotParamOut,'Scalar');
1103end
1104
1105%%   vector plot %%%%%%%%%%%%%%%%%%%%%%%%%%
1106if test_vec
1107   %vector scale representation
1108    if size(vec_U,1)==numel(vec_Y) && size(vec_U,2)==numel(vec_X); % x, y  coordinate variables
1109        [vec_X,vec_Y]=meshgrid(vec_X,vec_Y);
1110    end   
1111    vec_X=reshape(vec_X,1,numel(vec_X));%reshape in matlab vectors
1112    vec_Y=reshape(vec_Y,1,numel(vec_Y));
1113    vec_U=reshape(vec_U,1,numel(vec_U));
1114    vec_V=reshape(vec_V,1,numel(vec_V));
1115     MinMaxX=max(vec_X)-min(vec_X);
[294]1116    if  isfield(PlotParam.Vectors,'CheckFixVectors') && isequal(PlotParam.Vectors.CheckFixVectors,1)&& isfield(PlotParam.Vectors,'VecScale')...
[210]1117               &&~isempty(PlotParam.Vectors.VecScale) && isa(PlotParam.Vectors.VecScale,'double') %fixed vector scale
1118        scale=PlotParam.Vectors.VecScale;  %impose the length of vector representation
1119    else
1120        if ~test_false %remove false vectors   
[201]1121            indsel=1:numel(vec_X);%
1122        end
1123        if isempty(vec_U)
1124            scale=1;
1125        else
1126            if isempty(indsel)
1127                MaxU=max(abs(vec_U));
1128                MaxV=max(abs(vec_V));
1129            else
1130                MaxU=max(abs(vec_U(indsel)));
1131                MaxV=max(abs(vec_V(indsel)));
1132            end
1133            scale=MinMaxX/(max(MaxU,MaxV)*50);
1134            PlotParam.Vectors.VecScale=scale;%update the 'scale' display
1135        end
[210]1136    end
[201]1137   
1138    %record vectors on the plotting axes
1139    if test_C==0
1140        vec_C=ones(1,numel(vec_X));
1141    end
1142   
1143    %decimate by a factor 2 in vector mesh(4 in nbre of vectors)
[581]1144    check_decimate=0;
[294]1145    if isfield(PlotParam.Vectors,'CheckDecimate4') && PlotParam.Vectors.CheckDecimate4
[581]1146        check_decimate=1;
[201]1147        diffy=diff(vec_Y); %difference dy=vec_Y(i+1)-vec_Y(i)
1148        dy_thresh=max(abs(diffy))/2;
1149        ind_jump=find(abs(diffy) > dy_thresh); %indices with diff(vec_Y)> max/2, detect change of line
1150        ind_sel=1:ind_jump(1);%select the first line
1151        for i=2:2:length(ind_jump)-1
1152            ind_sel=[ind_sel (ind_jump(i)+1:ind_jump(i+1))];% select the odd lines
1153        end
1154        nb_sel=length(ind_sel);
1155        ind_sel=ind_sel(1:2:nb_sel);% take half the points on a line
[581]1156    elseif isfield(PlotParam.Vectors,'CheckDecimate16') && PlotParam.Vectors.CheckDecimate16
1157        check_decimate=1;
1158        diffy=diff(vec_Y); %difference dy=vec_Y(i+1)-vec_Y(i)
1159        dy_thresh=max(abs(diffy))/2;
1160        ind_jump=find(abs(diffy) > dy_thresh); %indices with diff(vec_Y)> max/2, detect change of line
1161        ind_sel=1:ind_jump(1);%select the first line
1162        for i=2:4:length(ind_jump)-1
1163            ind_sel=[ind_sel (ind_jump(i)+1:ind_jump(i+1))];% select the odd lines
1164        end
1165        nb_sel=length(ind_sel);
1166        ind_sel=ind_sel(1:4:nb_sel);% take half the points on a line
1167    end
1168    if check_decimate
[201]1169        vec_X=vec_X(ind_sel);
1170        vec_Y=vec_Y(ind_sel);
1171        vec_U=vec_U(ind_sel);
1172        vec_V=vec_V(ind_sel);
1173        vec_C=vec_C(ind_sel);
1174        if ~isempty(ivar_F)
1175           vec_F=vec_F(ind_sel);
1176        end
[674]1177        if ~isempty(ivar_FF_vec)
[201]1178           vec_FF=vec_FF(ind_sel);
1179        end
1180    end
1181   
1182    %get main level color code
1183    [colorlist,col_vec,PlotParamOut.Vectors]=set_col_vec(PlotParam.Vectors,vec_C);
1184   
1185    % take flags into account: add flag colors to the list of colors
1186    sizlist=size(colorlist);
1187    nbcolor=sizlist(1);
1188    if test_black
1189       nbcolor=nbcolor+1;
1190       colorlist(nbcolor,:)=[0 0 0]; %add black to the list of colors
[674]1191       if ~isempty(ivar_FF_vec)
[210]1192            col_vec(vec_F~=1 & vec_F~=0 & vec_FF==0)=nbcolor;
[201]1193       else
[210]1194            col_vec(vec_F~=1 & vec_F~=0)=nbcolor;
[201]1195       end
1196    end
1197    nbcolor=nbcolor+1;
[674]1198    if ~isempty(ivar_FF_vec)
[294]1199        if isfield(PlotParam.Vectors,'CheckHideFalse') && PlotParam.Vectors.CheckHideFalse==1
[201]1200            colorlist(nbcolor,:)=[NaN NaN NaN];% no plot of false vectors
1201        else
1202            colorlist(nbcolor,:)=[1 0 1];% magenta color
1203        end
[210]1204        col_vec(vec_FF~=0)=nbcolor;
[201]1205    end
1206    %plot vectors:
1207    quiresetn(haxes,vec_X,vec_Y,vec_U,vec_V,scale,colorlist,col_vec);   
1208
1209else
1210    hvec=findobj(haxes,'Tag','vel');
1211    if ~isempty(hvec)
1212        delete(hvec);
1213    end
1214    PlotParamOut=rmfield(PlotParamOut,'Vectors');
1215end
[546]1216% nbvar=0;
[201]1217
1218%store the coordinate extrema occupied by the field
1219if ~isempty(Data)
[569]1220    MinX=[];
1221    MaxX=[];
1222    MinY=[];
1223    MaxY=[];
[748]1224    fix_lim=isfield(PlotParam.Axes,'CheckFixLimits') && PlotParam.Axes.CheckFixLimits;
[201]1225    if fix_lim
[748]1226        if isfield(PlotParam.Axes,'MinX')&&isfield(PlotParam.Axes,'MaxX')&&isfield(PlotParam.Axes,'MinY')&&isfield(PlotParam.Axes,'MaxY')
1227            MinX=PlotParam.Axes.MinX;
1228            MaxX=PlotParam.Axes.MaxX;
1229            MinY=PlotParam.Axes.MinY;
1230            MaxY=PlotParam.Axes.MaxY;
[569]1231        end  %else PlotParamOut.MinX =PlotParam.MinX...
[252]1232    else
1233        if test_ima %both background image and vectors coexist, take the wider bound
[782]1234            MinX=min(Coord_x);
1235            MaxX=max(Coord_x);
1236            MinY=min(Coord_y);
1237            MaxY=max(Coord_y);
[252]1238            if test_vec
[569]1239                MinX=min(MinX,min(vec_X));
1240                MaxX=max(MaxX,max(vec_X));
1241                MinY=min(MinY,min(vec_Y));
1242                MaxY=max(MaxY,max(vec_Y));
[252]1243            end
1244        elseif test_vec
[569]1245            MinX=min(vec_X);
1246            MaxX=max(vec_X);
1247            MinY=min(vec_Y);
1248            MaxY=max(vec_Y);
[201]1249        end
[252]1250    end
[748]1251    PlotParamOut.Axes.MinX=MinX;
1252    PlotParamOut.Axes.MaxX=MaxX;
1253    PlotParamOut.Axes.MinY=MinY;
1254    PlotParamOut.Axes.MaxY=MaxY;
[569]1255    if MaxX>MinX
1256        set(haxes,'XLim',[MinX MaxX]);% set x limits of frame in axes coordinates
[546]1257    end
[569]1258    if MaxY>MinY
1259        set(haxes,'YLim',[MinY MaxY]);% set x limits of frame in axes coordinates
[546]1260    end
[201]1261    set(haxes,'YDir','normal')
1262    set(get(haxes,'XLabel'),'String',[XName ' (' x_units ')']);
1263    set(get(haxes,'YLabel'),'String',[YName ' (' y_units ')']);
[748]1264    PlotParamOut.Axes.x_units=x_units;
1265    PlotParamOut.Axes.y_units=y_units;
[201]1266end
[760]1267% if isfield(PlotParam,'Axes') && isfield(PlotParam.Axes,'CheckFixAspectRatio') && isequal(PlotParam.Axes.CheckFixAspectRatio,1)
1268%     set(haxes,'DataAspectRatioMode','manual')
1269%     if isfield(PlotParam.Axes,'AspectRatio')
1270%         set(haxes,'DataAspectRatio',[PlotParam.Axes.AspectRatio 1 1])
1271%     end
1272% else
1273%     set(haxes,'DataAspectRatioMode','auto')
1274% end
1275
[201]1276%-------------------------------------------------------------------
1277% --- function for plotting vectors
1278%INPUT:
1279% haxes: handles of the plotting axes
1280% x,y,u,v: vectors coordinates and vector components to plot, arrays withb the same dimension
1281% scale: scaling factor for vector length representation
1282% colorlist(icolor,:): list of vector colors, dim (nbcolor,3), depending on color #i
1283% col_vec: matlab vector setting the color number #i for each velocity vector
1284function quiresetn(haxes,x,y,u,v,scale,colorlist,col_vec)
1285%-------------------------------------------------------------------
1286%define arrows
1287theta=0.5 ;%angle arrow
1288alpha=0.3 ;%length arrow
1289rot=alpha*[cos(theta) -sin(theta); sin(theta) cos(theta)]';
1290%find the existing lines
1291h=findobj(haxes,'Tag','vel');% search existing lines in the current axes
1292sizh=size(h);
[932]1293%set(h,'EraseMode','xor');
[201]1294set(haxes,'NextPlot','replacechildren');
1295
1296%drawnow
1297%create lines (if no lines) or modify them
1298if ~isequal(size(col_vec),size(x))
1299    col_vec=ones(size(x));% case of error in col_vec input
1300end
1301sizlist=size(colorlist);
1302ncolor=sizlist(1);
1303
1304for icolor=1:ncolor
1305    %determine the line positions for each color icolor
1306    ind=find(col_vec==icolor);
1307    xc=x(ind);
1308    yc=y(ind);
1309    uc=u(ind)*scale;
1310    vc=v(ind)*scale;
1311    n=size(xc);
1312    xN=NaN*ones(size(xc));
1313    matx=[xc(:)-uc(:)/2 xc(:)+uc(:)/2 xN(:)]';
1314    matx=reshape(matx,1,3*n(2));
1315    maty=[yc(:)-vc(:)/2 yc(:)+vc(:)/2 xN(:)]';
1316    maty=reshape(maty,1,3*n(2));
1317   
1318    %determine arrow heads
1319    arrowplus=rot*[uc;vc];
1320    arrowmoins=rot'*[uc;vc];
1321    x1=xc+uc/2-arrowplus(1,:);
1322    x2=xc+uc/2;
1323    x3=xc+uc/2-arrowmoins(1,:);
1324    y1=yc+vc/2-arrowplus(2,:);
1325    y2=yc+vc/2;
1326    y3=yc+vc/2-arrowmoins(2,:);
1327    matxar=[x1(:) x2(:) x3(:) xN(:)]';
1328    matxar=reshape(matxar,1,4*n(2));
1329    matyar=[y1(:) y2(:) y3(:) xN(:)]';
1330    matyar=reshape(matyar,1,4*n(2));
1331    %draw the line or modify the existing ones
[421]1332%     tri=reshape(1:3*length(uc),3,[])';   
[201]1333    isn=isnan(colorlist(icolor,:));%test if color NaN
1334    if 2*icolor > sizh(1) %if icolor exceeds the number of existing ones
1335        if ~isn(1) %if the vectors are visible color not nan
1336            if n(2)>0
1337                hold on
1338                line(matx,maty,'Color',colorlist(icolor,:),'Tag','vel');% plot new lines
1339                line(matxar,matyar,'Color',colorlist(icolor,:),'Tag','vel');% plot arrows
1340            end
1341        end
1342    else
1343        if isn(1)
1344            delete(h(2*icolor-1))
1345            delete(h(2*icolor))
1346        else
1347            set(h(2*icolor-1),'Xdata',matx,'Ydata',maty);
1348            set(h(2*icolor-1),'Color',colorlist(icolor,:));
[932]1349%            set(h(2*icolor-1),'EraseMode','xor');
[201]1350            set(h(2*icolor),'Xdata',matxar,'Ydata',matyar);
1351            set(h(2*icolor),'Color',colorlist(icolor,:));
[932]1352            %set(h(2*icolor),'EraseMode','xor');
[201]1353        end
1354    end
1355end
1356if sizh(1) > 2*ncolor
1357    for icolor=ncolor+1 : sizh(1)/2%delete additional objects
1358        delete(h(2*icolor-1))
1359        delete(h(2*icolor))
1360    end
1361end
1362
1363%-------------------------------------------------------------------
1364% ---- determine tick positions for colorbar
1365function YTick=colbartick(MinA,MaxA)
1366%-------------------------------------------------------------------
1367%determine tick positions with "simple" values between MinA and MaxA
1368YTick=0;%default
1369maxabs=max([abs(MinA) abs(MaxA)]);
1370if maxabs>0
1371ord=10^(floor(log10(maxabs)));%order of magnitude
1372div=1;
1373siz2=1;
1374while siz2<2
1375    values=-10:div:10;
1376    ind=find((ord*values-MaxA)<0 & (ord*values-MinA)>0);%indices of 'values' such that MinA<ord*values<MaxA
1377    siz=size(ind);
1378    if siz(2)<4%if there are less than 4 selected values (4 levels)
1379        values=-9:0.5*div:9;
1380        ind=find((ord*values-MaxA)<0 & (ord*values-MinA)>0);
1381    end
1382    siz2=size(ind,2);
1383    div=div/10;
1384end
1385YTick=ord*values(ind);
1386end
1387
[356]1388% -------------------------------------------------------------------------
[389]1389% --- 'proj_grid': project  fields with unstructured coordinantes on a regular grid
[356]1390function [A,rangx,rangy]=proj_grid(vec_X,vec_Y,vec_A,rgx_in,rgy_in,npxy_in)
[389]1391% -------------------------------------------------------------------------
[356]1392if length(vec_Y)<2
1393    msgbox_uvmat('ERROR','less than 2 points in proj_grid.m');
1394    return;
1395end
1396diffy=diff(vec_Y); %difference dy=vec_Y(i+1)-vec_Y(i)
1397index=find(diffy);% find the indices of vec_Y after wich a change of horizontal line occurs(diffy non zero)
1398if isempty(index); msgbox_uvmat('ERROR','points aligned along abscissa in proj_grid.m'); return; end;%points aligned% A FAIRE: switch to line plot.
1399diff2=diff(diffy(index));% diff2 = fluctuations of the detected vertical grid mesh dy
1400if max(abs(diff2))>0.001*abs(diffy(index(1))) % if max(diff2) is larger than 1/1000 of the first mesh dy
1401    % the data are not regularly spaced and must be interpolated  on a regular grid
1402    if exist('rgx_in','var') & ~isempty (rgx_in) & isnumeric(rgx_in) & length(rgx_in)==2%  positions imposed from input
1403        rangx=rgx_in; % first and last positions
1404        rangy=rgy_in;
1405        dxy(1)=1/(npxy_in(1)-1);%grid mesh in y
1406        dxy(2)=1/(npxy_in(2)-1);%grid mesh in x
1407        dxy(1)=(rangy(2)-rangy(1))/(npxy_in(1)-1);%grid mesh in y
1408        dxy(2)=(rangx(2)-rangx(1))/(npxy_in(2)-1);%grid mesh in x
1409    else % interpolation grid automatically determined
1410        rangx(1)=min(vec_X);
1411        rangx(2)=max(vec_X);
1412        rangy(2)=min(vec_Y);
1413        rangy(1)=max(vec_Y);
1414        dxymod=sqrt((rangx(2)-rangx(1))*(rangy(1)-rangy(2))/length(vec_X));
1415        dxy=[-dxymod/4 dxymod/4];% increase the resolution 4 times
1416    end
[869]1417    xi=rangx(1):dxy(2):rangx(2);
1418    yi=rangy(1):dxy(1):rangy(2);
1419    A=griddata(vec_X,vec_Y,vec_A,xi,yi');
[356]1420    A=reshape(A,length(yi),length(xi));
1421else
1422    x=vec_X(1:index(1));% the set of abscissa (obtained on the first line)
1423    indexend=index(end);% last vector index of line change
1424    ymax=vec_Y(indexend+1);% y coordinate AFTER line change
1425    ymin=vec_Y(index(1));
1426    y=vec_Y(index);
1427    y(length(y)+1)=ymax;
1428    nx=length(x);   %number of grid points in x
1429    ny=length(y);   % number of grid points in y
1430    B=(reshape(vec_A,nx,ny))'; %vec_A reshaped as a rectangular matrix
1431    [X,Y]=meshgrid(x,y);% positions X and Y also reshaped as matrix
1432
1433    %linear interpolation to improve the image resolution and/or adjust
1434    %to prescribed positions
1435    test_interp=1;
1436    if exist('rgx_in','var') & ~isempty (rgx_in) & isnumeric(rgx_in) & length(rgx_in)==2%  positions imposed from input
1437        rangx=rgx_in; % first and last positions
1438        rangy=rgy_in;
1439        npxy=npxy_in;
1440    else       
1441        rangx=[vec_X(1) vec_X(nx)];% first and last position found for x
1442          rangy=[max(ymax,ymin) min(ymax,ymin)];
1443        if max(nx,ny) <= 64 & isequal(npxy_in,'np>256')
1444            npxy=[8*ny 8*nx];% increase the resolution 8 times
1445        elseif max(nx,ny) <= 128 & isequal(npxy_in,'np>256')
1446            npxy=[4*ny 4*nx];% increase the resolution 4 times
1447        elseif max(nx,ny) <= 256 & isequal(npxy_in,'np>256')
1448            npxy=[2*ny 2*nx];% increase the resolution 2 times
1449        else
1450            npxy=[ny nx];
1451            test_interp=0; % no interpolation done
1452        end
1453    end
1454    if test_interp==1%if we interpolate
1455        xi=[rangx(1):(rangx(2)-rangx(1))/(npxy(2)-1):rangx(2)];
1456        yi=[rangy(1):(rangy(2)-rangy(1))/(npxy(1)-1):rangy(2)];
1457        [XI,YI]=meshgrid(xi,yi);
1458        A = interp2(X,Y,B,XI,YI);
1459    else %no interpolation for a resolution higher than 256
1460        A=B;
1461    end
[801]1462end
Note: See TracBrowser for help on using the repository browser.