I need to have an error message for when the user inputs a letter into the a,b,c, or d inputs but im not sure how to do it, ive tried using isnan but could not figure out how to implement it into my code for it to work.

조회 수: 13 (최근 30일)
I need to have an error message for when the user inputs a letter into the a,b,c, or d inputs but im not sure how to do it, ive tried using isnan but could not figure out how to implement it into my code for it to work.
clc clear all close all; %variables% start=1; while start==1 %rerun loop close all %closing plot after loop
%VARIABLE MENU
v=inputdlg({'enter value for a','enter value for b','enter value for c','enter value for d'}, 'Constant',[1 50; 1 50; 1 50; 1 50]); %input values of constants
a= str2num (v{1});b= str2num (v{2});c= str2num (v{3});d= str2num (v{4});
%VARIABLE MENU
%DOMAIN AND STEP MENU
r=inputdlg({'enter minimum x-value','enter maximum x-value','enter step size'}, 'Domain',[1 50; 1 50; 1 50;]);%input values for graph dimensions
e= str2num (r{1});f= str2num (r{2});g= str2num (r{3});
x= e:g:f; %domain
%DOMAIN AND STEP MENU
%FUNCTIONS
y=a*x+b; %y(x) = ax + b
y2=(a*x.^2)+(b*x)+c; %y=ax^2 + bx + c
y3=(a*x.^3)+(b*x.^2)+(c*x)+d; %y=ax^3+bx^2+cx+d
%FUNCTIONS
%USER INPUT TOLERANCE OF ZERO
tolerance=inputdlg({'Enter lower tolerance, Ex: -0.001','Enter upper tolerance, Ex: 0.001'}, 'Tolerance of zero',[1 50; 1 50]);%lower and upper tolerance of zero
lower= str2num (tolerance{1});upper= str2num (tolerance{2});
%USER INPUT TOLERANCE OF ZERO
%PLOT FUNCTIONS
plot (x,y,'r')%plot function #1 in red
hold on
plot (x,y2,'b')%plot function #2 in blue
hold on
plot (x,y3,'g')%plot function #3 in green
hold on
%PLOT FUNCTIONS END
%FINDING ZEROS START
%ROOT OF 1st FUNCTION
for x1=e:1e-5:f
zero1=([a b]);%function vector
tol=a*x1+b; %evaluating function for every value in domain
if tol>=lower && tol<=upper %grabbing zeros only from user set tolerance
msg{1}=sprintf ('Approximate root for y=%1.3fx+%1.3f is: x= %1.6f \n',a,b,x1);
tol=zero1;
plot (x1,zero1,'rx')%plot root #3 in green x
else
%if no roots exist in domain fucntions ends
end
end
hold on
%ROOT OF 2nd FUNCTION
for x2=e:1e-5:f
zero2=([a b c]); %function vector
tol2=(a*x2.^2)+(b*x2)+c; %evaluating function for every value in domain
if tol2>=lower && tol2<=upper %grabbing zeros only from user set tolerance
msg{2}=sprintf('Approxiate root for y=%1.3fx^2+%1.3fx+%1.3f is: x= %1.6f \n',a,b,c,x2);
tol2=zero2;
plot (x2,zero2,'bx')%plot root #2 in blue x
else
%if no roots exist in domain fucntions ends
end
end
hold on
%ROOT OF 3rd FUNCTION1
for x3=e:1e-5:f
zero3=([a b c d]);%function vector
tol3=(a*x3.^3)+(b*x3.^2)+(c*x3)+d;%evaluating function for every value in domain
if tol3>=lower && tol3<=upper %grabbing zeros only from user set tolerance
tol3=zero3;
plot (x3,zero3,'gx')%plot root #3 in green x
msg{3}=sprintf('Approximate root for y=%1.3fx^3+%1.3fx^2+%1.3fx+%1.3f is: x= %f \n',a,b,c,d,x3);
else
%if no roots exist in domain fucntions ends
end
end
hold on
%FINDING ZEROS END
%ROOTS MESSAGE
msgbox(msg,'roots','help');
%ROOTS END
%GRAPH LEGEND START
xlabel('x');
ylabel('y');
title('Functions and Roots');
leg{1}= sprintf('%1.3fx + %1.3f',a,b);
leg{2}= sprintf('%1.3fx^2 + %1.3fx + %1.3f',a,b,c);
leg{3}= sprintf('%1.3fx^3 + %1.3fx^2 + %1.3fx + %1.3f',a,b,c,d);
legend(leg)
%GRAPH LEGEND END
%RE-RUN MENU
start=menu('Return to start?','Yes','End');
if start==2
close all
end
%RE-RUN MENU
end

답변 (1개)

Eric
Eric 2017년 11월 1일
편집: Eric 2017년 11월 7일
str2num will return empty if it cannot convert, so you would need to use isempty(). Alternatively, there is a second output from str2num, which is a flag that you could check to see if the conversion was successful.
Edit:
For example,
[a, success_a] = str2num('c');
will return an empty a and success_a will be false/0, so you can check using either of the following conditions (only one is necessary):
if isempty(a) || ~success_a
% Do error message like:
error('ERROR: This is a character, not a number!');
end
If you want to keep asking until it's right...
a = str2num(mynum);
while isempty(a) % or if you prefer the success flag, ~success_a
%%%Complain/Error and get new 'a' here %%%
a = str2num(mynum);
end

카테고리

Help CenterFile Exchange에서 Argument Definitions에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by