使用MATLAB删除excel文件行

时间:2014-07-01 05:43:37

标签: excel matlab

我将使用Matlab删除excel文件行

具体来说,我已经删除了一些满足条件的值。 (在这种情况下,我删除了2 sigma之外的元素(统计分布))

但是我遇到了不受欢迎的结果,因为他们只删除了值并将位置保持为空。 所以我正在寻找删除行的方法或移动元素以不留空格。

%**Open the file**
fullFileName  = [pwd '\Eurostoxx50_7월.xlsm'];
excel = actxserver('Excel.Application');
file = excel.Workbooks.Open(fullFileName);
sheet1=excel.Worksheets.get('Item', 'Inputsheet');

size_of_pd = size(PriceDifference2);
size_of_pd =  size_of_pd(1);

%**Get the index where I want to remove**
m = mean(PriceDifference2);
s = std(PriceDifference2);
v1=m+2*s
v2=m-2*s

TF1 = PriceDifference2(:) >= v1 ;
TF2 = PriceDifference2(:) <= v2 ;
% combine them
TFall = TF1 | TF2;


%remove the elements 
for i = 1:1:size_of_pd 
    if TFall(i) > 0 
        first_cell = strcat('B',num2str(i+34));
        last_cell = strcat('Q',num2str(i+34));
        range1=get(sheet1,'Range', first_cell,last_cell);
        range1.Value=[];
    end

end

file.Save;
file.Close;
delete(excel);    

更具体地说,结果如下所示在excel文件中

0.002678839 0   0.479452055 3204.381729 2850    41.1    P   -1.472671354
0.002678839 0   0.479452055 3204.381729 2900    48.9    P   -1.508805266
0.002678839 0   0.479452055 3204.381729 2925    53.3    P   -1.341898247



0.002678839 0   0.479452055 3204.381729 3350    210.8   P   12.3246967
0.002678839 0   0.479452055 3204.381729 3375    226.5   P   11.98361578
0.002678839 0   0.479452055 3204.381729 3400    243.1   P   11.31755056
0.002678839 0   0.479452055 3204.381729 3425    260.4   P   10.86345463


0.002678839 0   0.479452055 3204.381729 3350    210.8   P   12.3246967
0.002678839 0   0.479452055 3204.381729 3375    226.5   P   11.98361578
0.002678839 0   0.479452055 3204.381729 3400    243.1   P   11.31755056
0.002678839 0   0.479452055 3204.381729 3425    260.4   P   10.86345463

但我想删除所有空格,如下所示

0.002678839 0   0.479452055 3204.381729 2850    41.1    P   -1.472671354
0.002678839 0   0.479452055 3204.381729 2900    48.9    P   -1.508805266
0.002678839 0   0.479452055 3204.381729 2925    53.3    P   -1.341898247                                                                                                                                                
0.002678839 0   0.479452055 3204.381729 3350    210.8   P   12.3246967
0.002678839 0   0.479452055 3204.381729 3375    226.5   P   11.98361578
0.002678839 0   0.479452055 3204.381729 3400    243.1   P   11.31755056
0.002678839 0   0.479452055 3204.381729 3425    260.4   P   10.86345463
0.002678839 0   0.479452055 3204.381729 3350    210.8   P   12.3246967
0.002678839 0   0.479452055 3204.381729 3375    226.5   P   11.98361578
0.002678839 0   0.479452055 3204.381729 3400    243.1   P   11.31755056
0.002678839 0   0.479452055 3204.381729 3425    260.4   P   10.86345463

1 个答案:

答案 0 :(得分:0)

我以愚蠢的方式解决了这个问题。 只需复制和粘贴多次

fullFileName  = [pwd '\Eurostoxx50_7월.xlsm'];
if isempty(fullFileName)
    % User clicked Cancel.
    return;
end
excel = actxserver('Excel.Application');
sheet1=excel.Worksheets.get('Item', 'Inputsheet');




start_point = 34;

size_of_pd = size(PriceDifference2);
size_of_pd =  size_of_pd(1);
%last_cell = strcat('Q',num2str(size_of_pd ));
first_cell = strcat('Q',num2str(start_point+1));
last_cell = strcat('Q',num2str(size_of_pd));
range1=get(sheet1,'Range', first_cell,last_cell);
PD_EXCEL = range1.value;


m = mean(PD_EXCEL);
s = std(PD_EXCEL);
v1 = m+2*s;
v2 = m-2*s;

TF1 = PD_EXCEL(:) >= v1 ;
TF2 = PD_EXCEL(:) <= v2 ;
% combine them
TFall = TF1 | TF2;


for i = 1:1:size_of_pd 
    if TFall(i) > 0
        first_cell = strcat('B',num2str(i+start_point));
        last_cell = strcat('Q',num2str(i+start_point));

        range1= get(sheet1,'Range', first_cell,last_cell);
        range1.Value = [];
    end

end

for i = size_of_pd:-1:1
    if TFall(i)>0
        copy_first_cell = strcat('B',num2str(i+start_point+1));
        copy_last_cell = strcat('Q',num2str(size_of_pd+start_point));


        first_cell = strcat('B',num2str(i+start_point));
        last_cell = strcat('Q',num2str(size_of_pd+start_point-1));


        range1=get(sheet1,'Range', copy_first_cell,copy_last_cell);
        range2 = get(sheet1,'Range',first_cell,last_cell);
        range2.Value=range1.value;

    end
end

first_cell = strcat('B',num2str(start_point + sum(TFall(:)==0)));
last_cell = strcat('Q',num2str(start_point+size_of_pd));
range1 = get(sheet1,'Range', first_cell,last_cell);
range1.Value = [];

file.Save;
file.Close;
delete(excel);