Differences between script and function file
조회 수: 14 (최근 30일)
이전 댓글 표시
Does script file and function file relate to one another when comes to coding? or it's two different file? I saw alot of tutorial but i still do not understand.
댓글 수: 0
답변 (2개)
Jürgen
2012년 9월 18일
편집: Jürgen
2012년 9월 18일
Hi,
the coding between script file and function is the same, only the header is different
let's say I make in a script I want to calculate the mean of of some numbers
A= [ 1 2 3 4 5 6]
mean1= (1+2/2) mean2=(3+4/2) mean3=(5+6)/2
instead of writing and copying nearly the same code use a function that you create:( this just a simple example there is an inbuilt function for it of course) you start the function always with the header function .....
function [Mean]= CalcMean(Input)
Mean=( sum(Input)/size(Input,2))
rJ
Isktaine
2012년 9월 18일
편집: Isktaine
2012년 9월 18일
A script file will run one line after another, and all the information is available to your workspace in MATLAB. For example if you run the script
%some saved script
A=46;
B=49;
C=A-B;
You will now have the variables A,B and C in your workspace. All you can do is run the script file and replay those 3 lines over and over. If you wrote a function
function C=pointless_task(D,E)
C=D-E;
end
and click the run button then nothing will happen if D and E have not been defined. However if you save the function then go to your MATLAB command line and write C=pointless_task(46,49) you will get a value for C in your workspace. It will have put your D and E into the function and not saved them. With the function you can effectively run the same script over and over with different values. You could write P=pointless_task(13,15) and get an instant answer rather than writing a whole new script and running it.
So you can have one or the other. Hope this makes sense.
댓글 수: 0
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!