2014-07-01 772 views
0

我打算使用Matlab刪除excel文件的行使用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 
+0

什麼樣的數據類型是你的矩陣? cell,struct,double? –

回答

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);