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
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
2017년 5월 9일
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 Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!