Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Apply if/else if to each element in array in MATLAB

  1. Mar 30, 2010 #1
    I have an array that is 1 column by many rows, of which each element represents a time. I want to find variable f at each time, where f is defined as follows:

    if t < tp
    if tp < t < ts
    otherwise f=0

    I've tried defining f as above in a .m function file (called elastance), and then using f=arrayfun(elastance,t) to find f at each time, but that doesn't work (It says, "Error in ==> elastance at 5, if t < tp".) Just typing the above function directly into the command window doesn't work either, whether or not I put it in terms of t or (t.).

    Does anyone have any suggestions? Thanks!
  2. jcsd
  3. Mar 30, 2010 #2


    Staff: Mentor

    I see several things that might be causing your problems:
    othewise is used in switch statements, not in if - elseif - else statements.
    Your 2nd if statement was testing a malformed expression (i.e., tp < t < ts). In most programming languages this should be made into two expressions.

    The changes below might fix your problems.

    Code (Text):

    if t < tp
    elseif if tp < t & t < ts
    else f=0
  4. Mar 30, 2010 #3
    Hi, thanks for your reply!

    I did fix those errors, but still can't get it to work and apply to each element in the array. I've done it both just entering this code into the command window, and it only returns one element f=0 instead of f values for each time step in the array... Here's the code I'm using:

    if t < tp
    elseif tp < t & t < ts
    else f=0

    Also tried making a separate function with it and using arrayfun, as follows:

    function f=elastance2(t)

    if t < tp
    elseif tp < t && t < ts
    else f=0;

    and in the command window:

    but I get the following error:

    ??? Input argument "t" is undefined.

    Error in ==> elastance2 at 5
    if t < tp

    t is the array of times; if I type t and enter the array comes up, so I'm not sure why it is undefined. Does someone have any advice?
  5. Mar 31, 2010 #4
    your function works fine when i plug it in but you seem to have some errors?....is f intended to be an output array corresponding to your functions?

    you need to use the . operator for array manipulation and the zeros and size functions for your else condition...

    did you try defining t inside to see if the function works....or the stupid quesiton is if you defined it before calling the function. try using a different letter encase yoru using two different letters.
  6. Apr 1, 2010 #5
    Hi oceanspiral20,
    The following may help you:
    I will define an array for time t, which will be many columns in one row, the opposite to what you said but this is pretty standard. Then using a for loop, we look at each t and check what conditions it satisfies etc. I have had to make up some constants which should be similar to yours. Here's the code:
    clear all
    t = 0:0.01:0.2;
    tp = 0.09;
    ts = 0.15;
    for h = 1:length(t)
    if t(h) <= tp
    elseif t(h) > tp & t(h) <= ts

    I have set tp as less than ts as is evident from your post. The time range is 0 to 0.2 seconds. So, when t is less than tp, it applies the first expression involving sin, when tp < t < ts, it applies the second expression invoving cos and when t is greater than ts (this addresses your 'otherwise' condition) it sets f = 0. Note the use of (h) to indicate looking at individual array elements.
    Just set your values for t, tp and ts and it should work fine.

    This code should do what you want when used independently, however if you are using it within another script you may need to make some adjusments. In other words, this doesn't need any defined functions to work.
    Last edited: Apr 2, 2010
  7. Apr 1, 2010 #6
    Hi Ben H!

    Thank you so much for your reply, that worked beautifully! :)
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook