How can i display once a warning?

5 ビュー (過去 30 日間)
Le Dung
Le Dung 2017 年 2 月 10 日
コメント済み: Le Dung 2017 年 2 月 14 日
Hi. I have a code as below:
x=input('Please enter value of x: ');
y=input('Please enter value of y: ');
z=input('Please enter value of z: ');
V={'x','y','z'};
Ktra=[x y z];
for i=1:numel(Ktra)
if Ktra(i)<0
disp('Value enter into must positive');
fprintf('Value of %s is negative \n',V{i});
end
end
So, If i enter x = -9 and y = -6, program will give:
Value enter into must positive
Value of x is negative
Value enter into must positive
Value of y is negative
And you can see, we have two warnings: "Value enter into must positive". It is unfavorable.
So, how can i do to display that warning once?
I mean that no matter how much I enter, it is just given a warning message. As below:
Value enter into must positive
Value of x is negative
Value of y is negative
Thank you so much!

採用された回答

Stephen23
Stephen23 2017 年 2 月 10 日
編集済み: Stephen23 2017 年 2 月 10 日
N = {'x','y','z'};
V = nan(size(N));
for k = 1:numel(V)
str = sprintf('Please enter value of %s: ',N{k});
V(k) = str2double(input(str,'s'));
end
if any(V<0)
fprintf('Value enter into must positive\n')
end
for k = reshape(find(V<0),1,[])
fprintf('Value of %s is negative\n',N{k});
end
and tested:
Please enter value of x: -6
Please enter value of y: -9
Please enter value of z: 4
Value enter into must positive
Value of x is negative
Value of y is negative
Note that, as you were told in your last question, it is faster and safer to call input with its second 's' option, which prevents input from evaluating anything that the user might supply. Using this option is highly recommended.
  4 件のコメント
Le Dung
Le Dung 2017 年 2 月 14 日
編集済み: Le Dung 2017 年 2 月 14 日
Yes. Dear Stephen. I have a code that i used many "for" loops to solve a problem that to find minimum value of a function of several variables. Could you show me your email to i sent to you my code. And could you check it and show me why my code costs too much time? I runs it from 5 PM to 8.30 PM. But it still hasn't finished yet. This is my code:
% GIOI THIEU
% Chuong trinh tinh toan toi uu hoa mat cat dam ban BTCT
% Tac gia: Le Tuan Dung
% Bo mon Cong trinh - Co so Thai Nguyen - DHCNGTVT
%-------------------*************--------------------------
% PHAN1: THAM SO DAU VAO CUA BAI TOAN
% 1.1. Cac gia tri trong luong rieng:
% Trong luong rieng be tong dam va ban
gamabt=25; % Don vi: kN/m3
% Trong luong rieng lop phu
gamalp=22.5; % Don vi: kN/m3
% 1.2. Cuong do cua vat lieu
% Cuong do be tong dam (f'c)
fcdam=30; % Don vi: Mpa
% Cuong do be tong ban (f'c)
fcban=30; % Don vi: Mpa
% Cuong do cot thep DUL (dieu 5.9.3)
fpu=1860; % Cuong do keo dut (Mpa)
fpy=0.9*fpu; % Cuong do chay deo (Mpa)
fpe=0.8*fpy; % Cuong do huu hieu (sau khi tru toan bo mat mat US) (Mpa)
% Modun dan hoi cua thep DUL (dieu 5.4.4.2)
Eps=197000; % (Mpa)
% 1.3. Tham so mat cat ngang cau:
Bcau=12; % % Be rong MCN cau (m)
hbbien=0.1; % Chieu day ban lien ket ben ngoai (m)
hbtrong=0.21; % Chieu day ban len ket ben trong (m)
Sodam=12; % Cau co 12 dam chu
daylp=0.08; % Chieu day lop phu mat cau (m)
Aclc=0.231; % Dien tich chan lan can
mlc=0.04; % Khoi luong/chieu dai cua lan can (kg/m)
gtt=9.81; % Gia toc trong truong (m/s2)
Ldam=24; % Chieu dai dam (m)
agoi=0.35; % Dau dam cach goi cau (m)
% 1.4. Cac he so tinh toan mo men:
neta=0.95; % He so dieu chinh
gamatt1=1.25; % He so tai trong tinh tai giai doan 1
gamatt2=1.5; % He so tai trong tinh tai giai doan 2
gamall=1.75; % He so tai trong cua hoat tai
IM=0.25; % He so xung kich
phif=1.0; % He so suc khang uon
% 1.5. Tai trong Hoat tai:
Ptruck=[145 145 35]; % (kN)
Kctruck=4.3; % (m)
Ptandem=[110 110]; % (kN)
Kctandem=1.2; % (m)
qlan=9.3; % (kN/m)
% 1.6. Tham so kich thuoc cua mat cat ngang:
disp('MOI CAC BAN NHAP CAC THAM SO DAU BAI');
B1=input('Gia tri can duoi be rong B1(cm): ');
B2=input('Gia tri can tren be rong B2(cm): ');
bs1=input('Gia tri can duoi be rong suon bien bs1(cm): ');
bs2=input('Gia tri can tren be rong suon bien bs2(cm): ');
bsg1=input('Gia tri can duoi be rong suon giua bsg1(cm): ');
bsg2=input('Gia tri can tren be rong suon giua bsg2(cm): ');
H1=input('Gia tri can duoi chieu cao H1(cm): ');
H2=input('Gia tri can tren chieu cao H2(cm): ');
hnt1=input('Gia tri can duoi chieu cao nap tren hnt1(cm): ');
hnt2=input('Gia tri can tren chieu cao nap tren hnt2(cm): ');
hnd1=input('Gia tri can duoi chieu cao nap duoi hnd1(cm): ');
hnd2=input('Gia tri can tren chieu cao nap duoi hnd2(cm): ');
Bslk=input('Gia tri chieu rong ban be tong lien ket Bslk(cm): '); % Bs= 1(m)
hslk=input('Gia tri chieu day ban be tong lien ket hslk(cm): ');
ntao1=input('Gia tri can duoi cua so tao cap ntao1: ');
ntao2=input('Gia tri can tren cua so tao cap ntao2: ');
dtao=input('Nhap duong kinh cua mot tao thep DUL dtao(mm): ');
B=(B1:B2); % Be rong MCN (0,5 - 1,0 met)
bs=(bs1:bs2); % Be rong suon bien (0,1 - 0,25 met)
bsg=(bsg1:bsg2); % Be rong suon giua (0,15 - 0,25 met)
H=(H1:H2); % Chieu cao MCN (0,5 - 1,0 met)
hnt=(hnt1:hnt2); % Chieu day ban tren (0,1 - 0,2 met)
hnd=(hnd1:hnd2); % Chieu day ban duoi (0,15 - 0,3 met)
ntao=(ntao1:ntao2); % So tao cap DUL (20 - 40 tao cap)
% Kiem tra tham so nhap vao:
Ktra=[B1 B2 bs1 bs2 bsg1 bsg2 H1 H2 hnt1 hnt2 hnd1 hnd2 Bslk hslk ntao dtao];
V={'B1','B2','bs1','bs2','bsg1','bsg2','H1','H2','hnt1','hnt2','hnd1','hnd2','Bs','hs','ntao','dtao'};
if find(Ktra<0)>0
disp('GIA TRI NHAP PHAI LA SO DUONG');
end
for i=1:numel(Ktra)
if Ktra(i)<0
%Cach bieu dien ten bien so
fprintf('Nhap lai gia tri cua: %s \n',V{i});
end
end
% PHAN 2: TOI UU HOA MAT CAT
% 2.1. Xac dinh tai trong tinh tai:
% Trong luong phan ban lien ket:
qlk=(2*(hbbien+hbtrong)*Bcau/2)/(2*Sodam); % Don vi: kN/m
% Tai trong do trong luong lop phu mat cau
qlp=(Bcau*daylp*gamalp)/Sodam;
% Tai trong do trong luong phan lan can
qlc=Aclc*gamabt+mlc*gtt;
% 2.2. Xac dinh cac tai trong do hoat tai:
% Chieu dai nhip tinh toan
Ldamtt=Ldam-2*agoi;
% Gia tri tung do duong anh huong:
y1=Ldamtt/4;
y2=((Ldamtt/2-4.3)*y1)/(Ldamtt/2);
y3=((Ldamtt/2-8.6)*y1)/(Ldamtt/2);
y4=((Ldamtt/2-1.2)*y1)/(Ldamtt/2);
Mrmin=Inf;
imin=1;
jmin=1;
kmin=1;
zmin=1;
lmin=1;
mmin=1;
nmin=1;
for i=1:numel(B)
for j=1:numel(bs)
for k=1:numel(bsg)
for z=1:numel(H)
for l=1:numel(hnt)
for m=1:numel(hnd)
for n=1:numel(ntao)
% Be rong cua lo rong:
br=((B(i)-2*bs(j)-bsg(k))/2)*0.01; % (m)
% Chieu cao phan lo rong
hr=(H(z)-hnt(l)-hnd(m))*0.01; % (m)
% TINH TOÁN MAT CAT QUY DOI
% Chieu cao canh tren:
ht=hnt(l)*B(i)/B(i)*0.01; % (m)
% Chieu cao canh duoi:
hd=hnd(m)*B(i)/B(i)*0.01; % (m)
% Chieu cao phan suon dam:
hsd=(H(z)-ht-hd)*0.01; % (m)
% Be rong phan suon dam:
bsd=((2*bs(j)*hr+bsg(k)*hr)/hsd)*0.01; % (m)
% Tai trong do trong luong ban than dam chu:
qdc=(B(i)*0.01*hd+bsd*hsd+B(i)*001*ht)*gamabt; % (kN/m)
% TINH TOAN GIA TRI MOMEN UON
% Momen uon do tinh tai:
Mttai=neta*(gamatt1*(qdc*Ldamtt*Ldamtt+qlk*Ldamtt*Ldamtt+...
qlc*Ldamtt*Ldamtt)+gamatt2*(qlp*Ldamtt*Ldamtt))/8;
% Tinh he so phan bo ngang dam giua:
K=2.5*power(Sodam,-0.2);
de=0;
e=1.04+de/7600;
gg1lan=K*power((B(i)*0.01)/(2.8*Ldamtt),0.5)*power(1,0.25); % He so pbn 1 lan dam giua
gg2lan=K*power(B(i)*10/7600,0.6)*power((B(i)*0.01)/Ldamtt,0.2)*power(1,006); % He so pbn 2 lan dam giua
% Tinh he so phan bo ngang dam bien:
gb1lan=0.2; % Tinh theo PP don bay
gb2lan=e*gg2lan;
glay=[gg1lan gg2lan gb1lan gb2lan];
% He so phan bo ngang de tinh toan:
gpbn=max(glay);
% Momen uon do xe tai thiet ke:
Mtruck=neta*gpbn*(1+IM)*gamall*(Ptruck(1)*y1+Ptruck(2)*y2+Ptruck(3)*y3); % kN.m
% Momen uon do xe 2 truc:
M2truc=neta*gpbn*(1+IM)*gamall*(Ptandem(1)*y1+Ptandem(2)*y4); % kN.m
Mxetk=[Mtruck M2truc]; % kN.m
% Momen uon do tai trong lan xe
Mlanxe=neta*gpbn*gamall*9.3*0.5*(1/4)*Ldamtt*Ldamtt; % kN.m
% Momen uon do hoat tai
Mhtai=max(Mxetk)+Mlanxe; % kN.m
% Momen uon su dung tinh toan
Mutt=Mttai+Mhtai;
% TINH TOAN DAC TRUNG HINH HOC MAT CAT QUY DOI
% Dien tich mat cat ngang:
Aqd=B(i)*0.01*hd+bsd*hsd+B(i)*0.01*ht; %(m2)
% Momen tinh cua mat cat so voi day mat cat:
Sqd=(1/2)*B(i)*0.01*hd*hd+(0.5*hsd+hd)*hsd*bsd+(H(z)*0.01-0.5*ht)*ht*B(i)*0.01; % (m3)
% K/cach tu trong tam cua tiet dien quy doi toi day
% mat cat:
yd=Sqd/Aqd; %(m)
% K/cach tu tho tren cung toi truc trung hoa:
yt=H(z)*0.01-yd; %(m)
% Momen quan tinh cua mat cat doi voi truc trung
% hoa:
Jqd=(B(i)*0.01*ht*ht*ht)/12+power((yt-0.5*ht),2)*ht*(B(i)*0.01)+bsd*power(hsd,3)/12+...
power((yt-ht-hsd/2),2)*hsd*bsd+(B(i)*0.01)*power(hd,3)/12+power((yd-0.5*hd),2)*hd*(B(i)*0.01);
% TINH TOAN DAC TRUNG HINH HOC MAT CAT LIEN HOP
% Modun dan hoi dam:
Edam=0.043*power(gamabt*100,1.5)*sqrt(fcdam); % Mpa
% Modun dan hoi dam:
Eban=0.043*power(gamabt*100,1.5)*sqrt(fcban); % Mpa
% He so quy doi tu be tong ban sang be tong dam:
n1=Eban/Edam;
% Dien tich mat cat ngang:
Alh=Aqd+n1*Bslk*0.01*hslk*0.01; %(m2)
% Mome tinh cua mat cat so voi day mat cat:
Slh=Sqd+n1*Bslk*0.01*hslk*0.01*(H(z)*0.01+0.5*hslk*0.01); % (m3)
% K/cach tu trong tam cua tiet dien lien hop toi
% day ban:
ydlh=Slh/Alh; % (m)
% K/cach tu tho tren cung toi truc trung hoa:
ytlh=H(z)*0.01+hslk*0.01-ydlh; % (m)
% Momen quan tinh cua mat cat lien hop:
Jlh=(B(i)*0.01)*power(ht,3)/12+power((ytlh-hslk-0.5*ht),2)*ht*(B(i)*0.01)...
+bsd*power(hsd,3)/12+power((ytlh-hslk*0.01-ht-0.5*hsd),2)*hsd*bsd+...
(B(i)*0.01)*power(hd,3)/12+power((ydlh-0.5*hd),2)*hd*(B(i)*0.01)+...
n1*(Bslk*0.01*power(hslk*0.01,3)/12+Bslk*0.01*hslk*0.01*power((ytlh-0.5*hslk*0.01),2)); % (m4)
% TINH TOAN DAC TRUNG HINH HOC MAT CAT CO COT THEP
% He so quy doi tu cot thep DUL sang be tong dam:
n2=Eps/Edam;
% Dien tich mat cat ngang:
Act=Alh+(n2-1)*pi*dtao*0.001*dtao*0.001*ntao(n)*(1/4); %(m2)
% K/cach tu tim hang cot thep ngoai cung toi tho
% duoi:
ctduoi=0.06; % (m)
% K/cach tim toi tim giua cac hang cot thep:
timct=0.045; % (m)
% K/cach tu trong tam cot thep toi day mat cat:
dpsduoi=ctduoi+timct/2; %(m)
% K/cach tu trong tam dien tich cot thep toi tho tren cung cua MC:
dps=H(z)*0.01+hslk*0.01-dpsduoi;
% Momen tinh cua mat cat so voi day mat cat:
Sct=Slh+(n2-1)*pi*dtao*0.001*dtao*0.001*ntao(n)*(1/4)*(H(z)*0.01+hslk*0.01-dps); % (m3)
% K/cach tu trong tam tiet dien lien hop toi day MC
ydct=Sct/Act; %(m)
% K/cach tu tho tren cung toi TTH:
ytct=H(z)*0.01+hslk*0.01-ydct; % (m)
% Momen quan tinh cua mat cat co cot thep:
Jct=(B(i)*0.01)*power(ht,3)/12+power((ytct-hslk*0.01-ht/2),2)*ht*(B(i)*0.01)+...
bsd*power(hsd,3)/12+power((ytct-hslk*0.01-ht-hsd/2),2)*hsd*bsd+...
(B(i)*0.01)*power(hd,3)/12+power((ydct-hd/2),2)*hd*B(i)*0.01+...
n1*(Bslk*0.01*power(hslk*0.01,3)/12+Bslk*0.01*hslk*0.01*power((ytct-(hslk*0.01)/2),2))+...
(n2-1)*pi*dtao*0.001*dtao*0.001*(1/4)*ntao(n)*power((ydct-dpsduoi),2); % (m4)
% XAC DINH SUC KHANG UON CUA MAT CAT NGANG
% He so quy quy khoi ung suat chu nhat:
if fcdam <= 28
beta1=0.85;
elseif 28<fcdam<56
beta1=0.85-0.05*(fcdam-28)/7;
else
beta1=0.65;
end
% Chieu cao khoi ung suat hinh chu nhat:
acn=beta1*ytct; %(m)
if ytct>(ht+hslk*0.01)
% Suc khang uon danh dinh:
Mn=pi*dtao*0.001*dtao*0.001*(1/4)*ntao(n)*fpe*1000*(dps-acn/2)+...
0.85*fcdam*1000*(B(i)*0.01-bsd)*ht*(acn/2-ht/2)+0.85*fcdam*1000*(Bslk*0.01-bsd)*hslk*0.01*(acn/2-(hslk*0.01)/2); % kN.m
else
Mn=pi*dtao*0.001*dtao*0.001/4*ntao(n)*fpe*1000*(dps-acn/2)+...
0.85*fcdam*1000*(Bslk*0.01-B(i)*0.01)*hslk*0.01*(acn/2-(hslk*0.01)/2); % kN.m
end
% Suc khang uon tinh toan:
Mr=phif*Mn; %kN.m
if (Mr<=Mrmin)&&(Mr>=Mutt)
Mrmin=Mr;
imin=i;
jmin=j;
kmin=k;
zmin=z;
lmin=l;
mmin=m;
nmin=n;
end
end
end
end
end
end
end
end
if ytct>(ht+hslk*0.01)
disp('TRUC TRUNG HOA DI QUA SUON DAM');
else
disp('TRUC TRUNG HOA DI QUA BAN CANH TREN');
end
% CAC GIA TRI KICH THUOC MAT CAT NGANG VA SO TAO THEP TOI UU
disp(Mrmin);
disp(B(imin));
disp(bs(jmin));
disp(bsg(kmin));
disp(H(zmin));
disp(hnd(lmin));
disp(hnt(mmin));
disp(ntao(nmin));
Le Dung
Le Dung 2017 年 2 月 14 日
And this is value of variables that have to enter:
Gia tri can duoi be rong B1(cm): 50
Gia tri can tren be rong B2(cm): 100
Gia tri can duoi be rong suon bien bs1(cm): 10
Gia tri can tren be rong suon bien bs2(cm): 25
Gia tri can duoi be rong suon giua bsg1(cm): 10
Gia tri can tren be rong suon giua bsg2(cm): 25
Gia tri can duoi chieu cao H1(cm): 50
Gia tri can tren chieu cao H2(cm): 100
Gia tri can duoi chieu cao nap tren hnt1(cm): 10
Gia tri can tren chieu cao nap tren hnt2(cm): 20
Gia tri can duoi chieu cao nap duoi hnd1(cm): 15
Gia tri can tren chieu cao nap duoi hnd2(cm): 30
Gia tri chieu rong ban be tong lien ket Bslk(cm): 100
Gia tri chieu day ban be tong lien ket hslk(cm): 10
Gia tri can duoi cua so tao cap ntao1: 20
Gia tri can tren cua so tao cap ntao2: 40
Nhap duong kinh cua mot tao thep DUL dtao(mm): 12.7

サインインしてコメントする。

その他の回答 (2 件)

Image Analyst
Image Analyst 2017 年 2 月 10 日
I'd recommend a more in-your-face approach using errordlg():
errorMessage = sprintf('You must enter a positive value.\nYou entered %f, which is negative.\nPlease try again.', Ktra(i));
uiwait(errordlg(errorMessage));

Image Analyst
Image Analyst 2017 年 2 月 11 日
編集済み: Image Analyst 2017 年 2 月 11 日
I prefer a more user friendly way to ask the user for numbers, like this:
% Ask user for three floating point numbers.
defaultValue = {'10', '20', '30'};
titleBar = 'Enter a value';
userPrompt = {'Enter x: ', 'Enter y: ', 'Enter z: '};
caUserInput = inputdlg(userPrompt, titleBar, 1, defaultValue);
if isempty(caUserInput),return,end; % Bail out if they clicked Cancel.
% Convert to floating point from string.
usersValue1 = str2double(caUserInput{1})
usersValue2 = str2double(caUserInput{2})
usersValue3 = str2double(caUserInput{3})
% Check usersValue1 for validity.
if isnan(usersValue1)
% They didn't enter a number.
% They clicked Cancel, or entered a character, symbols, or something else not allowed.
% Convert the default from a string and stick that into usersValue1.
usersValue1 = str2double(defaultValue{1});
message = sprintf('I said it had to be a number.\nTry replacing the user.\nI will use %.2f and continue.', usersValue1);
uiwait(warndlg(message));
end
% Do the same for usersValue2
% Check usersValue1 for validity.
if isnan(usersValue2)
% They didn't enter a number.
% They clicked Cancel, or entered a character, symbols, or something else not allowed.
% Convert the default from a string and stick that into usersValue2.
usersValue2 = str2double(defaultValue{2});
message = sprintf('I said it had to be a number.\nTry replacing the user.\nI will use %.2f and continue.', usersValue2);
uiwait(warndlg(message));
end
% Check usersValue3 for validity.
if isnan(usersValue3)
% They didn't enter a number.
% They clicked Cancel, or entered a character, symbols, or something else not allowed.
% Convert the default from a string and stick that into usersValue3.
usersValue3 = str2double(defaultValue{3});
message = sprintf('I said it had to be a number.\nTry replacing the user.\nI will use %.2f and continue.', usersValue3);
uiwait(warndlg(message));
end

カテゴリ

Help Center および File ExchangeOrdinary Differential Equations についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by