我正在尝试编写一个脚本,加载一个excel文件,并将10个子文件写入10个单独的文件或单独的sheets.Im,这是MATLAB的新功能,但我遇到了一些麻烦。
需要找出一种加载文件的方法,并且只访问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);这会产生垃圾
我也不确定如何对循环进行编码,以生成单独的文件或单独的工作表。
任何帮助都将不胜感激。
发布于 2016-10-27 09:59:07
下面是从文件中读取并写入前两列的代码,而不管其中包含什么数据(数字或字符)。
% 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函数返回的前两个值;我们需要的是第三个输出,它将是一个单元格数组。
https://stackoverflow.com/questions/40273514
复制相似问题