Putting a for loop in a function

hello guys,
I'm pretty new to MATLAB and just completed an assignment for school in which we needed to write a for loop to build our own moving average filter. i'm glad to say that i got through that part (not that easy at all :)). not i'm trying to make the code 'better' for myself.
what i'm trying to accomplish is to set this for loop in a function so that i can call for it in another .m file. In the other file i'd like to plot a few graphs simultaneously with different vallues for the moving average. The plots i'm not really worried about and i'd like to accomplish that on my own.
the thing i'm getting stuck on is putting my made .m file in a function so that i can call for it and immediately specify the number of samples it has to gather.
my question: 1. Can you guys nudge me in the right direction of how this function should look like 2. is making this for-loop into a function the right way to go? (or is there a more obvious way i'm not seeing)
Help would be appreciated :)
kind regards,
Wim
P.S. I've uploaded my .m file, hope you can read it because the comments I put in there are in Dutch :)

回答 (1 件)

Star Strider
Star Strider 2017 年 5 月 9 日

0 投票

I applaud your efforts. However if you have R2016a or later, you can use the movmean (link) function. (Note that you also have to account for NaN and other exceptional values in your function.)

2 件のコメント

WTB
WTB 2017 年 5 月 9 日
Thanks for your quick anwser,
while writing the code i've indeed encounterd the function movmean. but i'm trying to work on my matlab skills (which are pretty much non existing :P). that is why i'm trying to accomplish putting this in a function. so that i can accomplish this with the code i wrote myself :)
Star Strider
Star Strider 2017 年 5 月 9 日
My pleasure.
Since your coce apparently works and all you need to do is to create it as a function, I refer you to the documentation on Function Basics.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

質問済み:

WTB
2017 年 5 月 9 日

コメント済み:

2017 年 5 月 9 日

Community Treasure Hunt

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

Start Hunting!

Translated by