【问题标题】:xlsread and xlswrite troubles in MATLABMATLAB中的xlsread和xlswrite问题
【发布时间】:2016-10-27 09:59:07
【问题描述】:

我正在尝试编写一个脚本来加载一个 excel 文件并将 10 个子文件写入 10 个单独的文件或单独的工作表中。我是 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);

这会产生垃圾

我也不确定如何对循环进行编码以生成单独的文件或单独的工作表。

任何帮助将不胜感激。

【问题讨论】:

    标签: excel matlab xlsread


    【解决方案1】:

    这是从文件中读取并写入前两列的代码,无论其中包含什么数据(数字或字符)。

    % 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 函数返回的前两个值;我们想要的只是第三个输出,它将是一个元胞数组。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多