Hi everyone,
I'm working on a wave dataset stored in a .csv format (I've attached a sample to this post). I'm using the 2019b edition of Matlab. I wrote a script that enables me to plot the changes in wave properties for the chosen month, over the years:
%script to plot changes in wave height data with time
%load data
data=readtable(myfilename);%create subsets of data divided by month
Jan=data{:,2}==1;Feb=data{:,2}==2;%etc...
%define variables
year=Jan{:,1};height=Jan{:,3};%plot
figure(1)clfplot(year,height,'b')xlabel('Year')ylabel('H_s')hold on%fit a trendline through the data
height_coeff=polyfit(year,height,1);height_trend=polyval(height_coeff,year);plot(year,height_trend,'r');
I would like to turn this into a function with two inputs (filename and month), so that I could change the file and the month of data that I want to plot without editing the code itself. I've tried something like the code below, but it returns an empty graph. I suspect it has something to do with me not defining 'month' correctly, but I don't know how to fix it. Help would be much appreciated
function trial(filename, month)data=readtable(filename);Jan=data{:,2}==1;Feb=data{:,2}==2;Mar=data{:,2}==3;Apr=data{:,2}==4;May=data{:,2}==5;Jun=data{:,2}==6;Jul=data{:,2}==7;Aug=data{:,2}==8;Sep=data{:,2}==9;Oct=data{:,2}==10;Nov=data{:,2}==11;Dec=data{:,2}==12;month={Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec};year=month{:,1};height=month{:,3};plot(year,height,'b')xlabel('Year')ylabel('H_s')hold onheight_coeff=polyfit(year,height,1);height_trend=polyval(height_coeff,year);plot(year,height_trend,'r');end
Best Answer