how to prevent user from entering non-numeric values?
Mostrar comentarios más antiguos
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 comentarios
Azzi Abdelmalek
el 16 de Sept. de 2013
amir, You have two flagged questions because you deleted them. You have to restore them
@Amir: I agree with Azzi. When we post a solution, we have to expect that you delete the question again and our work is lost for the community. I recommend not to support such a behavior.
amir
el 16 de Sept. de 2013
Jan
el 16 de Sept. de 2013
Please explain the relation between the title and the code. Do you mean the 4 INPUT commands? Then posting the rest is confusing only.
amir
el 16 de Sept. de 2013
Image Analyst
el 17 de Sept. de 2013
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.
Respuestas (2)
Image Analyst
el 16 de Sept. de 2013
0 votos
Hints: ischar() and isnumeric().
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 comentario
amir
el 17 de Sept. de 2013
Categorías
Más información sobre Logical en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!