Sorry this is such a basic question, but I can’t find a straight answer and it doesn’t seem to have been answered on here before (perhaps because it is so basic?!)
I’m trying to call a M function from within another M file. The function I am calling is a primary function and has no inputs or outputs: it is simply some lines of code that I’d like to insert many times in my main M-file.
The function is called
function generateGrating
and resides in the file generateGrating.m. The main function is called
function project
and resides in the file project.m. As you can see, there is no input or output in either. They simply run their routines.
I have tried the following to attempt to call the function, with no luck:
generateGrating()
generateGrating
generateGrating.m()
generateGrating.m
generateGrating();
generateGrating;
generateGrating.m();
generateGrating.m;
Any help would be appreciated! It seems that the answer must be so basic that I can’t find it anywhere 🙁
I think that you want to use a script, not a function.
A script is a .m file without the keyword function at the beginning.
So for instance, i fI have two files:
sub.m:
main.m:
I get the answer:
If I change sub.m to make it a function:
sub.m:
The variable b inside sub is now from a different scope than b in main.m
So I get the answer: