if else条件未给出所需结果

  • 本文关键字:结果 else 条件 if matlab
  • 更新时间 :
  • 英文 :


我写了这个代码,它检测救护车并在警笛百分比上执行if条件,如果percnt>0.5,并在if条件的else部分执行代码的第2部分。到目前为止,代码运行良好,但我现在想要的是,在执行代码的第2节时,如果percn>0.5,则它应该立即终止第2节并开始执行代码的第1节。有人能帮我怎么做这种情况的吗

%ccc
%load finaltrainednet.mat
%a=arduino('com5','uno');
%cam=webcam('A4tech USB2.0 Camera');
%cam1=webcam(3);
%fs = 8000;
%recorder = audiorecorder(fs,8,1,1);
%recorder1 = audiorecorder(fs,8,1,3);
%%
while true
recordblocking(recorder, 10);
recordblocking(recorder1, 10);
inbuf = getaudiodata(recorder);
inbuf1 = getaudiodata(recorder);
dataout=dataoutput(inbuf,fs);
dataout1=dataoutput(inbuf1,fs);
YTest = classify(net, dataout);
YTest1 = classify(net, dataout1);
count = sum(YTest(:) == categorical(1));
count1 = sum(YTest1(:) == categorical(1));
length_t = length(YTest);
length_t1 = length(YTest1);
percnt=count/length_t
percnt1=count1/length_t1;
if percnt || percnt1>=0.5 %this condition works on ambulance detection
%%section 1
power = bandpower(inbuf,fs,[0 fs/2]);
if power>0.01 %this condition works on direction
configurePin(a,'D10','DigitalOutput'); %red
configurePin(a,'D12','DigitalOutput'); %yellow
configurePin(a,'D13','DigitalOutput'); %green
writeDigitalPin(a,'D10',0);
writeDigitalPin(a,'D12',1);
writeDigitalPin(a,'D13',0);
pause(8);
writeDigitalPin(a,'D10',1);
writeDigitalPin(a,'D13',1);
writeDigitalPin(a,'D12',1);
else
configurePin(a,'D11','DigitalOutput'); %red
configurePin(a,'D9','DigitalOutput'); %yellow
configurePin(a,'D8','DigitalOutput'); %green
writeDigitalPin(a,'D11',0);
writeDigitalPin(a,'D9',1);
writeDigitalPin(a,'D8',0);
pause(8);
writeDigitalPin(a,'D8',1);
writeDigitalPin(a,'D11',1);
writeDigitalPin(a,'D9',1);
end
else 
%% section 2
i=snapshot(cam1);
I = imcrop(i,[30.5100 31.5100 550.9800 420.9800]);
%i=imread('f1.jpg');
b=I;
numObjects=counting(b);
%%
t=(numObjects*5);
configurePin(a,'D10','DigitalOutput'); %red
configurePin(a,'D12','DigitalOutput'); %yellow
configurePin(a,'D13','DigitalOutput'); %green
writeDigitalPin(a,'D10',1);
pause(0);
writeDigitalPin(a,'D12',1);
pause(0);
writeDigitalPin(a,'D13',1);
pause(0);
writeDigitalPin(a,'D12',0);
pause(1);
writeDigitalPin(a,'D13',0);
writeDigitalPin(a,'D10',0);
writeDigitalPin(a,'D12',1);
pause(t);
writeDigitalPin(a,'D12',0);
writeDigitalPin(a,'D13',1);
writeDigitalPin(a,'D10',1);
pause(1);
writeDigitalPin(a,'D12',1);   
%%
i=snapshot(cam);
I = imcrop(i,[85.5100 70.5100 450.9800 440.9800]);
%i=imread('f1.jpg');
b=I;
numObjects=counting(b);
%%
t2=(numObjects*5);
configurePin(a,'D11','DigitalOutput'); %red
configurePin(a,'D9','DigitalOutput'); %yellow
configurePin(a,'D8','DigitalOutput'); %green
writeDigitalPin(a,'D11',1);
pause(0);
writeDigitalPin(a,'D9',1);
pause(0);
writeDigitalPin(a,'D8',1);
pause(0);
writeDigitalPin(a,'D9',0);
pause(1);
writeDigitalPin(a,'D8',0);
writeDigitalPin(a,'D11',0);
writeDigitalPin(a,'D9',1);
pause(t2);
writeDigitalPin(a,'D9',0);
writeDigitalPin(a,'D12',0);
writeDigitalPin(a,'D8',1);
writeDigitalPin(a,'D11',1);
pause(1);
writeDigitalPin(a,'D9',1);
writeDigitalPin(a,'D12',1);
end
end  

if percnt || percnt1>=0.5几乎总是计算为true,因为它被解释为:

if (percnt is truthy) or ((percnt1 > 0.5) is truthy)

percnt ~= 0时,percnt是真的,几乎总是这样。

如果你想检查两个数字是否满足相同的条件,你需要写两次条件:if percnt >= 0.5 || percnt1 >= 0.5

然后,您可以定义函数以在";第1节";以及";第2节":

function doSection1()
power = bandpower(inbuf,fs,[0 fs/2]);
if power>0.01 %this condition works on direction
configurePin(a,'D10','DigitalOutput'); %red
configurePin(a,'D12','DigitalOutput'); %yellow
configurePin(a,'D13','DigitalOutput'); %green
writeDigitalPin(a,'D10',0);
writeDigitalPin(a,'D12',1);
writeDigitalPin(a,'D13',0);
pause(8);
writeDigitalPin(a,'D10',1);
writeDigitalPin(a,'D13',1);
writeDigitalPin(a,'D12',1);
else
configurePin(a,'D11','DigitalOutput'); %red
configurePin(a,'D9','DigitalOutput'); %yellow
configurePin(a,'D8','DigitalOutput'); %green
writeDigitalPin(a,'D11',0);
writeDigitalPin(a,'D9',1);
writeDigitalPin(a,'D8',0);
pause(8);
writeDigitalPin(a,'D8',1);
writeDigitalPin(a,'D11',1);
writeDigitalPin(a,'D9',1);
end
end
function doSection2() 
...
end

然后调用代码中的函数:

if percnt >= 0.5 || percnt1 >= 0.5
doSection1();
else
doSection2();
end

在运行时,并没有一个好的方法来停止doSection2()的执行,除非在函数内部添加检查以检查条件,如果条件为true,则添加doSection1()。但是,您可以决定在它完成后不再调用它。请注意,pause(1)行会让MATLAB等待一秒钟。也许你可以在每个pause之后加一张支票,然后打电话给doSection1()

我相信你可以把你的第一个部分放进一个函数中,然后在第2部分中,当percn>=0.5.

有关函数的详细信息:https://www.mathworks.com/help/matlab/matlab_prog/create-functions-in-files.html

您可能还发现break语句很有用:https://www.mathworks.com/help/matlab/ref/break.html

相关内容

  • 没有找到相关文章