Home > . > read_xls.m

# read_xls

## PURPOSE

'read_xls': function for reading and displaying Excel files

## SYNOPSIS

function [hfig_xls]=read_xls(fileinput,hfig)

## DESCRIPTION

    'read_xls': function for reading and displaying Excel files
    ------------------------------------------------------------------------
     function [hfig_xls]=read_xls(fileinput,hfig)

     OUTPUT:
      hfig_xls: figure handle for display

     INPUT:
     fileinput: name of the input file (char string)
     hfig: handle of the display figure (a new display figure hfig_xls is created if hfig undefined)

    AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
      Copyright Joel Sommeria, 2008, LEGI / CNRS-UJF-INPG, sommeria@coriolis-legi.org.
    AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
         This file is part of the toolbox UVMAT.

         UVMAT is free software; you can redistribute it and/or modify
         it under the terms of the GNU General Public License as published by
         the Free Software Foundation; either version 2 of the License, or
         (at your option) any later version.

         UVMAT is distributed in the hope that it will be useful,
         but WITHOUT ANY WARRANTY; without even the implied warranty of
         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
         GNU General Public License (file UVMAT/COPYING.txt) for more details.
    AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA

## CROSS-REFERENCE INFORMATION

This function calls:

- editxml 'editxml': function for editing xml files using a xml schema (associated with the GUI editxml.fig)
- name2display 'name2display': extracts the root name and field numbers from an input filename

This function is called by:

- editxml 'editxml': function for editing xml files using a xml schema (associated with the GUI editxml.fig)

## SUBFUNCTIONS

- function link2file(obj,event,fileinput)
- function project_update(obj,event,fileinput)

