source: trunk/src/read_xls.m @ 1080

Last change on this file since 1080 was 1071, checked in by g7moreau, 4 years ago
  • Update COPYRIGHT
File size: 16.2 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-2020, 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});   
104else
105    answer=questdlg({['ExpDoc file ' ExpDocName ' does not exist, create the experiment?'];''})
106    if isequal(answer,'Yes')
107        if exist(ExpPath,'dir')~=7 %create a directory if it does not exist
108            dircur=pwd; %current working directory
109            cd(ProjectFullName);
110            [m1,m2,m3]=mkdir(ExpName);
111            cd(pwd);%come back to the initial working dir
112        end
113%         %copy exp parameters
114        ParamNames=ExpNameStruct.Param;
115        ParamValues=ExpNameStruct.Num(ind-1,ExpNameStruct.Column-1);
116        ParamUnits=ExpNameStruct.Units;
117        t=xmltree;%new xmltree
118        t=set(t,1,'name','ExpDoc');
119        t=attributes(t,'add',1,'xmlns:xsi','none');
120        [t,ExpElement]=add(t,1,'element','Exp');
121        [t]=add(t,ExpElement,'chardata',ExpName);
122        for iparam=1:length(ParamNames)
123            [t,ParamElement]=add(t,1,'element',ParamNames{iparam});
124            t=add(t,ParamElement,'chardata',num2str(ParamValues(iparam)));
125            t=attributes(t,'add',ParamElement,'unit',ParamUnits{iparam}); %ADD UNIT ATTRIBUTE
126        end
127        list_dir=dir(ExpPath);%list of the Exp directory,  detect sub-directories,.xml and image files
128        nbdir_exp=0;
129        %scan the Exp directory
130        for idir_exp=3:length(list_dir)
131            %detect subdirectories 
132            if list_dir(idir_exp).isdir% 'device' subdirectories
133                nbdir_exp=nbdir_exp+1;
134                ExpData.Device{nbdir_exp}=list_dir(idir_exp).name;
135                [t,DeviceElement]=add(t,1,'element',list_dir(idir_exp).name);
136                t=attributes(t,'add',DeviceElement,'type','DEVICE_DIR');
137                t=attributes(t,'add',DeviceElement,'source','dir');
138                list_subdir=dir(fullfile(ExpPath,list_dir(idir_exp).name));
139                nbsubdir=0;
140                testrecord=1;
141                RootIma='';
142                RootNc='';
143                nbfile=0;
144%                 nbfile={};
145                %scan the Device subdirectory
146                for isubdir=3:length(list_subdir)
147                    if list_subdir(isubdir).isdir
148                        nbsubdir=nbsubdir+1;
149                        Device.Record{nbsubdir}=list_subdir(isubdir).name;
150                    else
151                        nbfile=nbfile+1;
152                        fname{nbfile}=list_subdir(isubdir).name;
153                    end
154                end
155                if isunix%sort by root names and indices , change the separator '_' so that 1_1 come as the first name
156                    fname_mod=regexprep(fname,'_','/');
157                    fname_mod=sort(fname_mod);     %sort by name
158                    fname=regexprep(fname_mod,'/','_');
159                end
160                for ifile=1:nbfile;
161                    [Path,Name,Ext]=fileparts(fname{ifile});
162                    if isequal(Ext,'.xml')
163                       [t,ImaDocElement]=add(t,DeviceElement,'element','ImaDoc');
164                       t=add(t,ImaDocElement,'chardata',fname{ifile});
165                       t=attributes(t,'add',ImaDocElement,'source','file');
166                       testrecord=0;%we have an image series without 'record' subdir
167                    elseif isequal(Ext,'.png')
168                       %[Path,Root,field_count,str2,str_a,str_b,ext,nom_type,subdir]=name2display(fname{ifile});
169                       [~,~,Root]=fileparts_uvmat(fname{ifile});
170                       if ~isequal(Root,RootIma)%only one image recorded for each root name
171                           [t,ImaDocElement]=add(t,DeviceElement,'element','Image');
172                           t=add(t,ImaDocElement,'chardata',fname{ifile});
173                           t=attributes(t,'add',ImaDocElement,'source','file');
174                           RootIma=Root;
175                       end
176                       testrecord=0;%we have an image series without 'record' subdir
177                    elseif isequal(Ext,'.nc')
178                       %[Path,Root,field_count,str2,str_a,str_b,ext,nom_type,subdir]=name2display(fname{ifile});
179                       [~,~,Root]=fileparts_uvmat(fname{ifile});
180                       if ~isequal(Root,RootNc)%only one image recorded for each root name
181                           [t,ImaDocElement]=add(t,DeviceElement,'element','Ncdata');
182                           t=add(t,ImaDocElement,'chardata',fname{ifile});
183                           t=attributes(t,'add',ImaDocElement,'source','file');
184                           RootNc=Root;
185                       end
186                       testrecord=0;%we have an image series without 'record' subdir
187                    end
188                end
189                if testrecord
190                    %the subdevice directory is 'record' (no images detected at this level)
191                    for idir_s=1:nbsubdir
192                        [t,RecordElement]=add(t,DeviceElement,'element',Device.Record{idir_s});
193                        t=attributes(t,'add',RecordElement,'type','RECORD_DIR');
194                        t=attributes(t,'add',RecordElement,'source','dir');
195                        list_subdir=dir(fullfile(ExpPath,list_dir(idir_exp).name,Device.Record{idir_s}));
196                        nbsubdir=0;
197                        RootIma='';
198                        RootNc='';
199                        nbfile=0;
200                        fname={};
201                        for isubdir=3:length(list_subdir)
202                            if list_subdir(isubdir).isdir
203                                nbsubdir=nbsubdir+1;
204                                [t,RecordElement]=add(t,DeviceElement,'element',Device.Record{idir_exp});
205                                t=attributes(t,'add',RecordElement,'type','RECORD_DIR');
206                                t=attributes(t,'add',RecordElement,'source','dir');
207                                %VOIR les .netcdf a l'interieur
208                            else
209                                nbfile=nbfile+1;
210                                fname{nbfile}=list_subdir(isubdir).name;
211                            end
212                        end
213                        if isunix
214                            fname_mod=regexprep(fname,'_','/');
215                            fname_mod=sort(fname_mod);     %sort by name
216                            fname=regexprep(fname_mod,'/','_');
217                        end
218                        for ifile=1:nbfile;           
219                            [Path,Name,Ext]=fileparts(fname{ifile});
220                            if isequal(Ext,'.xml')
221                               [t,ImaDocElement]=add(t,DeviceElement,'element','ImaDoc');
222                               t=add(t,ImaDocElement,'chardata',fname{ifile});
223                               t=attributes(t,'add',ImaDocElement,'source','file');
224                            elseif isequal(Ext,'.png')
225                              % [Path,Root,field_count,str2,str_a,str_b,ext,nom_type,subdir]=name2display(fname{ifile});
226                                [~,~,Root]=fileparts_uvmat(fname{ifile});
227                               if ~isequal(Root,RootIma)
228                                   [t,ImaDocElement]=add(t,DeviceElement,'element','Image');
229                                   t=add(t,ImaDocElement,'chardata',fname{ifile});
230                                   t=attributes(t,'add',ImaDocElement,'source','file');
231                                   RootIma=Root;
232                               end
233                            elseif isequal(Ext,'.nc')
234                               %[Path,Root,field_count,str2,str_a,str_b,ext,nom_type,subdir]=name2display(fname{ifile});
235                               [~,~,Root]=fileparts_uvmat(fname{ifile});
236                               if ~isequal(Root,RootNc)%only one image recorded for each root name
237                                  [t,ImaDocElement]=add(t,DeviceElement,'element','Ncdata');
238                                  t=add(t,ImaDocElement,'chardata',fname{ifile});
239                                  t=attributes(t,'add',ImaDocElement,'source','file');
240                                  RootNc=Root;
241                               end
242                            end
243                        end
244                    end
245                else%the subdevice directory is a civ directory (coexist with images)
246                     for idir_s=1:nbsubdir
247                        [t,RecordElement]=add(t,DeviceElement,'element',Device.Record{idir_s});
248                        t=attributes(t,'add',RecordElement,'type','CIV_DIR');
249                        t=attributes(t,'add',RecordElement,'source','dir');
250                        %list files under the civ directory
251                        list_subdir=dir(fullfile(ExpPath,list_dir(idir_exp).name,Device.Record{idir_s}));
252                                       
253                        nbsubdir=0;
254                        nbfile=0;
255                        RootXml='';
256                        RootNc='';       
257                        fname={};
258                        for isubdir=3:length(list_subdir)
259                            if list_subdir(isubdir).isdir
260                                nbsubdir=nbsubdir+1;
261                                [t,SubElement]=add(t,RecordElement,'element',list_subdir(isubdir).name);
262                                t=attributes(t,'add',SubElement,'type','UNKNOWN_DIR');
263                                t=attributes(t,'add',SubElement,'source','dir');
264                            else
265                                nbfile=nbfile+1;
266                                fname{nbfile}=list_subdir(isubdir).name;
267                            end
268                        end
269                        if isunix
270                            fname_mod=regexprep(fname,'_','/');
271                            fname_mod=sort(fname_mod);     %sort by name
272                            fname=regexprep(fname_mod,'/','_');
273                        end
274                        for ifile=1:nbfile;
275                            [Path,Name,Ext]=fileparts(fname{ifile});
276                            if isequal(Ext,'.xml')
277                               %[Path,Root,field_count,str2,str_a,str_b,ext,nom_type,subdir]=name2display(fname{ifile});
278                               [~,~,Root]=fileparts_uvmat(fname{ifile});
279                               if ~isequal(Root,RootXml)%only one image recorded for each root name
280                                   [t,ImaDocElement]=add(t,RecordElement,'element','CivDoc');
281                                   t=add(t,ImaDocElement,'chardata',fname{ifile});
282                                   t=attributes(t,'add',ImaDocElement,'source','file');
283                                   RootXml=Root;
284                               end
285                            elseif isequal(Ext,'.nc')
286                               %[Path,Root,field_count,str2,str_a,str_b,ext,nom_type,subdir]=name2display(fname{ifile});
287                               [~,~,Root]=fileparts_uvmat(fname{ifile});
288                               if ~isequal(Root,RootNc)%only one image recorded for each root name
289                                  [t,ImaDocElement]=add(t,RecordElement,'element','Ncdata');
290                                  t=add(t,ImaDocElement,'chardata',fname{ifile});
291                                  t=attributes(t,'add',ImaDocElement,'source','file');
292                                  RootNc=Root;
293                               end
294                            end
295                        end
296                    end
297                end
298            end
299        end
300        save(t);%display xml file on the screen
301        save(t,ExpDocName);
302    end
303end
304% [erread,message]=fileattrib('./DATA');
305% if ~isempty(message) & ~isequal(message.UserWrite,1)
306%      errordlg(['Need writting access to ' message.Name])
307%      return
308% end
309
310
311    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
312    function project_update(obj,event,fileinput)
313    hchild=get(gcbf,'children');
314    h=findobj(gcbf,'Tag','listbox')
315   
Note: See TracBrowser for help on using the repository browser.