How can I correctly use if and elseif?

조회 수: 3(최근 30일)
gblmtc 2018년 3월 22일
댓글: Stephen23 2018년 3월 22일
Hi there! I'm a newbie on the Matlab and I just don't know how to write this function.
I have two inputs : t and toe
I have to calculate
tk = t-toe
if tk > 302400 tf = tk - 604800
if tk < -302400 tf tk + 604800
I created this function :
function [tk] = time(a)
if tk>302400; tf = tk-604800;
elseif tk<-302400; tf = tk+604800;
when I run this function I get this error.
% Undefined function 'time' for input arguments of type 'double'.
what does it mean "input arguments of type double"? what is the double there?
Thank you for your help!
P.S: How can I learn more quickly this program? It seems a bit complicated for me considering I've never learnt programming before. Could I find some exercises to help me learn it?
  댓글 수: 3
Walter Roberson
Walter Roberson 2018년 3월 22일
Notice by the way that used
instead of tk = t-toe
Also, you have not defined toe.

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

채택된 답변

Stalin Samuel
Stalin Samuel 2018년 3월 22일
create a file with below code
function [tf] = time(t,toe)
if tk>302400; tf = tk-604800;
elseif tk<-302400; tf = tk+604800;
while saving the code you must use "time" as file name.
  댓글 수: 2
Stephen23 2018년 3월 22일
편집: Stephen23 2018년 3월 22일
@gblmtc: What sizes are t and toe ?
Why did you accept this answer if it does not do what you want?

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

추가 답변(1개)

Stephen23 2018년 3월 22일
I guess that some of the inputs are non-scalar, in which case you should use indexing:
function tk = time(t,toe)
tk = t-toe;
idx = tk>302400;
tk(idx) = tk(idx)-604800;
idx = tk<-302400;
tk(idx) = tk(idx)+604800;
  댓글 수: 6
Stephen23 2018년 3월 22일
gblmtc's "Answer" moved here:
Yes, you are right! Sometimes it is really really daunting. Sometimes I just say it is not for me and I just can't do it. Thanks for your help!

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

Community Treasure Hunt

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

Start Hunting!

Translated by