0

I'm trying to write a script that loads an excel file and writes 10 sub-file either into 10 separate file or separate sheets.Im new to MATLAB and I've been running into some trouble.

Need to figure a way to load the file and only access A1:B1000 on the excel file then write that information on a new excel file. Then load A1000:B2000, etc...

My idea and code is as follows:

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

I tired the following:

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

This crashed MATLAB and freezes my laptop.

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

This produces garbage

I'm also not sure how to encode the loop to produce separate files or separate sheets.

Any help would be most appreciated.

learnmore
  • 1
  • 1
  • 1

1 Answers1

0

Here is code to read from a file and write the first two columns, regardless of what data is in them (numeric or char).

% 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

The tildes in the line [~, ~, data] just mean that the first two values returned from the xlsread function should be ignored; all we want is the third output, which will be a cell array.

verbatross
  • 607
  • 1
  • 5
  • 10