Here are a few functions that I think might be a little bit helpful to handle office task on Windows, especially EXCEL.
Computing Levenshtein and editor distances with use of Vagner-Fisher algorithm.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
% 20170331 function d=fuz_strcmp(r,b,krk,cas) % fuzzy string comparation % calculation of distance btw strings % Computing Levenshtein and editor distances with use of Vagner-Fisher algo. %d=strdist(r,b,krk,cas) computes Levenshtein and editor distance %between strings r and b with use of Vagner-Fisher algorithm. % Levenshtein distance is the minimal quantity of character %substitutions, deletions and insertions for transformation %of string r into string b. An editor distance is computed as %Levenshtein distance with substitutions weight of 2. %d=strdist(r) computes numel(r); %d=strdist(r,b) computes Levenshtein distance between r and b. %If b is empty string then d=numel(r); %d=strdist(r,b,krk)computes both Levenshtein and an editor distance %when krk=2. d=strdist(r,b,krk,cas) computes a distance accordingly %with krk and cas. If cas>0 then case is ignored. % %Example. % disp(strdist('matlab')) % 6 % disp(strdist('matlab','Mathworks')) % 7 % disp(strdist('matlab','Mathworks',2)) % 7 11 % disp(strdist('matlab','Mathworks',2,1)) % 6 9 switch nargin case 1 d=numel(r); return case 2 krk=1; bb=b; rr=r; case 3 bb=b; rr=r; case 4 bb=b; rr=r; if cas>0 bb=upper(b); rr=upper(r); end end if krk~=2 krk=1; end d=[]; luma=numel(bb); lima=numel(rr); lu1=luma+1; li1=lima+1; dl=zeros([lu1,li1]); dl(1,:)=0:lima; dl(:,1)=0:luma; %Distance for krk1=1:krk for i=2:lu1 bbi=bb(i-1); for j=2:li1 kr=krk1; if strcmp(rr(j-1),bbi) kr=0; end dl(i,j)=min([dl(i-1,j-1)+kr,dl(i-1,j)+1,dl(i,j-1)+1]); end end d=[d dl(end,end)]; end d = d/max(numel(r),numel(b)); return |
Use the following file to manipulate massive Excel files.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 |
clear; close all; %% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % !!!!!!!!!!!!!Change and Check everytime you use!!!!!!!!!!!!!!!!!!!! path0= 'D:\GN Luo\WagePayroll\Merged\Files\Table_30\'; cd(path0); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% if ~exist('NewFormat','dir')==7 mkdir('NewFormat'); end path1=strcat(path0,'NewFormat\'); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Swith mode mode1 = 'Single'; % mode1 = 'All'; % Pruning all xlsx files in the folder % mode1 = 'Single'; % Pruning specific single file in the folder mode2 = 'Cut_Blank_Row'; % mode2 = 'Cut_Blank_Row'; % delete the row that there are too many blanks % mode2 = 'Cut_Blank_Col'; % delete the row that there are too many blanks % mode2 = 'Tetris'; % move the cells like tetris % mode2 = 'Special'; % Special cases %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % specify the range that you want to manipulate row1 = 7; % Protect title row2 = 1000; col1 = 'A'; col2 = 'Z'; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % for the mode2 = 'Cut_Blank_Col' or mode2 = 'Cut_Blank_Row' % !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Tolerance = 0; % Tolerance of the number of blank %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % for mode2 = 'Tetris' ot mode2 = 'Special'; % !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Gravity = 'Up'; % Move the cells up %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % for mode1 = 'Single'; % declare the file which you want to prune filename = '2007M06D26V0_Table_3_T30.xlsx'; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % ActiveX to use VBA in MATLAB Excel = actxserver('excel.application'); % Create COM server % Create a COM server running Microsoft Excel. Excel.Visible = true; % Make the Excel frame window visible. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% switch mode1 case 'Single' % Activate the workbook WB = invoke(Excel.Workbook,'open', ... strcat(path0,filename)); SH = WB.Worksheets.Item(1); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% if strcmp(mode2,'Cut_Blank_Row') for rownum = row2:-1:row1 % suppose row2 is large enough rowinfo = SH.Range(strcat(col1,num2str(rownum),... ':',col2,num2str(rownum))).Value; % suppose A:BZ is large enough if sum(cellfun(@(V)any(1-isnan(V(:))),rowinfo))<=Tolerance % delete the row that there are too many blanks % 3 is the critirion, which can be decided by the % specific excel SH.Rows.Item(rownum).Delete; end end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% elseif strcmp(mode2,'Cut_Blank_Col') for colnum = Cha2Num(col2):-1:Cha2Num(col1) % suppose col2 is large enough colinfo = SH.Range(strcat(Num2Cha(colnum),num2str(row1),... ':',Num2Cha(colnum),num2str(row2))).Value; % suppose A:BZ is large enough if sum(cellfun(@(V)any(1-isnan(V(:))),colinfo))<=Tolerance % delete the row that there are too many blanks % 3 is the critirion, which can be decided by the % specific excel SH.Columns.Item(colnum).Delete; end end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% elseif strcmp(mode2,'Tetris') if strcmp(Gravity ,'Up') %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% for colnum = Cha2Num(col1):Cha2Num(col2) % suppose col2 is large enough for rownum = row2:-1:row1 % suppose row2 is large enough bottom = row2; % the bottom of the trtris block in column colnum cellinfo = SH.Range(strcat(Num2Cha(colnum)... ,num2str(rownum))).Value; % the value of the cell if isnan(cellinfo) % if the cell is blank SH.Range(strcat(Num2Cha(colnum),num2str(rownum),... ':',Num2Cha(colnum),num2str(bottom-1))).Value = ... SH.Range(strcat(Num2Cha(colnum),num2str(rownum+1),... ':',Num2Cha(colnum),num2str(bottom))).Value; bottom = bottom-1; end end end end elseif strcmp(mode2,'Special') if strcmp(Gravity ,'Up') %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% for colnum = 1 vector_row = (5:2:row2); for i = 1:numel(vector_row) SH.Range(strcat(Num2Cha(colnum),... num2str(vector_row(i)))).Value= ... SH.Range(strcat(Num2Cha(colnum),... num2str(vector_row(i)+1))).Value; end for i = 1:numel(vector_row) SH.Range(strcat(Num2Cha(colnum),... num2str(vector_row(i)+1))).Value= NaN; end end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% end end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% WB.SaveAs(strcat(path1,filename)); WB.Close() %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% case 'All' %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% files = dir(strcat(path0,'**.xlsx')); % Get all files in the current folder that contait the word 'Page' %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% N_st_file = 1; N_fi_file = length(files); % # of the folders in the subFolder list that we want to manipulate for j = N_st_file:N_fi_file fprintf('Files #%d = %s\n', j, files(j).name); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% WB = invoke(Excel.Workbook,'open', ... strcat(path0,files(j).name)); SH = WB.Worksheets.Item(1); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% if strcmp(mode2,'Cut_Blank_Row') for rownum = row2:-1:row1 % suppose row2 is large enough rowinfo = SH.Range(strcat(col1,num2str(rownum),... ':',col2,num2str(rownum))).Value; % suppose A:BZ is large enough if sum(cellfun(@(V)any(1-isnan(V(:))),rowinfo))<=Tolerance % delete the row that there are too many blanks % 3 is the critirion, which can be decided by the % specific excel SH.Rows.Item(rownum).Delete; end end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% elseif strcmp(mode2,'Cut_Blank_Col') for colnum = Cha2Num(col2):-1:Cha2Num(col1) % suppose col2 is large enough colinfo = SH.Range(strcat(Num2Cha(colnum),num2str(row1),... ':',Num2Cha(colnum),num2str(row2))).Value; % suppose A:BZ is large enough if sum(cellfun(@(V)any(1-isnan(V(:))),colinfo))<=Tolerance % delete the row that there are too many blanks % 3 is the critirion, which can be decided by the % specific excel SH.Columns.Item(colnum).Delete; end end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% elseif strcmp(mode2,'Tetris') if strcmp(Gravity ,'Up') %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% for colnum = Cha2Num(col1):Cha2Num(col2) % suppose col2 is large enough for rownum = row2:-1:row1 % suppose row2 is large enough bottom = row2; % the bottom of the trtris block in column colnum cellinfo = SH.Range(strcat(Num2Cha(colnum)... ,num2str(rownum))).Value; % the value of the cell if isnan(cellinfo) % if the cell is blank SH.Range(strcat(Num2Cha(colnum),num2str(rownum),... ':',Num2Cha(colnum),num2str(bottom-1))).Value = ... SH.Range(strcat(Num2Cha(colnum),num2str(rownum+1),... ':',Num2Cha(colnum),num2str(bottom))).Value; bottom = bottom-1; end end end end elseif strcmp(mode2,'Special') if strcmp(Gravity ,'Up') %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% for colnum = 1 vector_row = (7:2:row2); for i = 1:numel(vector_row) SH.Range(strcat(Num2Cha(colnum),... num2str(vector_row(i)))).Value= ... SH.Range(strcat(Num2Cha(colnum),... num2str(vector_row(i)+1))).Value; end for i = 1:numel(vector_row) SH.Range(strcat(Num2Cha(colnum),... num2str(vector_row(i)+1))).Value= NaN; end end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% end vector_row = (5:-2:1); for i = 1:numel(vector_row) SH.Rows.Item(vector_row(i)).Delete; end SH.Range('C1').Value = SH.Range('A2').Value; SH.Rows.Item(2).Delete end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% WB.SaveAs(strcat(path1,files(j).name)); WB.Close() end end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% cd(path0) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Excel.Quit(); Excel.delete(); clear Excel; clear WB; clear SH; |
in which the Num2Cha and Cha2Num functions are my self-defined functions as follow:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
function Cha = Num2Cha(Num) ChaList='ABCDEFGHIJKLMNOPQRSTUVWXYZ'; if Num<=26 Cha=ChaList(Num); else s=ceil(log(Num)/log(26)); n=floor(Num/26^(s-1)); Num = Num -n*26^(s-1); Cha=ChaList(n); for i = s-1:-1:1 n=floor(Num/26^(i-1)); Num = Num -n*26^(i-1); Cha=strcat(Cha,ChaList(n)); end end return |
1 2 3 4 5 6 7 8 9 10 11 12 13 |
function Num = Cha2Num(Char) ChaList='ABCDEFGHIJKLMNOPQRSTUVWXYZ'; Num=0; if size(Char,2)==1 Num=find(ChaList==Char); else s=size(Char,2); for i = s:-1:1 tempChar=Char(i); Num=Num+26^(s-i)*find(ChaList==tempChar); end end return |
You must be logged in to post a comment.
baoduge
2017年6月20日
终于解决了Code Box的滚动条的问题--代码太长就会占据太多篇幅。现在我使用的是Crayon Syntax Highlighter插件,可以自选主题,同时还有Scroll bar等工具选择,功能很强大。推荐!