source: trunk/src/read_xls.m @ 965

Last change on this file since 965 was 924, checked in by g7moreau, 8 years ago
  • Update Copyright Copyright 2008-2016, LEGI UMR 5519 / CNRS UGA G-INP, Grenoble, France
File size: 17.0 KB
Line 
1%'read_xls': function for reading and displaying Excel files 
2%------------------------------------------------------------------------
3% function [hfig_xls]=read_xls(fileinput,hfig)
4%
5% OUTPUT:
6%  hfig_xls: figure handle for display
7%
8% INPUT:
9% fileinput: name of the input file (char string)
10% hfig: handle of the display figure (a new display figure hfig_xls is created if hfig undefined)
11
12%=======================================================================
13% Copyright 2008-2016, LEGI UMR 5519 / CNRS UGA G-INP, Grenoble, France
14%   http://www.legi.grenoble-inp.fr
15%   Joel.Sommeria - Joel.Sommeria (A) legi.cnrs.fr
16%
17%     This file is part of the toolbox UVMAT.
18%
19%     UVMAT is free software; you can redistribute it and/or modify
20%     it under the terms of the GNU General Public License as published
21%     by the Free Software Foundation; either version 2 of the license,
22%     or (at your option) any later version.
23%
24%     UVMAT is distributed in the hope that it will be useful,
25%     but WITHOUT ANY WARRANTY; without even the implied warranty of
26%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27%     GNU General Public License (see LICENSE.txt) for more details.
28%=======================================================================
29
30function [hfig_xls]=read_xls(fileinput,hfig)
31[Tabnum,Tabtext]=xlsread(fileinput);
32    [textnx,textny]=size(Tabtext);
33    [numnx,numny]=size(Tabnum);
34    ilastxt=textnx-numnx;%index of last text row
35    jlastxt=textny-numny;%index of last text column
36    for jtab=1:textny%read line
37        for itab=1:textnx% read column
38            textlu=cell2mat(Tabtext(itab,jtab));
39            if isequal(textlu,[])& itab > ilastxt & jtab > jlastxt %replace txt by number
40                textlu=num2str(Tabnum(itab-ilastxt,jtab-jlastxt));
41            end
42            Tabdisplay(itab,jtab)={textlu};
43            lengthtext(itab)=length(textlu);
44        end
45        widthcolumn(jtab)=max(lengthtext);
46    end
47    Tabchar={};%default
48    for itab=1:textnx    %justify table
49        charchain=[];         
50        for jtab=1:textny% read line
51            textlu=Tabdisplay{itab,jtab};
52            if widthcolumn(jtab)>length(textlu)
53                blankstr=char(46*ones(1,widthcolumn(jtab)-length(textlu)));
54                textlu=[textlu blankstr ];
55            end
56            charchain=[charchain textlu char(9) ' | '];
57        end
58        Tabchar(itab)={charchain};
59    end
60    if exist('hfig','var') & ishandle(hfig)
61        figure(hfig);
62        hfig_xls=hfig;
63    else
64        hfig_xls=figure;
65    end
66    set(hfig_xls,'Name',fileinput)
67    set(hfig_xls,'MenuBar','none')
68    hpos=get(hfig_xls,'Pos');
69    ExpName.cell=Tabtext([2:textnx],1);%first column (dir name)
70    ExpName.Num=Tabnum;
71%     ExpName.Units=Tabtext(2,[2:textny]);%look for the units line (needs to be the second line)
72    iparam=0;
73    for icol=2:textny
74%         Tabtext(2,icol)
75        if ~isempty(Tabtext{2,icol})&~isequal(Tabtext{2,icol},'')
76            iparam=iparam+1;
77            ExpName.Param{iparam}=Tabtext{1,icol};
78            ExpName.Units{iparam}=Tabtext{2,icol};
79            ExpName.Column(iparam)=icol;
80        end
81    end
82 
83    ExpName.path=fileparts(fileinput);
84    h=uicontrol('Style','listbox', 'Position', [5 5 0.9*hpos(3) 0.9*hpos(4)], 'String', Tabchar, ...
85        'FontName','Monospaced','Callback',@link2file,'UserData',ExpName,'Tag','listbox'); 
86%     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);
87%      set(h,'HorizontalAlignment','left')
88%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
89%called by xlsdisplay to navigate from  a .xls file or create the
90% the experiment directories
91function link2file(obj,event,fileinput)
92global t
93bla=get(gcbo,'String');
94ind=get(gcbo,'Value')
95if (ind==1|ind==2),return,end; %no action on the first line
96ExpNameStruct=get(gcbo,'UserData')
97ExpName=ExpNameStruct.cell{ind-1}
98ProjectFullName=ExpNameStruct.path;%full name of the project (including path)
99[Pth,ProjectName]=fileparts(ProjectFullName);
100ExpPath=fullfile(ProjectFullName,ExpName);% full name of the experiment directory
101ExpDocName=fullfile(ExpPath,[ExpName '.xml']);% full name of the .xml file ExpDoc
102if exist(ExpDocName,'file')
103    hh=editxml({ExpDocName})   
104%     [FileName, PathName, filterindex] = uigetfile( ...
105%        {'*.xml','(*.xml)';
106%        '*.xml',  '.xml files ';
107%         '*.*',  'All Files (*.*)'}, ...
108%         'Pick a file',ExpDocName);
109%      fileinput=[PathName FileName];%complete file name
110%      sizf=size(fileinput);
111%     if (~ischar(fileinput)|~isequal(sizf(1),1)),return;end
112%         [path,name,ext]=fileparts(fileinput);
113%     if isequal(ext,'.civ') | isequal(ext,'.log') | isequal(ext,'.cmx') isequal(ext,'.txt')
114%         edit(fileinput)
115%     elseif isequal(ext,'.xml')
116%         varargin{1}=fileinput;
117%         editxml(varargin)
118%     elseif isequal(ext,'.fig')
119%         open(fileinput)
120%     elseif isequal(ext,'.xls')
121%         xlsdisplay(fileinput)
122%     else
123%         uvmat({fileinput})
124%     end
125else
126    answer=questdlg({['ExpDoc file ' ExpDocName ' does not exist, create the experiment?'];''})
127    if isequal(answer,'Yes')
128        if exist(ExpPath,'dir')~=7 %create a directory if it does not exist
129            dircur=pwd; %current working directory
130            cd(ProjectFullName);
131            [m1,m2,m3]=mkdir(ExpName);
132            cd(pwd);%come back to the initial working dir
133        end
134%         %copy exp parameters
135        ParamNames=ExpNameStruct.Param;
136        ParamValues=ExpNameStruct.Num(ind-1,ExpNameStruct.Column-1);
137        ParamUnits=ExpNameStruct.Units;
138        t=xmltree;%new xmltree
139        t=set(t,1,'name','ExpDoc');
140        t=attributes(t,'add',1,'xmlns:xsi','none');
141        [t,ExpElement]=add(t,1,'element','Exp');
142        [t]=add(t,ExpElement,'chardata',ExpName);
143        for iparam=1:length(ParamNames)
144            [t,ParamElement]=add(t,1,'element',ParamNames{iparam});
145            t=add(t,ParamElement,'chardata',num2str(ParamValues(iparam)));
146            t=attributes(t,'add',ParamElement,'unit',ParamUnits{iparam}); %ADD UNIT ATTRIBUTE
147        end
148        list_dir=dir(ExpPath);%list of the Exp directory,  detect sub-directories,.xml and image files
149        nbdir_exp=0;
150        %scan the Exp directory
151        for idir_exp=3:length(list_dir)
152            %detect subdirectories 
153            if list_dir(idir_exp).isdir% 'device' subdirectories
154                nbdir_exp=nbdir_exp+1;
155                ExpData.Device{nbdir_exp}=list_dir(idir_exp).name;
156                [t,DeviceElement]=add(t,1,'element',list_dir(idir_exp).name);
157                t=attributes(t,'add',DeviceElement,'type','DEVICE_DIR');
158                t=attributes(t,'add',DeviceElement,'source','dir');
159                list_subdir=dir(fullfile(ExpPath,list_dir(idir_exp).name));
160                nbsubdir=0;
161                testrecord=1;
162                RootIma='';
163                RootNc='';
164                nbfile=0;
165%                 nbfile={};
166                %scan the Device subdirectory
167                for isubdir=3:length(list_subdir)
168                    if list_subdir(isubdir).isdir
169                        nbsubdir=nbsubdir+1;
170                        Device.Record{nbsubdir}=list_subdir(isubdir).name;
171                    else
172                        nbfile=nbfile+1;
173                        fname{nbfile}=list_subdir(isubdir).name;
174                    end
175                end
176                if isunix%sort by root names and indices , change the separator '_' so that 1_1 come as the first name
177                    fname_mod=regexprep(fname,'_','/');
178                    fname_mod=sort(fname_mod);     %sort by name
179                    fname=regexprep(fname_mod,'/','_');
180                end
181                for ifile=1:nbfile;
182                    [Path,Name,Ext]=fileparts(fname{ifile});
183                    if isequal(Ext,'.xml')
184                       [t,ImaDocElement]=add(t,DeviceElement,'element','ImaDoc');
185                       t=add(t,ImaDocElement,'chardata',fname{ifile});
186                       t=attributes(t,'add',ImaDocElement,'source','file');
187                       testrecord=0;%we have an image series without 'record' subdir
188                    elseif isequal(Ext,'.png')
189                       %[Path,Root,field_count,str2,str_a,str_b,ext,nom_type,subdir]=name2display(fname{ifile});
190                       [~,~,Root]=fileparts_uvmat(fname{ifile});
191                       if ~isequal(Root,RootIma)%only one image recorded for each root name
192                           [t,ImaDocElement]=add(t,DeviceElement,'element','Image');
193                           t=add(t,ImaDocElement,'chardata',fname{ifile});
194                           t=attributes(t,'add',ImaDocElement,'source','file');
195                           RootIma=Root;
196                       end
197                       testrecord=0;%we have an image series without 'record' subdir
198                    elseif isequal(Ext,'.nc')
199                       %[Path,Root,field_count,str2,str_a,str_b,ext,nom_type,subdir]=name2display(fname{ifile});
200                       [~,~,Root]=fileparts_uvmat(fname{ifile});
201                       if ~isequal(Root,RootNc)%only one image recorded for each root name
202                           [t,ImaDocElement]=add(t,DeviceElement,'element','Ncdata');
203                           t=add(t,ImaDocElement,'chardata',fname{ifile});
204                           t=attributes(t,'add',ImaDocElement,'source','file');
205                           RootNc=Root;
206                       end
207                       testrecord=0;%we have an image series without 'record' subdir
208                    end
209                end
210                if testrecord
211                    %the subdevice directory is 'record' (no images detected at this level)
212                    for idir_s=1:nbsubdir
213                        [t,RecordElement]=add(t,DeviceElement,'element',Device.Record{idir_s});
214                        t=attributes(t,'add',RecordElement,'type','RECORD_DIR');
215                        t=attributes(t,'add',RecordElement,'source','dir');
216                        list_subdir=dir(fullfile(ExpPath,list_dir(idir_exp).name,Device.Record{idir_s}));
217                        nbsubdir=0;
218                        RootIma='';
219                        RootNc='';
220                        nbfile=0;
221                        fname={};
222                        for isubdir=3:length(list_subdir)
223                            if list_subdir(isubdir).isdir
224                                nbsubdir=nbsubdir+1;
225                                [t,RecordElement]=add(t,DeviceElement,'element',Device.Record{idir_exp});
226                                t=attributes(t,'add',RecordElement,'type','RECORD_DIR');
227                                t=attributes(t,'add',RecordElement,'source','dir');
228                                %VOIR les .netcdf a l'interieur
229                            else
230                                nbfile=nbfile+1;
231                                fname{nbfile}=list_subdir(isubdir).name;
232                            end
233                        end
234                        if isunix
235                            fname_mod=regexprep(fname,'_','/');
236                            fname_mod=sort(fname_mod);     %sort by name
237                            fname=regexprep(fname_mod,'/','_');
238                        end
239                        for ifile=1:nbfile;           
240                            [Path,Name,Ext]=fileparts(fname{ifile});
241                            if isequal(Ext,'.xml')
242                               [t,ImaDocElement]=add(t,DeviceElement,'element','ImaDoc');
243                               t=add(t,ImaDocElement,'chardata',fname{ifile});
244                               t=attributes(t,'add',ImaDocElement,'source','file');
245                            elseif isequal(Ext,'.png')
246                              % [Path,Root,field_count,str2,str_a,str_b,ext,nom_type,subdir]=name2display(fname{ifile});
247                                [~,~,Root]=fileparts_uvmat(fname{ifile});
248                               if ~isequal(Root,RootIma)
249                                   [t,ImaDocElement]=add(t,DeviceElement,'element','Image');
250                                   t=add(t,ImaDocElement,'chardata',fname{ifile});
251                                   t=attributes(t,'add',ImaDocElement,'source','file');
252                                   RootIma=Root;
253                               end
254                            elseif isequal(Ext,'.nc')
255                               %[Path,Root,field_count,str2,str_a,str_b,ext,nom_type,subdir]=name2display(fname{ifile});
256                               [~,~,Root]=fileparts_uvmat(fname{ifile});
257                               if ~isequal(Root,RootNc)%only one image recorded for each root name
258                                  [t,ImaDocElement]=add(t,DeviceElement,'element','Ncdata');
259                                  t=add(t,ImaDocElement,'chardata',fname{ifile});
260                                  t=attributes(t,'add',ImaDocElement,'source','file');
261                                  RootNc=Root;
262                               end
263                            end
264                        end
265                    end
266                else%the subdevice directory is a civ directory (coexist with images)
267                     for idir_s=1:nbsubdir
268                        [t,RecordElement]=add(t,DeviceElement,'element',Device.Record{idir_s});
269                        t=attributes(t,'add',RecordElement,'type','CIV_DIR');
270                        t=attributes(t,'add',RecordElement,'source','dir');
271                        %list files under the civ directory
272                        list_subdir=dir(fullfile(ExpPath,list_dir(idir_exp).name,Device.Record{idir_s}));
273                                       
274                        nbsubdir=0;
275                        nbfile=0;
276                        RootXml='';
277                        RootNc='';       
278                        fname={};
279                        for isubdir=3:length(list_subdir)
280                            if list_subdir(isubdir).isdir
281                                nbsubdir=nbsubdir+1;
282                                [t,SubElement]=add(t,RecordElement,'element',list_subdir(isubdir).name);
283                                t=attributes(t,'add',SubElement,'type','UNKNOWN_DIR');
284                                t=attributes(t,'add',SubElement,'source','dir');
285                            else
286                                nbfile=nbfile+1;
287                                fname{nbfile}=list_subdir(isubdir).name;
288                            end
289                        end
290                        if isunix
291                            fname_mod=regexprep(fname,'_','/');
292                            fname_mod=sort(fname_mod);     %sort by name
293                            fname=regexprep(fname_mod,'/','_');
294                        end
295                        for ifile=1:nbfile;
296                            [Path,Name,Ext]=fileparts(fname{ifile});
297                            if isequal(Ext,'.xml')
298                               %[Path,Root,field_count,str2,str_a,str_b,ext,nom_type,subdir]=name2display(fname{ifile});
299                               [~,~,Root]=fileparts_uvmat(fname{ifile});
300                               if ~isequal(Root,RootXml)%only one image recorded for each root name
301                                   [t,ImaDocElement]=add(t,RecordElement,'element','CivDoc');
302                                   t=add(t,ImaDocElement,'chardata',fname{ifile});
303                                   t=attributes(t,'add',ImaDocElement,'source','file');
304                                   RootXml=Root;
305                               end
306                            elseif isequal(Ext,'.nc')
307                               %[Path,Root,field_count,str2,str_a,str_b,ext,nom_type,subdir]=name2display(fname{ifile});
308                               [~,~,Root]=fileparts_uvmat(fname{ifile});
309                               if ~isequal(Root,RootNc)%only one image recorded for each root name
310                                  [t,ImaDocElement]=add(t,RecordElement,'element','Ncdata');
311                                  t=add(t,ImaDocElement,'chardata',fname{ifile});
312                                  t=attributes(t,'add',ImaDocElement,'source','file');
313                                  RootNc=Root;
314                               end
315                            end
316                        end
317                    end
318                end
319            end
320        end
321        save(t);%display xml file on the screen
322        save(t,ExpDocName);
323    end
324end
325% [erread,message]=fileattrib('./DATA');
326% if ~isempty(message) & ~isequal(message.UserWrite,1)
327%      errordlg(['Need writting access to ' message.Name])
328%      return
329% end
330
331
332    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
333    function project_update(obj,event,fileinput)
334    hchild=get(gcbf,'children');
335    h=findobj(gcbf,'Tag','listbox')
336   
Note: See TracBrowser for help on using the repository browser.