How can I correctly use if and elseif?

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)
a=t-toe;
if tk>302400; tf = tk-604800;
elseif tk<-302400; tf = tk+604800;
end
end
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 Kommentare

Von Duesenberg
Von Duesenberg am 22 Mär. 2018
The Matlab documentation would be a good place to start: https://fr.mathworks.com/help/matlab/ref/if.html
Notice by the way that used
a=t-toe;
instead of tk = t-toe
Also, you have not defined toe.

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Stalin Samuel
Stalin Samuel am 22 Mär. 2018

1 Stimme

create a file with below code
function [tf] = time(t,toe)
tk=t-toe;
if tk>302400; tf = tk-604800;
elseif tk<-302400; tf = tk+604800;
end
end
while saving the code you must use "time" as file name.

2 Kommentare

gblmtc
gblmtc am 22 Mär. 2018
Thanks Samuel! I applied your function, it works but it calculates just tk=t-toe. It doesn't either add or subtract 604800. Doesn't continue and do the if . Do you know why?
Stephen23
Stephen23 am 22 Mär. 2018
Bearbeitet: Stephen23 am 22 Mär. 2018
@gblmtc: What sizes are t and toe ?
Why did you accept this answer if it does not do what you want?

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Stephen23
Stephen23 am 22 Mär. 2018

0 Stimmen

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;
end

6 Kommentare

gblmtc
gblmtc am 22 Mär. 2018
I apologize! I checked it again and it does work. At the beginning when I saw it didn't work I put unaccepted but didn't know how to change it back. I apologize again!
gblmtc
gblmtc am 22 Mär. 2018
I'm new here and I don't really know how it works.
Stephen23
Stephen23 am 22 Mär. 2018
Bearbeitet: Stephen23 am 22 Mär. 2018
@gblmtc: indexing is a very basic concept that is used a lot in MATLAB. You can learn about indexing by doing the introductory tutorials, which are highly recommended for all beginners:
and also by reading this article:
gblmtc
gblmtc am 22 Mär. 2018
Thanks Stephen! I'll have a look on what you recommend. I need to learn Matlab but being at the very beginning it's quite challenging.
Stephen23
Stephen23 am 22 Mär. 2018
Bearbeitet: Stephen23 am 22 Mär. 2018
@gblmtc: I understand that there is a lot to learn, and it can seem quite daunting, but luckily MATLAB has easy-to-read documentation, which should always be the first place you look for information. You also might like to read this, it has some tips to keep in mind when learning MATLAB:
Stephen23
Stephen23 am 22 Mär. 2018
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!

Melden Sie sich an, um zu kommentieren.

Kategorien

Gefragt:

am 22 Mär. 2018

Kommentiert:

am 22 Mär. 2018

Community Treasure Hunt

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

Start Hunting!

Translated by