how can I fix a problem using the Elseif construct in App designer?

1 回表示 (過去 30 日間)
jacopo del Pero
jacopo del Pero 2019 年 11 月 12 日
hi everybody. i'm trying to develop an app that localize points in a fixed volume. the problem is caused by a button callback that include an "If elseif loop" as written below.
this problem is caused by an update of the positions of sensor. this cause the problem of the wrong localization until i press two times the button "localize" (the code is shown below).
the first part of the cose use 5 sensors, while the second uses 8 sensors and the last one (not shown in the code below) uses 9 sensors. the position of the sensor can change and if I use the 8 and 9 configuration (with changed coordinates of the sensors) i have the problem explained while for the configuration with 5 sensors all the code work perfectly.
i think that the problem is inside the Elseif Operation but i cannot fix it.
any tips for alternative form or to avoid the if else loop?
Cattura.JPG
if isequal(app.UITable.Data, app.cinquesensori) %MOTORE PER 5 SENSORI
puntigriglia=zeros(18491,3);
n=1;
for i=-5:.5:15 % ciclo for che genera coordinate per punti della mia figura
for j=-5:.5:15
for k=-4:.5:1
puntigriglia(n,:)=[i*1 j*1 k*1];
n = n+1;
end
end
end
dist=zeros(5,18491);
for i=1:5
for j=1:18491
dist(i,j) = sqrt((app.cinquesensori(i,1) - puntigriglia(j,1)) ^ 2 + (app.cinquesensori(i,2) - puntigriglia(j, 2)) ^ 2 +(app.cinquesensori(i,3) - puntigriglia(j, 3)) ^ 2);
end
end
tempi_reali = xlsread('5sensori.xlsx'); %importo excel dei tempi reali delle sorgenti ai sensori dati da prof
h=1;
k=1;
ritardi_reali=zeros(10,5);
diff=zeros(10,18491);
for i=1:5
for j=i+1:5
diff(k,:)=dist(i,:)-dist(j,:);
ritardi_reali(h,:)=tempi_reali(i,:)-tempi_reali(j,:);
h=h+1;
k=k+1;
end
end
L=reshape(diff,184910,1);
delta_reali=reshape(ritardi_reali,50,1);
lent1=zeros(18491,1);
lent2=zeros(18491,1);
lent3=zeros(18491,1);
lent4=zeros(18491,1);
lent5=zeros(18491,1);
tempi_sint1=zeros(5,18491); %inseriti da ciclo for sottostante --> abbassa tempo
tempi_sint2=zeros(5,18491);
tempi_sint3=zeros(5,18491);
tempi_sint4=zeros(5,18491);
tempi_sint5=zeros(5,18491);
j=0;
for i=1:18491
lent1(i,:)=pinv(L(j+1:j+10,:))*delta_reali(1:10,:);
lent2(i,:)=pinv(L(j+1:j+10,:))*delta_reali(11:20,:);
lent3(i,:)=pinv(L(j+1:j+10,:))*delta_reali(21:30,:);
lent4(i,:)=pinv(L(j+1:j+10,:))*delta_reali(31:40,:);
lent5(i,:)=pinv(L(j+1:j+10,:))*delta_reali(41:50,:);
tempi_sint1(:,i)=lent1(i,:)*dist(:,i);
tempi_sint2(:,i)=lent2(i,:)*dist(:,i);
tempi_sint3(:,i)=lent3(i,:)*dist(:,i);
tempi_sint4(:,i)=lent4(i,:)*dist(:,i);
tempi_sint5(:,i)=lent5(i,:)*dist(:,i);
j=j+10;
end
ritardi_sintetici1=zeros(10,18491);
ritardi_sintetici2=zeros(10,18491);
ritardi_sintetici3=zeros(10,18491);
ritardi_sintetici4=zeros(10,18491);
ritardi_sintetici5=zeros(10,18491);
h=1;
for i=1:5
for j=i+1:5
ritardi_sintetici1(h,:)=tempi_sint1(i,:)-tempi_sint1(j,:);
ritardi_sintetici2(h,:)=tempi_sint2(i,:)-tempi_sint2(j,:);
ritardi_sintetici3(h,:)=tempi_sint3(i,:)-tempi_sint3(j,:);
ritardi_sintetici4(h,:)=tempi_sint4(i,:)-tempi_sint4(j,:);
ritardi_sintetici5(h,:)=tempi_sint5(i,:)-tempi_sint5(j,:);
h=h+1;
end
end
somma1=zeros(18491,1);
somma2=zeros(18491,1);
somma3=zeros(18491,1);
somma4=zeros(18491,1);
somma5=zeros(18491,1);
for i=1:18491
somma1(i,:)=sum(((ritardi_sintetici1(:,i)-delta_reali(1:10,:)).*(ritardi_sintetici1(:,i)-delta_reali(1:10,:))));
somma2(i,:)=sum(((ritardi_sintetici2(:,i)-delta_reali(11:20,:)).*(ritardi_sintetici2(:,i)-delta_reali(11:20,:))));
somma3(i,:)=sum(((ritardi_sintetici3(:,i)-delta_reali(21:30,:)).*(ritardi_sintetici3(:,i)-delta_reali(21:30,:))));
somma4(i,:)=sum(((ritardi_sintetici4(:,i)-delta_reali(31:40,:)).*(ritardi_sintetici4(:,i)-delta_reali(31:40,:))));
somma5(i,:)=sum(((ritardi_sintetici5(:,i)-delta_reali(41:50,:)).*(ritardi_sintetici5(:,i)-delta_reali(41:50,:))));
end
[min_val_S1,pos_idx_1]=min(somma1(:));
[min_val_S2,pos_idx_2]=min(somma2(:));
[min_val_S3,pos_idx_3]=min(somma3(:));
[min_val_S4,pos_idx_4]=min(somma4(:));
[min_val_S5,pos_idx_5]=min(somma5(:));
zambo=[puntigriglia(pos_idx_1,:) 1/lent1(pos_idx_1,:);puntigriglia(pos_idx_2,:) 1/lent2(pos_idx_2,:);puntigriglia(pos_idx_3,:) 1/lent3(pos_idx_3,:); puntigriglia(pos_idx_4,:) 1/lent4(pos_idx_4,:); puntigriglia(pos_idx_5,:) 1/lent5(pos_idx_5,:)];
hold(app.axes1);
scatter3(app.axes1,zambo(1,1),zambo(1,2),zambo(1,3),'red','x');
scatter3(app.axes1,zambo(2,1),zambo(2,2),zambo(2,3),'black','o');
scatter3(app.axes1,zambo(3,1),zambo(3,2),zambo(3,3),'green','+');
scatter3(app.axes1,zambo(4,1),zambo(4,2),zambo(4,3),'blue','*');
scatter3(app.axes1,zambo(5,1),zambo(5,2),zambo(5,3),'yellow','s');
app.cinquesensori=zambo;
app.UITable2.Data=app.cinquesensori;
elseif isequal(app.UITable.Data,app.ottosensori) %MOTORE PER 8 SENSORI
puntigriglia=zeros(18491,3);
n=1;
for i=-5:.5:15 % ciclo for che genera coordinate per punti della mia figura
for j=-5:.5:15
for k=-4:.5:1
puntigriglia(n,:)=[i*1 j*1 k*1];
n = n+1;
end
end
end
dist=zeros(8,18491);
for i=1:8
for j=1:18491
dist(i,j) = sqrt((app.ottosensori(i,1) - puntigriglia(j,1)) ^ 2 + (app.ottosensori(i,2) - puntigriglia(j, 2)) ^ 2 +(app.ottosensori(i,3) - puntigriglia(j, 3)) ^ 2);
end
end
tempi_reali = xlsread("8sensori.xlsx"); %importo excel dei tempi reali delle sorgenti ai sensori dati da prof
h=1;
k=1;
ritardi_reali=zeros(28,5);
diff=zeros(28,18491);
for i=1:8
for j=i+1:8
diff(k,:)=dist(i,:)-dist(j,:);
ritardi_reali(h,:)=tempi_reali(i,:)-tempi_reali(j,:);
h=h+1;
k=k+1;
end
end
L=reshape(diff,517748,1);
delta_reali=reshape(ritardi_reali,140,1);
lent1=zeros(18491,1);
lent2=zeros(18491,1);
lent3=zeros(18491,1);
lent4=zeros(18491,1);
lent5=zeros(18491,1);
tempi_sint1=zeros(8,18491); %inseriti da ciclo for sottostante --> abbassa tempo
tempi_sint2=zeros(8,18491);
tempi_sint3=zeros(8,18491);
tempi_sint4=zeros(8,18491);
tempi_sint5=zeros(8,18491);
j=0;
for i=1:18491
lent1(i,:)=pinv(L(j+1:j+28,:))*delta_reali(1:28,:);
lent2(i,:)=pinv(L(j+1:j+28,:))*delta_reali(29:56,:);
lent3(i,:)=pinv(L(j+1:j+28,:))*delta_reali(57:84,:);
lent4(i,:)=pinv(L(j+1:j+28,:))*delta_reali(85:112,:);
lent5(i,:)=pinv(L(j+1:j+28,:))*delta_reali(113:140,:);
tempi_sint1(:,i)=lent1(i,:)*dist(:,i);
tempi_sint2(:,i)=lent2(i,:)*dist(:,i);
tempi_sint3(:,i)=lent3(i,:)*dist(:,i);
tempi_sint4(:,i)=lent4(i,:)*dist(:,i);
tempi_sint5(:,i)=lent5(i,:)*dist(:,i);
j=j+28;
end
ritardi_sintetici1=zeros(28,18491);
ritardi_sintetici2=zeros(28,18491);
ritardi_sintetici3=zeros(28,18491);
ritardi_sintetici4=zeros(28,18491);
ritardi_sintetici5=zeros(28,18491);
h=1;
for i=1:8
for j=i+1:8
ritardi_sintetici1(h,:)=tempi_sint1(i,:)-tempi_sint1(j,:);
ritardi_sintetici2(h,:)=tempi_sint2(i,:)-tempi_sint2(j,:);
ritardi_sintetici3(h,:)=tempi_sint3(i,:)-tempi_sint3(j,:);
ritardi_sintetici4(h,:)=tempi_sint4(i,:)-tempi_sint4(j,:);
ritardi_sintetici5(h,:)=tempi_sint5(i,:)-tempi_sint5(j,:);
h=h+1;
end
end
somma1=zeros(18491,1);
somma2=zeros(18491,1);
somma3=zeros(18491,1);
somma4=zeros(18491,1);
somma5=zeros(18491,1);
for i=1:18491
somma1(i,:)=sum(((ritardi_sintetici1(:,i)-delta_reali(1:28,:)).*(ritardi_sintetici1(:,i)-delta_reali(1:28,:))));
somma2(i,:)=sum(((ritardi_sintetici2(:,i)-delta_reali(29:56,:)).*(ritardi_sintetici2(:,i)-delta_reali(29:56,:))));
somma3(i,:)=sum(((ritardi_sintetici3(:,i)-delta_reali(57:84,:)).*(ritardi_sintetici3(:,i)-delta_reali(57:84,:))));
somma4(i,:)=sum(((ritardi_sintetici4(:,i)-delta_reali(85:112,:)).*(ritardi_sintetici4(:,i)-delta_reali(85:112,:))));
somma5(i,:)=sum(((ritardi_sintetici5(:,i)-delta_reali(113:140,:)).*(ritardi_sintetici5(:,i)-delta_reali(113:140,:))));
end
[min_val_S1,pos_idx_1]=min(somma1(:));
[min_val_S2,pos_idx_2]=min(somma2(:));
[min_val_S3,pos_idx_3]=min(somma3(:));
[min_val_S4,pos_idx_4]=min(somma4(:));
[min_val_S5,pos_idx_5]=min(somma5(:));
zambo=[puntigriglia(pos_idx_1,:) 1/lent1(pos_idx_1,:);puntigriglia(pos_idx_2,:) 1/lent2(pos_idx_2,:);puntigriglia(pos_idx_3,:) 1/lent3(pos_idx_3,:); puntigriglia(pos_idx_4,:) 1/lent4(pos_idx_4,:); puntigriglia(pos_idx_5,:) 1/lent5(pos_idx_5,:)];
hold(app.axes1);
scatter3(app.axes1,zambo(1,1),zambo(1,2),zambo(1,3),'red','x');
scatter3(app.axes1,zambo(2,1),zambo(2,2),zambo(2,3),'black','o');
scatter3(app.axes1,zambo(3,1),zambo(3,2),zambo(3,3),'green','+');
scatter3(app.axes1,zambo(4,1),zambo(4,2),zambo(4,3),'blue','*');
scatter3(app.axes1,zambo(5,1),zambo(5,2),zambo(5,3),'yellow','s');
app.ottosensori=zambo;
app.UITable2.Data=app.ottosensori;
if i use this code app doesn't localize the correct position of the

回答 (0 件)

カテゴリ

Help Center および File ExchangeMATLAB についてさらに検索

製品


リリース

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by