## SOURCE CODE

    0001 %'read_xls': function for reading and displaying Excel files
    0002 %------------------------------------------------------------------------
    0003 % function [hfig_xls]=read_xls(fileinput,hfig)
    0004 %
    0005 % OUTPUT:
    0006 %  hfig_xls: figure handle for display
    0007 %
    0008 % INPUT:
    0009 % fileinput: name of the input file (char string)
    0010 % hfig: handle of the display figure (a new display figure hfig_xls is created if hfig undefined)
    0011 %
    0012 %AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
    0013 %  Copyright Joel Sommeria, 2008, LEGI / CNRS-UJF-INPG, sommeria@coriolis-legi.org.
    0014 %AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
    0015 %     This file is part of the toolbox UVMAT.
    0016 %
    0017 %     UVMAT is free software; you can redistribute it and/or modify
    0018 %     it under the terms of the GNU General Public License as published by
    0019 %     the Free Software Foundation; either version 2 of the License, or
    0020 %     (at your option) any later version.
    0021 %
    0022 %     UVMAT is distributed in the hope that it will be useful,
    0023 %     but WITHOUT ANY WARRANTY; without even the implied warranty of
    0024 %     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    0025 %     GNU General Public License (file UVMAT/COPYING.txt) for more details.
    0026 %AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
    0027
    0028 function [hfig_xls]=read_xls(fileinput,hfig)
    0029 [Tabnum,Tabtext]=xlsread(fileinput);
    0030     [textnx,textny]=size(Tabtext);
    0031     [numnx,numny]=size(Tabnum);
    0032     ilastxt=textnx-numnx;%index of last text row
    0033     jlastxt=textny-numny;%index of last text column
    0034     for jtab=1:textny%read line
    0035         for itab=1:textnx% read column
    0036             textlu=cell2mat(Tabtext(itab,jtab));
    0037             if isequal(textlu,[])& itab > ilastxt & jtab > jlastxt %replace txt by number
    0038                 textlu=num2str(Tabnum(itab-ilastxt,jtab-jlastxt));
    0039             end
    0040             Tabdisplay(itab,jtab)={textlu};
    0041             lengthtext(itab)=length(textlu);
    0042         end
    0043         widthcolumn(jtab)=max(lengthtext);
    0044     end
    0045     Tabchar={};%default
    0046     for itab=1:textnx    %justify table
    0047         charchain=[];
    0048         for jtab=1:textny% read line
    0049             textlu=Tabdisplay{itab,jtab};
    0050             if widthcolumn(jtab)>length(textlu)
    0051                 blankstr=char(46*ones(1,widthcolumn(jtab)-length(textlu)));
    0052                 textlu=[textlu blankstr ];
    0053             end
    0054             charchain=[charchain textlu char(9) ' | '];
    0055         end
    0056         Tabchar(itab)={charchain};
    0057     end
    0058     if exist('hfig','var') & ishandle(hfig)
    0059         figure(hfig);
    0060         hfig_xls=hfig;
    0061     else
    0062         hfig_xls=figure;
    0063     end
    0064     set(hfig_xls,'Name',fileinput)
    0065     set(hfig_xls,'MenuBar','none')
    0066     hpos=get(hfig_xls,'Pos');
    0067     ExpName.cell=Tabtext([2:textnx],1);%first column (dir name)
    0068     ExpName.Num=Tabnum;
    0069 %     ExpName.Units=Tabtext(2,[2:textny]);%look for the units line (needs to be the second line)
    0070     iparam=0;
    0071     for icol=2:textny
    0072 %         Tabtext(2,icol)
    0073         if ~isempty(Tabtext{2,icol})&~isequal(Tabtext{2,icol},'')
    0074             iparam=iparam+1;
    0075             ExpName.Param{iparam}=Tabtext{1,icol};
    0076             ExpName.Units{iparam}=Tabtext{2,icol};
    0077             ExpName.Column(iparam)=icol;
    0078         end
    0079     end
    0080
    0081     ExpName.path=fileparts(fileinput);
    0082     h=uicontrol('Style','listbox', 'Position', [5 5 0.9*hpos(3) 0.9*hpos(4)], 'String', Tabchar, ...
    0083         'FontName','Monospaced','Callback',@link2file,'UserData',ExpName,'Tag','listbox');
    0084 %     hh=uicontrol('Style','Pushbutton', 'Position', [0.93*hpos(1) 0.93*hpos(2) 0.05*hpos(3) 0.05*hpos(4)], 'String', 'Update','Callback',@project_update);
    0085 %      set(h,'HorizontalAlignment','left')
    0086 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    0087 %called by xlsdisplay to navigate from  a .xls file or create the
    0088 % the experiment directories
    0089 function link2file(obj,event,fileinput)
    0090 global t
    0091 bla=get(gcbo,'String');
    0092 ind=get(gcbo,'Value')
    0093 if (ind==1|ind==2),return,end; %no action on the first line
    0094 ExpNameStruct=get(gcbo,'UserData')
    0095 ExpName=ExpNameStruct.cell{ind-1}
    0096 ProjectFullName=ExpNameStruct.path;%full name of the project (including path)
    0097 [Pth,ProjectName]=fileparts(ProjectFullName);
    0098 ExpPath=fullfile(ProjectFullName,ExpName);% full name of the experiment directory
    0099 ExpDocName=fullfile(ExpPath,[ExpName '.xml']);% full name of the .xml file ExpDoc
    0100 if exist(ExpDocName,'file')
    0101     hh=editxml({ExpDocName})
    0102 %     [FileName, PathName, filterindex] = uigetfile( ...
    0103 %        {'*.xml','(*.xml)';
    0104 %        '*.xml',  '.xml files ';
    0105 %         '*.*',  'All Files (*.*)'}, ...
    0106 %         'Pick a file',ExpDocName);
    0107 %      fileinput=[PathName FileName];%complete file name
    0108 %      sizf=size(fileinput);
    0109 %     if (~ischar(fileinput)|~isequal(sizf(1),1)),return;end
    0110 %         [path,name,ext]=fileparts(fileinput);
    0111 %     if isequal(ext,'.civ') | isequal(ext,'.log') | isequal(ext,'.cmx') isequal(ext,'.txt')
    0112 %         edit(fileinput)
    0113 %     elseif isequal(ext,'.xml')
    0114 %         varargin{1}=fileinput;
    0115 %         editxml(varargin)
    0116 %     elseif isequal(ext,'.fig')
    0117 %         open(fileinput)
    0118 %     elseif isequal(ext,'.xls')
    0119 %         xlsdisplay(fileinput)
    0120 %     else
    0121 %         uvmat({fileinput})
    0122 %     end
    0123 else
    0124     answer=questdlg({['ExpDoc file ' ExpDocName ' does not exist, create the experiment?'];''})
    0125     if isequal(answer,'Yes')
    0126         if exist(ExpPath,'dir')~=7 %create a directory if it does not exist
    0127             dircur=pwd; %current working directory
    0128             cd(ProjectFullName);
    0129             [m1,m2,m3]=mkdir(ExpName);
    0130             cd(pwd);%come back to the initial working dir
    0131         end
    0132 %         %copy exp parameters
    0133         ParamNames=ExpNameStruct.Param;
    0134         ParamValues=ExpNameStruct.Num(ind-1,ExpNameStruct.Column-1);
    0135         ParamUnits=ExpNameStruct.Units;
    0136         t=xmltree;%new xmltree
    0137         t=set(t,1,'name','ExpDoc');
    0138         t=attributes(t,'add',1,'xmlns:xsi','none');
    0139         [t,ExpElement]=add(t,1,'element','Exp');
    0140         [t]=add(t,ExpElement,'chardata',ExpName);
    0141         for iparam=1:length(ParamNames)
    0142             [t,ParamElement]=add(t,1,'element',ParamNames{iparam});
    0143             t=add(t,ParamElement,'chardata',num2str(ParamValues(iparam)));
    0144             t=attributes(t,'add',ParamElement,'unit',ParamUnits{iparam}); %ADD UNIT ATTRIBUTE
    0145         end
    0146         list_dir=dir(ExpPath);%list of the Exp directory,  detect sub-directories,.xml and image files
    0147         nbdir_exp=0;
    0148         %scan the Exp directory
    0149         for idir_exp=3:length(list_dir)
    0150             %detect subdirectories
    0151             if list_dir(idir_exp).isdir% 'device' subdirectories
    0152                 nbdir_exp=nbdir_exp+1;
    0153                 ExpData.Device{nbdir_exp}=list_dir(idir_exp).name;
    0154                 [t,DeviceElement]=add(t,1,'element',list_dir(idir_exp).name);
    0155                 t=attributes(t,'add',DeviceElement,'type','DEVICE_DIR');
    0156                 t=attributes(t,'add',DeviceElement,'source','dir');
    0157                 list_subdir=dir(fullfile(ExpPath,list_dir(idir_exp).name));
    0158                 nbsubdir=0;
    0159                 testrecord=1;
    0160                 RootIma='';
    0161                 RootNc='';
    0162                 nbfile=0;
    0163 %                 nbfile={};
    0164                 %scan the Device subdirectory
    0165                 for isubdir=3:length(list_subdir)
    0166                     if list_subdir(isubdir).isdir
    0167                         nbsubdir=nbsubdir+1;
    0168                         Device.Record{nbsubdir}=list_subdir(isubdir).name;
    0169                     else
    0170                         nbfile=nbfile+1;
    0171                         fname{nbfile}=list_subdir(isubdir).name;
    0172                     end
    0173                 end
    0174                 if isunix%sort by root names and indices , change the separator '_' so that 1_1 come as the first name
    0175                     fname_mod=regexprep(fname,'_','/');
    0176                     fname_mod=sort(fname_mod);     %sort by name
    0177                     fname=regexprep(fname_mod,'/','_');
    0178                 end
    0179                 for ifile=1:nbfile;
    0180                     [Path,Name,Ext]=fileparts(fname{ifile});
    0181                     if isequal(Ext,'.xml')
    0182                        [t,ImaDocElement]=add(t,DeviceElement,'element','ImaDoc');
    0183                        t=add(t,ImaDocElement,'chardata',fname{ifile});
    0184                        t=attributes(t,'add',ImaDocElement,'source','file');
    0185                        testrecord=0;%we have an image series without 'record' subdir
    0186                     elseif isequal(Ext,'.png')
    0187                        [Path,Root,field_count,str2,str_a,str_b,ext,nom_type,subdir]=name2display(fname{ifile});
    0188                        if ~isequal(Root,RootIma)%only one image recorded for each root name
    0189                            [t,ImaDocElement]=add(t,DeviceElement,'element','Image');
    0190                            t=add(t,ImaDocElement,'chardata',fname{ifile});
    0191                            t=attributes(t,'add',ImaDocElement,'source','file');
    0192                            RootIma=Root;
    0193                        end
    0194                        testrecord=0;%we have an image series without 'record' subdir
    0195                     elseif isequal(Ext,'.nc')
    0196                        [Path,Root,field_count,str2,str_a,str_b,ext,nom_type,subdir]=name2display(fname{ifile});
    0197                        if ~isequal(Root,RootNc)%only one image recorded for each root name
    0198                            [t,ImaDocElement]=add(t,DeviceElement,'element','Ncdata');
    0199                            t=add(t,ImaDocElement,'chardata',fname{ifile});
    0200                            t=attributes(t,'add',ImaDocElement,'source','file');
    0201                            RootNc=Root;
    0202                        end
    0203                        testrecord=0;%we have an image series without 'record' subdir
    0204                     end
    0205                 end
    0206                 if testrecord
    0207                     %the subdevice directory is 'record' (no images detected at this level)
    0208                     for idir_s=1:nbsubdir
    0209                         [t,RecordElement]=add(t,DeviceElement,'element',Device.Record{idir_s});
    0210                         t=attributes(t,'add',RecordElement,'type','RECORD_DIR');
    0211                         t=attributes(t,'add',RecordElement,'source','dir');
    0212                         list_subdir=dir(fullfile(ExpPath,list_dir(idir_exp).name,Device.Record{idir_s}));
    0213                         nbsubdir=0;
    0214                         RootIma='';
    0215                         RootNc='';
    0216                         nbfile=0;
    0217                         fname={};
    0218                         for isubdir=3:length(list_subdir)
    0219                             if list_subdir(isubdir).isdir
    0220                                 nbsubdir=nbsubdir+1;
    0221                                 [t,RecordElement]=add(t,DeviceElement,'element',Device.Record{idir_exp});
    0222                                 t=attributes(t,'add',RecordElement,'type','RECORD_DIR');
    0223                                 t=attributes(t,'add',RecordElement,'source','dir');
    0224                                 %VOIR les .netcdf a l'interieur
    0225                             else
    0226                                 nbfile=nbfile+1;
    0227                                 fname{nbfile}=list_subdir(isubdir).name;
    0228                             end
    0229                         end
    0230                         if isunix
    0231                             fname_mod=regexprep(fname,'_','/');
    0232                             fname_mod=sort(fname_mod);     %sort by name
    0233                             fname=regexprep(fname_mod,'/','_');
    0234                         end
    0235                         for ifile=1:nbfile;
    0236                             [Path,Name,Ext]=fileparts(fname{ifile});
    0237                             if isequal(Ext,'.xml')
    0238                                [t,ImaDocElement]=add(t,DeviceElement,'element','ImaDoc');
    0239                                t=add(t,ImaDocElement,'chardata',fname{ifile});
    0240                                t=attributes(t,'add',ImaDocElement,'source','file');
    0241                             elseif isequal(Ext,'.png')
    0242                                [Path,Root,field_count,str2,str_a,str_b,ext,nom_type,subdir]=name2display(fname{ifile});
    0243                                if ~isequal(Root,RootIma)
    0244                                    [t,ImaDocElement]=add(t,DeviceElement,'element','Image');
    0245                                    t=add(t,ImaDocElement,'chardata',fname{ifile});
    0246                                    t=attributes(t,'add',ImaDocElement,'source','file');
    0247                                    RootIma=Root;
    0248                                end
    0249                             elseif isequal(Ext,'.nc')
    0250                                [Path,Root,field_count,str2,str_a,str_b,ext,nom_type,subdir]=name2display(fname{ifile});
    0251                                if ~isequal(Root,RootNc)%only one image recorded for each root name
    0252                                   [t,ImaDocElement]=add(t,DeviceElement,'element','Ncdata');
    0253                                   t=add(t,ImaDocElement,'chardata',fname{ifile});
    0254                                   t=attributes(t,'add',ImaDocElement,'source','file');
    0255                                   RootNc=Root;
    0256                                end
    0257                             end
    0258                         end
    0259                     end
    0260                 else%the subdevice directory is a civ directory (coexist with images)
    0261                      for idir_s=1:nbsubdir
    0262                         [t,RecordElement]=add(t,DeviceElement,'element',Device.Record{idir_s});
    0263                         t=attributes(t,'add',RecordElement,'type','CIV_DIR');
    0264                         t=attributes(t,'add',RecordElement,'source','dir');
    0265                         %list files under the civ directory
    0266                         list_subdir=dir(fullfile(ExpPath,list_dir(idir_exp).name,Device.Record{idir_s}));
    0267
    0268                         nbsubdir=0;
    0269                         nbfile=0;
    0270                         RootXml='';
    0271                         RootNc='';
    0272                         fname={};
    0273                         for isubdir=3:length(list_subdir)
    0274                             if list_subdir(isubdir).isdir
    0275                                 nbsubdir=nbsubdir+1;
    0276                                 [t,SubElement]=add(t,RecordElement,'element',list_subdir(isubdir).name);
    0277                                 t=attributes(t,'add',SubElement,'type','UNKNOWN_DIR');
    0278                                 t=attributes(t,'add',SubElement,'source','dir');
    0279                             else
    0280                                 nbfile=nbfile+1;
    0281                                 fname{nbfile}=list_subdir(isubdir).name;
    0282                             end
    0283                         end
    0284                         if isunix
    0285                             fname_mod=regexprep(fname,'_','/');
    0286                             fname_mod=sort(fname_mod);     %sort by name
    0287                             fname=regexprep(fname_mod,'/','_');
    0288                         end
    0289                         for ifile=1:nbfile;
    0290                             [Path,Name,Ext]=fileparts(fname{ifile});
    0291                             if isequal(Ext,'.xml')
    0292                                [Path,Root,field_count,str2,str_a,str_b,ext,nom_type,subdir]=name2display(fname{ifile});
    0293                                if ~isequal(Root,RootXml)%only one image recorded for each root name
    0294                                    [t,ImaDocElement]=add(t,RecordElement,'element','CivDoc');
    0295                                    t=add(t,ImaDocElement,'chardata',fname{ifile});
    0296                                    t=attributes(t,'add',ImaDocElement,'source','file');
    0297                                    RootXml=Root;
    0298                                end
    0299                             elseif isequal(Ext,'.nc')
    0300                                [Path,Root,field_count,str2,str_a,str_b,ext,nom_type,subdir]=name2display(fname{ifile});
    0301                                if ~isequal(Root,RootNc)%only one image recorded for each root name
    0302                                   [t,ImaDocElement]=add(t,RecordElement,'element','Ncdata');
    0303                                   t=add(t,ImaDocElement,'chardata',fname{ifile});
    0304                                   t=attributes(t,'add',ImaDocElement,'source','file');
    0305                                   RootNc=Root;
    0306                                end
    0307                             end
    0308                         end
    0309                     end
    0310                 end
    0311             end
    0312         end
    0313         save(t);%display xml file on the screen
    0314         save(t,ExpDocName);
    0315     end
    0316 end
    0317 % [erread,message]=fileattrib('./DATA');
    0318 % if ~isempty(message) & ~isequal(message.UserWrite,1)
    0319 %      errordlg(['Need writting access to ' message.Name])
    0320 %      return
    0321 % end
    0322
    0323
    0324     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    0325     function project_update(obj,event,fileinput)
    0326     hchild=get(gcbf,'children');
    0327     h=findobj(gcbf,'Tag','listbox')
    0328