How to write the first line of a function?
조회 수: 11(최근 30일)
표시 이전 댓글
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
Can anyone help?
댓글 수: 5
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.
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.
댓글 수: 0
Find more on Get Started with MATLAB 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!