Hi all,
I need to ensure a value is within a range. I understand there's a function called mustBeInRange, but I need to check for multiple conditions. I'd ideally like a message to spit out a specific error that says something like "error, value is not within the range for the third case." I wrote the following but it says that I have too many arguments in mustBeInRange, but it individually works when I put the mustBeInRange line in the command window.
n_roof = 10;
int_H = [10 13 6]; % Case 1, case 2, case 3
n_floor = n_roof - int_H;
n_ins = -1; %Setting it to test the mustBeInRange function
if mustBeInRange(n_ins,n_floor(1),n_roof)
else disp('error, installation height is below floor or above roof elevation for case 1')
if mustBeInRange(n_ins,n_floor(2),n_roof)
else disp('error, installation height is below floor or above roof elevation for case 2')
if mustBeInRange(n_ins,n_floor(3),n_roof)
else disp('error, installation height is below floor or above roof elevation for case 3')
end
end
end

 채택된 답변

Dyuman Joshi
Dyuman Joshi 2023년 4월 27일
편집: Dyuman Joshi 2023년 4월 27일

0 개 추천

The function mustBeInRange itself throws an error if the value is not inside the range
n_roof = 10;
int_H = [10 13 6]; % Case 1, case 2, case 3
n_floor = n_roof - int_H;
n_ins = -1; %Setting it to test the mustBeInRange function
mustBeInRange(n_ins,n_floor(1),n_roof)
%For some reason, I am unable to run the code in live editor here
%Edit - I have attached a ss of the code run on MATLAB App, where you can
%see the error mustBeInRange() throws
If you want to show a specific/particular message for each condition, you will have to use logical operators.
%Define a function handle to use for each case
fun = @(val, in1, in2) (val >= min(in1,in2)) && (val <= max(in1,in2)) ;
if ~fun(n_ins,n_floor(1),n_roof)
disp('error, installation height is below floor or above roof elevation for case 1')
if ~fun(n_ins,n_floor(2),n_roof)
disp('error, installation height is below floor or above roof elevation for case 2')
if ~fun(n_ins,n_floor(3),n_roof)
disp('error, installation height is below floor or above roof elevation for case 3')
end
end
end

댓글 수: 5

Les Beckham
Les Beckham 2023년 4월 27일
편집: Les Beckham 2023년 4월 27일
You are correct about using logical comparisons instead of the mustBeInRange function which is "designed to be used for property and function argument validation" (according to the documentation). However, I believe the OP wants no error message if the value matches any of the three conditions. This approach will display at least two error messages, even if one of the three conditions is met.
Joy Shen
Joy Shen 2023년 4월 27일
편집: Joy Shen 2023년 4월 27일
What's the best way to turn n_ins to a NaN value so it cannot proceed for that specific case?
Joy Shen
Joy Shen 2023년 4월 27일
@Les Beckham Hi Les, I'm fine with individual error messages popping up which will let me isolate which case the condition isn't being met with. I want to also be able to stop the code for the particular case if the conditions aren't met
Dyuman Joshi
Dyuman Joshi 2023년 4월 27일
편집: Dyuman Joshi 2023년 4월 27일
You can directly use NaN as input to the function handle or assign n_ins as NaN. But note that the function handle will return 0 with NaN irrespective of the limits.
%Direct
if ~fun(NaN,n_floor(1),n_roof)
%Assign n_ins
n_ins = NaN;
if ~fun(n_ins,n_floor(1),n_roof)
"I want to also be able to stop the code for the particular case if the conditions aren't met"
So, if conditions for case 1 isn't met, what should the code do next? Display the error and don't check the rest of the conditions?
However you do not want to emit any message if it fits one of the cases. So
m(1) = fun(n_ins,n_floor(1),n_roof)
m(2) = etc
if ~any(m)
use the m values to figure out the error message
end

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Scripts에 대해 자세히 알아보기

제품

질문:

2023년 4월 27일

편집:

2023년 4월 27일

Community Treasure Hunt

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

Start Hunting!

Translated by