Return values from nested if statement in function

2 次查看(过去 30 天)
I'm trying to write a function that will take in the vectors date, pH and depth, the minimum and the maximum depth. It will return new date and pH vectors that only contain the data when the depth was >= the minimum or < the maximum. I've tried a few variations of the code, but everything either doesn't give a result or throws an error. Below is my most recent attempt, but it is only returning the empty vector, which isn't correct. The pH, date and pressure_dbar (which is the depth) variables were imported from a .mat file and work properly. Any tips on why this isn't working properly would be appreciated.
minimum = min(pressure_dbar);
maximum = max(pressure_dbar);
function [newDate, newpH] = oceanpHdepth(date, pH, pressure_dbar, minimum, maximum)
for i = 1:length(date)
for j = 1:length(pH)
if (pressure_dbar >= minimum)
if (pressure_dbar < maximum)
newDate = date(i);
newpH = pH(j);
end
end
end
end
end

采纳的回答

Stephen23
Stephen23 2017-4-14
编辑:Stephen23 2017-4-14
Using loops is a very inefficient way to perform this task: MATLAB is most efficient when operating on complete arrays, for example using vectorized operations or matrix indexing. So it is much better to simply generate the indices once, and then apply them to the complete input vectors, like this:
function [newDate, newpH] = oceanpHdepth(date, pH, pressure_dbar, mnv, mxv)
idx = pressure_dbar >= mnv & pressure_dbar < mxv;
newDate = date(idx);
newpH = pH(idx);
end
PS: the vector idx is called a "logical index", you can read about it here:
  8 个评论
Brittany Toohey
Brittany Toohey 2017-4-14
编辑:Brittany Toohey 2017-4-14
Awesome, thanks heaps. I'd tried that, but keep getting the error: Error using datetime/horzcat (line 1260) All inputs must be datetimes or date/time character vectors or date/time strings.
I plan to try to plot pHsurface against the pressure_dbar corresponding value btw
Stephen23
Stephen23 2017-4-14
编辑:Stephen23 2017-4-14
It is not possible (with some exceptions) to concatenate arrays of different types/classes. You will need to convert them to the same type first, otherwise it should be possible to plot them in one plot call but as separate inputs:
plot(X1,Y1,'x',X2,Y2)
or
plot(X1,Y1)
hold on
plot(X2,Y2)
read the plot help for more info on these.

请先登录,再进行评论。

更多回答(1 个)

Image Analyst
Image Analyst 2017-4-13
This looks weird to me:
for i = length(date)
First of all, date() is a built in function, normally, but you destoryed it by passing in a variable for it. I'm not sure what data is , but the length of it is a single number. Let's say it's 9 for the sake of illustration. So you have
for i = 9
Now, that's not in the normal form of "for i = start:step:stop", though you can do it. So the loop will execute only once with a value of 9 for i.
So then if you every get to the line
newDate = date(i);
it will take the 9th element of date, which is the last element since i was just the length of date. Seems weird. But then you do
newpH = pH(i);
So now you're taking the 9th element of pH - I don't even know if it has 9 elements. But whatever, this code will not do any kind of looping while changing the value of i. It will execute just one with i having one value (whatever the length of date is).
Even if you did loop and change i, you'd be continually overwriting the same value for newDate and newpH because you're not indexing those variables - they're single element scalars, not multiple element arrays.
I'm not sure how to fix it because I don't know what values date and pH have coming in, nor do I know exactly what you want to do.
  7 个评论
Image Analyst
Image Analyst 2017-4-14
Since you're unwilling to give code for reading in the .mat file and unwilling to attach the file like I asked to make it easy for me to help you, I think I'll just have to let my original answer stand. Good luck though. Perhaps someone else will help.
Brittany Toohey
Brittany Toohey 2017-4-14
I'm not unwilling, it just seemed like a waste of time to you. The reading in is just load('HOTSdata.mat'); which works for every other function I use it on, so it has nothing to do with that. And you didn't once ask me to attach any file. But anyway, you didn't sound like you had a clue what I'm trying to do anyway, so whatever. Thanks anyway.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Platform and License 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by