如何使用代码(MATLAB)在矩阵/excel电子表格中添加零行? [英] How to add rows of zeroes in matrices/excel spreadsheet with code (MATLAB)?

查看:411
本文介绍了如何使用代码(MATLAB)在矩阵/excel电子表格中添加零行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建此程序的原因是用户可以输入2009年至2011年之间的年份,并在图表上显示所选年份的积雪深度和气温数据.

I made this program were the user can type in a year between 2009 and 2011 and get snow depth and air temperature data for the chosen year displayed on a graph.

从第1天到第365天,我每天都有一个xls文件用于温度.从第1天到第212天,我还有另一个xls文件用于雪深度.所以有些日子,他们没有费心去测量雪深,因为反正还是夏天.在4月30日至10月1日之间,他们没有测量雪深.我想添加153个零,代码在4月30日至4月1日之间.因此,它最多可以计算365天.

I have one xls file for the temperature for each day from day 1 to day 365. I also have another xls file for snow depth from day 1 to day 212. So some days they didn't bother to measure snow depth because it was summer anyways. Between 30. april to 1. october they didn't measure snow depth. I want to add 153 zeroes with code between 30. april and 1. oct so it adds up to 365 days.

第121天= 4月30日

day 121 = 30. april

下载空气温度xls: https://dl.dropbox.com/u/11241083 /airtemp2009_2011.xls

Download Air temp xls: https://dl.dropbox.com/u/11241083/airtemp2009_2011.xls

下载下雪深度xls: https://dl.dropbox.com/u/11241083 /snow2009_2011.xls

Download Snow depth xls: https://dl.dropbox.com/u/11241083/snow2009_2011.xls

这是到目前为止的代码:

Here's the code so far:

chosenyear = input('Type in year: ');
snowdepth = xlsread('snow2009_2011.xls');
snowdepth2009 = snowdepth(:,1);
snowdepth2010 = snowdepth(:,2);
snowdepth2011 = snowdepth(:,3);
airtemp = xlsread('airtemp2009_2011.xls');
airtemp2009 = airtemp(:,1);
airtemp2010 = airtemp(:,2);
airtemp2011 = airtemp(:,3);
if chosenyear == 2009
    figure(1);
    plot(snowdepth2009, 'b');
      title(['Snowdepth/airtemp ' num2str(chosenyear)]);
      xlabel('Days/months');
      ylabel('data');
      grid on
      datetick('x','m');
      hold on
      plot(airtemp2009, 'r');
      legend('snow depth in cm','air temp in Celsius');
elseif chosenyear == 2010
    figure(2);
    plot(snowdepth2010, 'b');
      title(['Snowdepth/airtemp ' num2str(chosenyear)]);
      xlabel('Days/months');
      ylabel('data');
      grid on
      datetick('x','m');
      hold on
      plot(airtemp2010, 'r');
      legend('snow depth in cm','air temp in Celsius');
elseif chosenyear == 2011
    figure(3);
    plot(snowdepth2011, 'b');
      title(['Snowdepth/airtemp ' num2str(chosenyear)]);
      xlabel('Days/months');
      ylabel('data');
      grid on
      datetick('x','m');
      hold on
      plot(airtemp2011, 'r');
      legend('snow depth in cm','air temp in Celsius');
else
    disp('NB! Type in 2009, 2010 or 2011');
    filename.m
end

推荐答案

尝试一下,

snowDepth_full = zeros(365,3); % make a temporary variable to hold everything
snowDepth_full(1:121,:)  = snowDepth(1:121,:);   % First 121 days up to april 30
snowDepth_full(end-90,:) = snowDepth(122:end,:); % Last 91 days from oct 1 on
snowDepth = snowDepth_full;  % Copy it back 

然后像以前一样...

Then as before...

snowdepth2009 = snowdepth(:,1);
snowdepth2010 = snowdepth(:,2);
snowdepth2011 = snowdepth(:,3);
...

这篇关于如何使用代码(MATLAB)在矩阵/excel电子表格中添加零行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