Transforming a script into a function with multiple inputs

1 次查看(过去 30 天)
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)
clf
plot(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 on
height_coeff=polyfit(year,height,1);
height_trend=polyval(height_coeff,year);
plot(year,height_trend,'r');
end

采纳的回答

Sudhakar Shinde
Sudhakar Shinde 2020-10-15
编辑:Sudhakar Shinde 2020-10-15
%load data
myfilename = 'Example.csv';
month = 1;
trial(myfilename, month);
function trial(myfilename, month)
data=readtable(myfilename);
%Extract feb data
[year,height]=Extract(data,month);
plot(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');
end
%This function could extract data of jan(1) or feb(2) or march(3).
%Similaraly add cases till dec (12)
function [Year,Height]= Extract(data,month)
switch month
case 1
Jan=(data.Month==1);
Year = data.Year(Jan);
Height = data.Height(Jan);
case 2
FEB=(data.Month==2);
Year = data.Year(FEB);
Height = data.Height(FEB);
case 3
March=(data.Month==3);
Year = data.Year(March);
Height = data.Height(March);
end
end

更多回答(1 个)

Ameer Hamza
Ameer Hamza 2020-10-15
You input variable name is 'month', but you are overwriting it on this line
month={Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec};
Use some other variable name here.

类别

Help CenterFile Exchange 中查找有关 Standard File Formats 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by