matlab - Undefined function or variable vector -
i'm using matlab , have loaded file contains variables date, ph , pressure_dbar (all vectors). i'm trying write function take in these variables, maximum , minimum pressure_dbar variables , return 2 new vectors: newdate , newph. want populate new vectors date , ph data if date , ph >= minimum , < maximum. below code, getting error "undefined function or variable" on newdate , newph. tried defining them outside of variable newdate = []; , newph = []; unsuccessful. tried different ways of iterating through vector, nothing. tips appreciated, thanks!
minimum = min(pressure_dbar); maximum = max(pressure_dbar); function [newdate, newph] = oceanphdepth(date, ph, pressure_dbar, minimum, maximum) = 1:length(date) j = 1:length(ph) if (ge(pressure_dbar, minimum) && lt(pressure_dbar, maximum)) newdate = date(i); newph = ph(j); end end end end
the error due inside of loop never being reached, , therefore newdate
, newph
never defined inside of function. happening because aren't using i
, j
indices access single element of pressure_dbar
, instead comparing entire array every time bound have some false
values , if
statement evaluate false.
really, should be
if (ge(pressure_dbar(i), minimum) && lt(pressure_dbar(i), maximum))
also, aren't storing results of internal loop array @ since overwrite values newph
, newdate
every time. second, you're better off using logical indexing generate newdate
, newph
function [newdate, newph] = oceanphdepth(date, ph, pressure_dbar, minimum, maximum) mask = pressure_dbar >= minimum & pressure_dbar < maximum; newdate = date(mask); newph = ph(mask); end
Comments
Post a Comment