How can I efficiently pass variables from an m-file to a function in MATLAB?

  • Thread starter Thread starter andrew.c
  • Start date Start date
  • Tags Tags
    Matlab Variables
AI Thread Summary
To efficiently pass variables from an m-file to a function in MATLAB, avoid using global variables due to inefficiency. Instead, define your function in a separate file, such as "shift.m", and call it with the correct syntax from your main script. When calling the function, remember that MATLAB passes copies of the variables, similar to C. For further guidance, refer to the MATLAB tutorial, particularly section 4-22. Understanding these principles will help streamline your variable passing process.
andrew.c
Messages
46
Reaction score
0
Hiya,

I have been trying for hours to figure out how to pass a variable from an m-file to a function,

i could get it to work by making the variables global, but I've beeen told this is inefficient.

Any ideas?
Cheers!
 
Physics news on Phys.org
Take a look at this Matlab tutorial, especially section 4-22. http://www.mathworks.com/access/helpdesk/help/pdf_doc/matlab/getstart.pdf
 
Perhaps I am misunderstanding the question, but you call the function in your main ".m" file (script).
Assuming your function name is "shift(input1, input2)", then it should be stored in "shift.m", (as it is a function, and they are typically stored in separate files). You call it appropriately with the correct function syntax. Remember in Matlab, you are passing copies of the variables into the function, like you would in C.
 
Back
Top