Return values from nested if statement in function
Show older comments
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
Accepted Answer
More Answers (1)
Image Analyst
on 13 Apr 2017
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 Comments
Brittany Toohey
on 13 Apr 2017
Image Analyst
on 13 Apr 2017
I might be able to fix it if you tell me what you're passing in for date, pH, pressure_dbar, minimum, maximum.
Brittany Toohey
on 14 Apr 2017
Image Analyst
on 14 Apr 2017
That's not much help. Please give code that generates values for those variables, either by assigning them or by reading in from a file.
Brittany Toohey
on 14 Apr 2017
Image Analyst
on 14 Apr 2017
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
on 14 Apr 2017
Categories
Find more on Platform and License in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!