【问题标题】:Insert rows into Excel with MATLAB使用 MATLAB 将行插入 Excel
【发布时间】:2014-02-26 17:03:38
【问题描述】:

我的 Excel 文件中的数据应该是有争议的(第一列中的索引)。但是文件中缺少一些数据。例如,# 5 和 6 在 $ 4 和 7 之间丢失。我的目的是 (1) 识别缺少数据的文件,以及 (2) 如果数据丢失插入行以使其连续。谁能告诉我如何在现有数据中添加行?使用 xlswrite 我只能在文件末尾添加行或替换一些行。

编辑 1:

我有另一组文件,其中的索引不是那么直接。前 3 列描述如下(如 Excel 文件所示):

  • Column 1:Year: 2003(在matlab中读为数字)
  • 第 2 列:日期:1-Sep(在 matlab 中读取为文本)
  • 第 3 栏:时间:1:00(1:00 读为数字 0.04167,2:00 读为 0.0833,不知道是怎么回事)

那么判断它是否连续的方法将非常复杂,因为会有不同的年月日。你能就此给出一些提示吗?

【问题讨论】:

  • 我们可以假设索引列的第一个元素总是以 1 开头吗?
  • 在您的问题中,请在您所做的第一次编辑的开头添加文本 - “EDIT 1:”。这样,已接受答案中的已编辑部分将保留在上下文中。
  • 谢谢!相应修改。

标签: excel matlab time insert


【解决方案1】:

基本上,您需要读取整个数据,最好是原始(单元格)格式,添加缺失的行(相对于索引)并写回。

根据您的问题,此代码可能有效 -

% NOTE: We are assuming that the indexing starts with 1

% Read data from input excel file with missing indices
[num,txt,raw] = xlsread('input.xls');

% Error-checking
if (size(num,1)-num(end,1)~=0)
    disp('At least one index is  missing!');
end

% Expand data such that all indices are covered.
data1=NaN(num(end,1),size(raw,2));
data1(:,1) = 1:num(end,1);
data1=num2cell(data1);

k1=1;
for k = 1:num(end,1)
    if(num(k1,1)==k)
        data1(k,:)= raw(k1,:);
        k1 = k1+1;
    end
end

% Write data
xlswrite('output.xls',data1);

编辑 1: 鉴于您的新要求,接下来添加额外的代码。

请注意有关此代码的一些事项 -

  1. 代码添加每年的数据,而不是从特定的月份、日期和时间到另一个特定的月份、日期和时间。如果您希望实现这一点,请编辑相关的 函数 - 'create_comp_sheet'。
  2. 它保存了一个名为 - 'proper_base_data.xls' 的中间文件,它可能会在代码末尾被删除。
%% MAIN CODE - CODE1.M
INPUT_FILENAME = 'input.xls'; % Excel file that has some missing year,date and time info
OUTPUT_FILENAME = 'output.xls'; % Excel file that has data from the input file along with all the missing year,date and time info

%% Base data
start_year=2003;
end_year=2005;
proper_base_data = create_comp_sheet(start_year,end_year);
xlswrite('proper_base_data.xls',proper_base_data);

[num,txt,raw] = xlsread('proper_base_data.xls');
base_data=cell(size(num,1),1);
for row_ID = 1:size(num,1)
    base_data(row_ID) = {strcat(num2str(cell2mat(raw(row_ID,1))),'-', cell2mat(raw(row_ID,2)),'-',num2str(round(24*cell2mat(raw(row_ID,3)))))};
end

%% Input data
[num,txt,raw] = xlsread(INPUT_FILENAME);
input_data=cell(size(num,1),1);
for row_ID = 1:size(num,1)
    input_data(row_ID) = {strcat(num2str(cell2mat(raw(row_ID,1))),'-', cell2mat(raw(row_ID,2)),'-',num2str(round(24*cell2mat(raw(row_ID,3)))))};
end

%% Setup final data
final_data = num2cell(NaN(size(proper_base_data,1),size(raw,2)));
final_data(:,1:3) = proper_base_data;

for k1=1:size(input_data,1)
    for k2=1:size(base_data,1)
        if strcmp(cell2mat(base_data(k2)),cell2mat(input_data(k1)))
            final_data(k2,4:end) = raw(k1,4:end);
        end
    end
end

%% Write final data to excel 
xlswrite(OUTPUT_FILENAME,final_data);

关联函数-

function data1 = create_comp_sheet(start_year,end_year)

months_string = {'Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'};
date_count = [31 28 31 30 31 30 31 31 30 31 30 31];
num_hours = 24;

data1=[];
for year_ID = start_year:end_year
    for month_ID = 1:numel(months_string)

        days_per_month = date_count(month_ID);
        if rem(year_ID,4)==0 && month_ID ==2
            days_per_month = days_per_month+1;
        end

        for date_ID = 1:days_per_month

            year = repmat({num2str(year_ID)},[num_hours 1]);
            date = repmat({strcat(num2str(date_ID),'-',char(months_string(month_ID)))},[num_hours 1]);
            time=cell(num_hours,1);

            for k = 1:num_hours
                time(k) = {strcat(num2str(k),':00')};
            end

            data1 = [data1 ; [year date time]];
        end
    end

end

return;

希望这能省去你所有的麻烦!

【讨论】:

  • 非常感谢!这正是我需要的!我有另一组文件,其中的索引不是那么直接。我在问题中添加了更多信息。你能帮忙看看吗?
  • 抱歉,评论中的格式看起来不太好。我已经在原始问题中添加了信息。你能看看吗?谢谢!
  • 这太棒了!它运作良好!谢谢!但我还有两个问题:(1)如何将 0:00 的时间更改为 24:00,因为我的原始文件显示 24:00。 (2) 除了年份,我还可以设置起止日期和起止时间吗?谢谢!
  • (1)你真的想要那个吗?第 1 天的 24:00 不会与第 2 天的 0:00 重叠吗?如果你想要那样的话,我认为你需要在相关函数中编辑两行 - “num_hours = 25;”和“对于 k = 0:num_hours”。 (2)正如我所说,您需要深入研究相关功能。
  • (1) 我的意思是将 0:00 替换为 24:00。当前代码将删除带有“24:00”的行并插入带有“0:00”的新行。 (2) 我的意思是不仅设置开始/结束年份。我还想设置开始/结束日期和时间。有可能吗?
猜你喜欢
  • 2011-04-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-03
  • 1970-01-01
  • 2014-08-19
相关资源
最近更新 更多