Clear Filters
Clear Filters

How to write the first line of a function?

7 views (last 30 days)
Mary292
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
function[?]=composite_plot(?,?)
Can anyone help?
  5 Comments
James Tursa
James Tursa on 9 Mar 2015
"... asked by my professor to do this until the assignment deadline has passed ..."
Regardless of what your professor may have instructed you to do or not to do, I have to agree with others that deleting the original question (or altering it to meaningless text) is never appropriate on this forum. The reality is even if every student followed the professor's instructions, there would still be a disparity ... all those students who happened to see the post before the deletion would benefit, not just the original poster. There is no practical way to achieve on this forum what your instructor apparently wants (only original student and non-student posters can see the original post and answers).
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
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.

Categories

Find more on Resizing and Reshaping Matrices 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!