How to write the first line of a function?

조회 수: 12 (최근 30일)
Mary292
Mary292 2015년 1월 13일
댓글: Image Analyst 2015년 3월 9일
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
James Tursa
James Tursa 2015년 3월 9일
"... 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 2015년 3월 9일
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.

댓글을 달려면 로그인하십시오.

답변 (1개)

Iain
Iain 2015년 1월 13일
편집: Iain 2015년 1월 13일
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.

카테고리

Help CenterFile Exchange에서 Spectral Measurements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by