how do I make the function stop showing output arguments if an invalid input is entered.

2 次查看(过去 30 天)
function [max_h,H_dist]= A09Prob3_baler_erastogi(initial_v,launch_angle)
if ~(initial_v>=9.5 & initial_v<=13.25) %initial velocity limitation [m/s]
fprintf('Initial velocity input is invalid \n')
end
if ~(launch_angle>=30 & launch_angle<=60) % angle limitation [deg]
fprintf('Launch angle input is invalid \n')
end
t= (initial_v*sin(launch_angle))/g; % maximum time [s]
max_h= 1.25+(initial_v*sin(launch_angle)*t)-(0.5*g*t^2); % maximum height in m
H_dist= (initial_v*cos(launch_angle))*t; % horizontal distance in m
end

回答(1 个)

Ameer Hamza
Ameer Hamza 2020-9-28
编辑:Ameer Hamza 2020-9-28
You can use assert(): https://www.mathworks.com/help/matlab/ref/assert.html. Which will throw an error if invalid input is given
function [max_h,H_dist]= A09Prob3_baler_erastogi(initial_v,launch_angle)
assert((initial_v>=9.5 & initial_v<=13.25), 'Initial velocity input is invalid')
assert((launch_angle>=30 & launch_angle<=60), 'Launch angle input is invalid')
t= (initial_v*sin(launch_angle))/g; % maximum time [s]
max_h= 1.25+(initial_v*sin(launch_angle)*t)-(0.5*g*t^2); % maximum height in m
H_dist= (initial_v*cos(launch_angle))*t; % horizontal distance in m
end
If you don't want to throw an error, then you can simply return, but in that case, you will need to provide values for output variables
function [max_h,H_dist]= A09Prob3_baler_erastogi(initial_v,launch_angle)
max_h = 0;
H_dist = 0;
if ~(initial_v>=9.5 & initial_v<=13.25) %initial velocity limitation [m/s]
fprintf('Initial velocity input is invalid \n');
return
end
if ~(launch_angle>=30 & launch_angle<=60) % angle limitation [deg]
fprintf('Launch angle input is invalid \n')
return
end
t= (initial_v*sin(launch_angle))/g; % maximum time [s]
max_h= 1.25+(initial_v*sin(launch_angle)*t)-(0.5*g*t^2); % maximum height in m
H_dist= (initial_v*cos(launch_angle))*t; % horizontal distance in m
end

类别

Help CenterFile Exchange 中查找有关 Develop Apps Using App Designer 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by