How to write the first line of a function?

16 views (last 30 days)
Mary292 on 13 Jan 2015
Commented: Image Analyst on 9 Mar 2015
I have a matrix of data, X. I need to create a function that will plot all columns of this data in a given number of different figures. The function should be called composite_plot. The function should have two inputs, the first will be the data, X, the second input will be the number of figures to be produced. For example, a call to composite_plot(X,10), should produce ten figures.
How would I write the first line of the function? I've never written a function before and so don't really know how many inputs and outputs to have or what to call them. I know it should be set out something like this
Can anyone help?
Image Analyst
Image Analyst on 9 Mar 2015
I think questions about homework should be allowed, but the ideal way is to "Tag" them as homework so we know that we should just give hints rather than outright solutions . This is better than not tagging, getting a full solution, then editing away the question. Of course probably only like 10% of posters tag their own homework questions as homework. How do we let first-time posters know about the homework tag? Maybe they would tag it as homework if they knew that was the ethical/proper way to post.

Sign in to comment.

Answers (1)

Iain on 13 Jan 2015
Edited: Iain on 13 Jan 2015
The first line of a function should look something like this:
function [a b c] = something(a, b, c) % 3 inputs, 3 outputs
function something(a,b) % 2 inputs, 0 outputs
function varargout = something(varargin) %any number of inputs and outputs
function a = something(varargin) % 1 output, any number of inputs.
function [a b c d varargout] = something % no inputs, 4 outputs MINIMUM
function something(a,b,c,d,varargin) % no outputs, 4 inputs MINIMUM.
You should use sensible variable names, instead of random letters like I did. varargin and varargout are more advanced features that let you deal with variable numbers of inputs and outputs. nargin and nargout are what tells you how many inputs & outputs you have if you want to use them.
You should also remember that any input or output can be an array.


Find more on Historical Contests in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!