Can I create function that use default input unless a user gives one?
조회 수: 1 (최근 30일)
이전 댓글 표시
Hi I want to create function that will use current time to calculate the position of satellites. But later on I will need this function to calculate the satellite position at a few second ahead of the current time for example current time is let's say 50 seconds for the sake of simplicity and I want to update it with 2 second later.
Now I want this function to calculate the sat position at t = 50 seconds by defult unless I give it the 2 second and it adds to the first time. Please check the code structure for better understanding of the uestion
function satposit(time)
if nargin<0
t = 50 % just an example only
s = vt % just an example only
else if nargin>=1
t = 50 + time
s = vt
end
end
댓글 수: 0
채택된 답변
Rik
2022년 2월 17일
The code you show is almost exactly what you describe (except for the <0 instead of <1): if the user doesn't provide an input, it will set t to 50, otherwise it will set t to 50+time.
You could have found this error using the debugger to step through your code.
Note that this is a more common way to do this:
function satposit(time)
if nargin<1
time=0; % define the default value of time
end
t = 50 + time;
s = vt;
end
댓글 수: 0
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Satellite Mission Analysis에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!