Insert rows into Excel with MATLAB

99封情书 提交于 2021-01-29 04:12:26

问题


The data in my Excel files is supposed to be contentious (index in the first column). But some data is missing in the file. For example, # 5, and 6 are missing between $ 4 and 7. My purpose are (1) identify the file with missing data and (2) if data is missing insert rows to make it continuous. Can anyone tell me how to add in rows in the existing data? Using xlswrite I can only add in rows at the end of the file or replace some rows.

EDIT 1:

I have another set of file in which the index is not so direct. The first 3 columns are described below (as shown in the Excel file):

  • Column 1:Year: 2003 (read as number in matlab)
  • Column 2:Date: 1-Sep (read as text in matlab)
  • Column 3:Time: 1:00 (1:00 read as number 0.04167 and 2:00 read as 0.0833, not sure how it works)

Then the way to tell if it is continuous will be quite complicate since there will be different years, months, and days. Could you give some hint on this?


回答1:


Basically you need to read the entire data, preferably in raw(cell) format, add the missing rows(with respect to the indices) and write back.

Based on your question, this code might work -

% 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);

EDIT 1: In view of your new requirements, additional code is added next.

Please note few things about this code -

  1. The code adds data for every year and not from a specific month, date and time to another specific month, date and time. If you wish to achieve that, please edit the associated function - 'create_comp_sheet'.
  2. It saves an intermediate file named - 'proper_base_data.xls', which maybe deleted at the end of the code.
%% 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);

Associated function -

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;

Hope this saves all your troubles!



来源:https://stackoverflow.com/questions/22048257/insert-rows-into-excel-with-matlab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!