Putting a for loop in a function
Ältere Kommentare anzeigen
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 :)
Antworten (1)
Star Strider
am 9 Mai 2017
0 Stimmen
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 Kommentare
WTB
am 9 Mai 2017
Star Strider
am 9 Mai 2017
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.
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!