필터 지우기
필터 지우기

how to prevent user from entering non-numeric values?

조회 수: 3 (최근 30일)
amir
amir 2013년 9월 16일
t = 0:0.0001:0.07;
v0 = input('please Enter the value for v0:');
R = input('please Enter the value for resistance:');
C = input('please Enter the value for capacitacne:');
f = input('please Enter the value for frequency:');
m = length(t);
state = 'on';
for A = 1:m
vs(A) = v0 * sin(2*pi*f*t(A));
switch state
case 'on'
vR(A) = vs(A);
iR = vR(A)/R;
iC = 2*pi*f*C*v0*cos(2*pi*f*t(A));
I = iC + iR;
if I<=0
state = 'off';
tA = t(A);
end
case 'off'
vR(A) = v0*sin(2*pi*f*tA)*exp(-(t(A)-tA)/(R * C));
if vs(A) >= vR(A)
state = 'on';
end
end
end
plot (t,vs,':',t,vR,'k')
thank you...
  댓글 수: 6
amir
amir 2013년 9월 16일
yes,i mean the 4 inputs...this is what i got so far .
Image Analyst
Image Analyst 2013년 9월 17일
I noticed you editied it but didn't get it quite right. See this link : http://www.mathworks.com/matlabcentral/answers/13205-tutorial-how-to-format-your-question-with-markup. Basically you don't need to double space your code. all you need to do is to make sure there's a blank line in front of it, then highlight it and click the {}Code button.

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

답변 (2개)

Image Analyst
Image Analyst 2013년 9월 16일
Hints: ischar() and isnumeric().

Jan
Jan 2013년 9월 16일
편집: Jan 2013년 9월 16일
v0S = input('please Enter the value for v0:', 's');
v0 = sscanf(v0S, '%g', 1);
if isempty(v0)
error('Invalid value for v0');
end
etc.
Of course you cannot prevent, that the user provides non-numeric values, but you can catch this exception.
  댓글 수: 1
amir
amir 2013년 9월 17일
thanks for help,can you give a brief explanation about what you wrote here...

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

카테고리

Help CenterFile Exchange에서 Time Series Objects에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by