2016-10-26 65 views
0

我正在嘗試編寫一個加載excel文件的腳本,並將10個子文件寫入10個單獨的文件或單獨的工作表中。我剛接觸MATLAB並且遇到了一些麻煩。MATLAB中xlsread和xlswrite麻煩

需要設法加載文件並只訪問excel文件上的A1:B1000,然後將該信息寫入新的excel文件。然後加載A1000:B2000,等...

我的想法,代碼如下:

i=1; 
j=1000; 
TenTimes=1; 
master= 'Master.xlsx'; 
while TenTimes < 10 
     num = xlsread('File1.xlsx');  
    Time = num(:,1); 
    Current = num(:,2); 
    xlswrite(master,[Time Current]); 
    i == j; 
    j = j +1000; 
    TenTimes = TenTimes + 1; 
end 

我厭倦了以下內容:

num=num = xlsread('File1.xlsx', 'Ai:Bj'); 

這墜毀MATLAB和凍結我的筆記本電腦。

num = xlsread('File1.xlsx');  
Time = num('Ai:Aj',1); 
Current = num('Bi:Bj",2); 

這會產生垃圾

我也不能確定如何循環產生單獨的文件或單獨的表進行編碼。

任何幫助將不勝感激。

回答

0

這裏是從文件中讀取並寫入前兩列的代碼,無論數據在哪裏(數字或字符)。

% parameters 
infile = 'File1.xlsx'; % file to read from 
infile_sheet = 'Sheet1'; % sheet to read from 
outfile = 'Master.xlsx'; % file to write to 
col_index = 1:2; % index of columns to write 
block_size = 1000; % size of blocks to write 

% the work 
[~, ~, data] = xlsread(infile, infile_sheet); % read from the file 
num_rows = size(data, 1); 
num_blocks = ceil(num_rows/block_size); 
for block = 1:num_blocks 
    % get the index of rows in the current block 
    start_row = (block-1) * block_size + 1; 
    end_row = min(block * block_size, num_rows); 
    row_index = start_row : end_row; 

    % write the current block to sheet Sheet<block> 
    xlswrite(outfile, data(row_index, col_index), sprintf('Sheet%.0f', block)); 
end 

行[〜,〜,data]中的波浪線意味着從xlsread函數返回的前兩個值應該被忽略;我們所要的只是第三個輸出,它將是一個單元陣列。