How do I call functions in MATLAB using an m-file?

In summary, the conversation is about creating an m-file that asks for user input and performs a series of functions to return an answer. The user is having trouble calling functions using an m-file and is seeking help to understand how to do so. They provide an example of one of their functions and ask if they can use it in their own m-file to get the desired values. The expert responds that it is possible and asks for clarification on the issue. The user later confirms that the function is working as intended.
  • #1
satinysol
14
0
I have to make an m-file that asks the user to input two values.

Then it performs a series of functions and returns an answer.

I have made the functions (each in their own m-file)

But I don't understand how you call functions using an m-file. I have to have one main m-file which calls all the functions so it can give an answer to the user...

Please help me ,thx
 
Physics news on Phys.org
  • #2
You just call the functions by their names as defined on the top line of the file. Post an example of one of your functions.
 
  • #3
Joffan said:
You just call the functions by their names as defined on the top line of the file. Post an example of one of your functions.

I want to call this function function varargout = peakfinder(x0, sel, thresh, extrema) which need me to input x0.

the command for running this function is [peakMag,peakloc]=peakfinder(x0) which can let me get the values of peakMag and peakloc.

then i wrote my own m.file and need to use this function to get peakMag and peakloc of my data , can i just use [peakMag,peakloc]=peakfinder(smootherwavedata); to get the values ?
 
  • #4
sounds reasonable. What's the issue?
 
  • #5
Joffan said:
sounds reasonable. What's the issue?

It works now. ^^
 

1. What is a function in Matlab?

A function in Matlab is a block of code that can be called and executed multiple times throughout a program. It is used to perform a specific task or calculation and can take in input arguments and produce output arguments.

2. How do I call a function in Matlab?

To call a function in Matlab, you need to use its name followed by parentheses. If the function has input arguments, they must be included inside the parentheses, separated by commas. If the function has output arguments, they can be assigned to variables using the assignment operator (=).

3. Can I create my own functions in Matlab?

Yes, you can create your own functions in Matlab. You can define a function using the keyword "function" followed by the function name, input arguments (if any), and output arguments (if any). The code for the function should be enclosed in a pair of curly braces {}.

4. What is the difference between a function and a script in Matlab?

A function is a block of code that can be called and executed multiple times, while a script is a series of commands that are executed in a sequence. Functions can take in input arguments and produce output arguments, while scripts cannot. Also, functions have their own variable scope, while scripts share the same variable scope as the main program.

5. How do I pass data between functions in Matlab?

Data can be passed between functions in Matlab by using input and output arguments. The input arguments are used to pass data from the main program to the function, while the output arguments are used to pass data from the function back to the main program. The input and output arguments must be defined and used in the correct order.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
1
Views
867
  • Engineering and Comp Sci Homework Help
Replies
4
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
754
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Programming and Computer Science
Replies
2
Views
276
  • Engineering and Comp Sci Homework Help
Replies
3
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
13
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
967
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
Back
Top