How to solve a problem in a user friendly program?

1 次查看(过去 30 天)
Dear all, I have a problem with the folllowing user friendly program.
%AVkam=[1;2;3;4;5;6;7;8] %m=number of averages %n=total number of averages a=size(AVkam);
n=a(1,1)
m=input('Hello, enter the number of averages : ');
if m<=0 display('error, enter again a number of averages')
elseif m>n
display('error, enter again a number of averages')
elseif m<=n
b=n/m;
%%%%%%%%%make the smaller integer%%%%%
c=floor(b)
nurow=n-c*m
NewAVkam=AVkam(1:n-nurow,:)
Result=(1:c)
end
for i=1:c
nvk(i,1)=sum(NewAVkam(1+(i-1)*m:m*i,1),1)./m;
end
nvk
Problem: when I enter a negative value or a value which is higher than n, i get this message Undefined function or variable 'c'.
Undefined function or variable 'c'.
Error in tesuser1 (line 44) for i=1:c
What I want in my program is that the user re enter a value till this value is lower than n.Do you kow how can I solve this problem?
Thanks in advance

采纳的回答

Jos (10584)
Jos (10584) 2013-12-10
Use a WHILE loop. This example may get you started.
isOK = false ;
while ~isOK
A = input('Give a number: ')
if A < 0
disp('Error: it should be larger than zero')
elseif A >= 10
disp('Error: it should be smaller than 10')
else
isOK = true ;
end
end
disp(A)

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by