Plot in GUIDE format

3 ビュー (過去 30 日間)
Tim Schaller
Tim Schaller 2022 年 11 月 7 日
回答済み: Nipun 2023 年 12 月 29 日
Hi there,
my question is how i need to change my matlab GUIDE code, so that the plots, that appear in my code, will also appear in the two "axes" areas in the GUIDE.
n = 3;
n1 = n-1;
a = 20; % Breite des Krummers entlang x
b = 10; % Höhe des Krümmers entlang y
P = [0 b;0 0;a 0]; % Punkte für Beziér-Spline Plot
T = 15; % Anazhl an Teilpunkten für den Plot
H = 8; % Höhe des Ausgangs
R = 2; % Radius des Eingangs
h = 6; % Breite des Ausgangs
syms t s(t)
B = bernsteinMatrix(n1,t);
bezierCurve = B*P;
s(t) = int(norm(diff(bezierCurve)),0,t);
snum = linspace(0,s(1),T);
for i = 1:T
tnum(i) = vpasolve(snum(i)==s(t),t); %vpasolve löst Gleichungen mit Symbolvariablen
end
px = double(subs(bezierCurve(:,1),t,tnum)).';
py = zeros(T,1);
pz = double(subs(bezierCurve(:,2),t,tnum)).';
normalToCurve = diff(bezierCurve)*[0 1;-1 0];
normalToCurve = normalToCurve/norm(normalToCurve);
newNormalToCurve = [double(subs(normalToCurve(1),t,tnum))' double(subs(normalToCurve(2),t,tnum))'];
newNormalToCurvex = newNormalToCurve(:,1);
newNormalToCurvez = newNormalToCurve(:,2);
%%%%%%%%%%%Obere Linie
for i = 1:T
S = double(s((i-1)/(T-1)));
d = R*(1-S/double(s(1)))+(H/2)*S/double(s(1));
delta(i) = d;
end
delta = delta';
pxnew1 = px+newNormalToCurvex.*delta;
pznew1 = pz+newNormalToCurvez.*delta;
for i = 1:T
S = double(s((i-1)/(T-1)));
rhilfe = (h/2)*S/double(s(1));
r(i) = rhilfe;
end
r = r';
% for i = 1:2
% pynew1 = r*(-1)^i;
% plot3(pxnew1,pynew1,pznew1,'r-o','MarkerSize',4)
% end
pynew1a = - r;
pynew1b = r;
%%%%%%%%%%Untere Linie
delta = - delta;
pxnew2 = px+newNormalToCurvex.*delta;
pznew2 = pz+newNormalToCurvez.*delta;
% for i = 1:2
% pynew1 = r*(-1)^i;
% plot3(pxnew2,pynew1,pznew2,'r-o','MarkerSize',4)
% end
pynew2a = pynew1a;
pynew2b = pynew1b;
%%%%%%%%%%seitliche Linien (schwarz)
for i = 1:T
S = double(s((i-1)/(T-1)));
chilfe = R*(1-S/double(s(1)))+(h/2)*S/double(s(1));
c(i) = chilfe;
end
for i = 1:T
S = double(s((i-1)/(T-1)));
lhilfe = H/2*S/double(s(1));
l(i) = lhilfe;
end
l = l';
c = c';
pynew3 = ones(T,1).*c;
pxnew3a = px-newNormalToCurvex.*l;
pxnew3b = px+newNormalToCurvex.*l;
pznew3a = pz-newNormalToCurvez.*l;
pznew3b = pz+newNormalToCurvez.*l;
pynew3a = pynew3;
pynew3b = -pynew3;
%%%%%%%%%%%%4Ecken
%%%vorne links/rechts (magenta)
Rnewx = R*cos(pi/4);
Rnewy = R*cos(pi/4);
for i = 1:T
S = double(s((i-1)/(T-1)));
jhilfe = Rnewx*(1-S/double(s(1)))+(H/2)*S/double(s(1));
j(i) = jhilfe;
end
for i = 1:T
S = double(s((i-1)/(T-1)));
ehilfe = Rnewy*(1-S/double(s(1)))+(h/2)*S/double(s(1));
e(i) = ehilfe;
end
j = j';
e = e';
pxnew4 = px+newNormalToCurvex.*j;
pznew4 = pz+newNormalToCurvez.*j;
% for i = 1:2
% pynew4 = ones(T,1).*e*(-1)^i;
% plot3(pxnew4,pynew4,pznew4,'m-o',"MarkerSize",4)
% end
pynew4a = -ones(T,1).*e;
pynew4b = ones(T,1).*e;
%%%%%vorne links/recht(grün)
pxnew5 = px-newNormalToCurvex.*j;
pznew5 = pz-newNormalToCurvez.*j;
% for i = 1:2
% pynew5 = ones(T,1).*e*(-1)^i;
% plot3(pxnew5,pynew5,pznew5,'g-o',"MarkerSize",4)
% end
pynew5a = pynew4a;
pynew5b = pynew4b;
%%%%%PLOT%%%%%%
plot3(px,py,pz,'b-o',"MarkerSize",4)
axis equal
axis tight
grid on
hold on
plot3(pxnew1,pynew1a,pznew1,'r-o','MarkerSize',4)
plot3(pxnew1,pynew1b,pznew1,'r-o','MarkerSize',4)
plot3(pxnew2,pynew2a,pznew2,'r-o','MarkerSize',4)
plot3(pxnew2,pynew2b,pznew2,'r-o','MarkerSize',4)
plot3(pxnew3a,pynew3a,pznew3a,'k-o','MarkerSize',4)
plot3(pxnew3b,pynew3a,pznew3b,'k-o','MarkerSize',4)
plot3(pxnew3a,pynew3b,pznew3a,'k-o','MarkerSize',4)
plot3(pxnew3b,pynew3b,pznew3b,'k-o','MarkerSize',4)
plot3(pxnew4,pynew4a,pznew4,'m-o',"MarkerSize",4)
plot3(pxnew4,pynew4b,pznew4,'m-o',"MarkerSize",4)
plot3(pxnew5,pynew5a,pznew5,'g-o',"MarkerSize",4)
plot3(pxnew5,pynew5b,pznew5,'g-o',"MarkerSize",4)
% view([0 0])
hold off
pxgesamt = [px;pxnew1; pxnew1; pxnew2; pxnew2; pxnew3a;pxnew3b;pxnew3a;pxnew3b;pxnew4; pxnew4; pxnew5; pxnew5];
pygesamt = [py;pynew1a;pynew1b;pynew2a;pynew2b;pynew3a;pynew3a;pynew3b;pynew3b;pynew4a;pynew4b;pynew5a;pynew5b];
pzgesamt = [pz;pznew1; pznew1; pznew2; pznew2; pznew3a;pznew3b;pznew3a;pznew3b;pznew4; pznew4; pznew5; pznew5];
pgesamt = [pxgesamt pygesamt pzgesamt];
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
arclen = arclength(px,pz);
S = linspace(0,arclen,T);
%obere Linien
p2y1 = linspace(0,h/2,T);
for i = 1:T
z2t = R*(1-S(i)/arclen)+(H/2)*S(i)/arclen;
z2(i) = z2t;
end
z2 = z2';
p2z1 = z2;
hold on
grid on
axis equal
axis tight
p2y1b = -p2y1;
%untere Linien
p2y2 = p2y1;
p2z2 = -p2z1;
p2y2b = p2y1b;
%seitliche Linien
for i = 1:T
c2hilfe = R*(1-S(i)/arclen)+h/2*(S(i)/arclen);
c2(i) = c2hilfe;
end
c2 = c2';
p2y3 = c2;
p2z3 = linspace(0,H/2,T);
p2z3b = -p2z3;
p2y3b = -p2y3;
%oben vorne/hinten (magenta)
Rnew = R*cos(pi/4);
for i = 1:T
j2hilfe = Rnew*(1-S(i)/arclen)+(H/2)*S(i)/arclen;
j2(i) = j2hilfe;
end
for i = 1:T
e2hilfe = Rnew*(1-S(i)/arclen)+(h/2)*S(i)/arclen;
e2(i) = e2hilfe;
end
j2 = j2';
e2 = e2';
p2y4 = e2;
p2z4 = j2;
p2y4b = -p2y4;
p2z5 = -p2z4;
hold off
for i = 1:T
pyt = [p2y1(i); p2y4(i); p2y3(i); p2y3(i); p2y4(i); p2y2(i); p2y2b(i); p2y4b(i);
p2y3b(i); p2y3b(i); p2y4b(i); p2y1b(i); p2y1(i)];
pzt = [p2z1(i); p2z4(i); p2z3(i); p2z3b(i); p2z5(i); p2z2(i); p2z2(i); p2z5(i);
p2z3b(i); p2z3(i); p2z4(i); p2z1(i); p2z1(i)];
plot(pyt,pzt,'o',"MarkerSize",3,"MarkerEdgeColor",'k')
hold on
end
axis equal
axis tight
grid on
for i = 1:T
pyt = [p2y1(i);p2y1b(i)];
pzt = [p2z1(i);p2z1(i)];
plot(pyt,pzt,'k-o',"MarkerSize",3,"MarkerEdgeColor",'k')
pyt = [p2y3(i); p2y3(i)];
pzt = [p2z3(i); p2z3b(i)];
plot(pyt,pzt,'k-o',"MarkerSize",3,"MarkerEdgeColor",'k')
pyt = [p2y2(i); p2y2b(i)];
pzt = [p2z2(i); p2z2(i)];
plot(pyt,pzt,'k-o',"MarkerSize",3,"MarkerEdgeColor",'k')
pyt = [p2y3b(i); p2y3b(i)];
pzt = [p2z3b(i); p2z3(i)];
plot(pyt,pzt,'k-o',"MarkerSize",3,"MarkerEdgeColor",'k')
hold on
end
axis equal
axis tight
grid on
%%%%%%%%%%%%%%%%%%%%%%%Ecken abrunden%%%%%%%%%%%%%%%%%%%%%
%radius (Funktion für linear sinkenden Radius)
for i = 1:T
radius = R*(1-S(i)/arclen);
rad(i) = radius;
end
rad = rad';
% Mittelpunkt
for i = 1:T
MPxhilfe = h/2*S(i)/arclen;
MPx(i) = MPxhilfe;
MPyhilfe = H/2*S(i)/arclen;
MPy(i) = MPyhilfe;
end
MPx = MPx';
MPy = MPy';
MP = [MPx MPy];
% Winkel
deg = 90:-1:0;
for i = 1:T
xc = MP(i,1)+rad(i)*cosd(deg);
yc = MP(i,2)+rad(i)*sind(deg);
plot(xc,yc,'Color','k')
hold on
end
axis tight
axis equal
grid on
for i = 1:T
xc = MP(i,1)+rad(i)*cosd(deg);
yc = -MP(i,2)-rad(i)*sind(deg);
plot(xc,yc,'Color','k')
hold on
end
for i = 1:T
xc = -MP(i,1)-rad(i)*cosd(deg);
yc = -MP(i,2)-rad(i)*sind(deg);
plot(xc,yc,'Color','k')
hold on
end
for i = 1:T
xc = -MP(i,1)-rad(i)*cosd(deg);
yc = MP(i,2)+rad(i)*sind(deg);
plot(xc,yc,'Color','k')
hold on
end
hold off
I really don't know what i was doing with the changes of the code in the Guide, it was just me trying to do something.
I hope somebody can help me with it.
Thanks!
  3 件のコメント
Image Analyst
Image Analyst 2022 年 11 月 7 日
Attach your .m file and .fig file with the paperclip icon. It will be then possible for people to download and run your program.
Tim Schaller
Tim Schaller 2022 年 11 月 8 日
編集済み: Tim Schaller 2022 年 11 月 8 日
Good idea, thank you.
Is it working?
You can easily see my problem now. The 3D plot is working now, but the 2D plot just appears to do nothing.

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

回答 (1 件)

Nipun
Nipun 2023 年 12 月 29 日
Hi Tim,
I understand that you are trying to plot the attached plots on a GUI built using GUIDE in MATLAB.
To display the plots within the axes areas of your MATLAB GUIDE GUI, you need to specify the axes handles when you call the plot3 or plot functions. These handles are usually created by GUIDE and can be accessed within the GUI's callback functions or anywhere in the GUI's code where you have access to the handles structure (usually named handles).
Here's a general way to use the axes handles:
% Example of plotting in the first axes area
axes(handles.axes1); % Set axes1 as the current axes
plot3(px, py, pz, 'b-o', 'MarkerSize', 4);
axis equal;
axis tight;
grid on;
% Example of plotting in the second axes area
axes(handles.axes2); % Set axes2 as the current axes
plot(pyt, pzt, 'o', 'MarkerSize', 3, 'MarkerEdgeColor', 'k');
axis equal;
axis tight;
grid on;
In your case, you should replace all occurrences of plot3 and plot with their axes-specific versions. Here's how you can modify some of your plotting code accordingly:
% Assuming you have two axes areas named axes1 and axes2 in GUIDE
% You would plot on axes1 like this:
axes(handles.axes1); % Set axes1 as the current axes
hold on;
plot3(px, py, pz, 'b-o', 'MarkerSize', 4);
plot3(pxnew1, pynew1a, pznew1, 'r-o', 'MarkerSize', 4);
plot3(pxnew1, pynew1b, pznew1, 'r-o', 'MarkerSize', 4);
% ... continue with other plot3 calls that should be in axes1 ...
hold off;
axis equal;
axis tight;
grid on;
% And on axes2 like this:
axes(handles.axes2); % Set axes2 as the current axes
hold on;
plot(pyt, pzt, 'o', 'MarkerSize', 3, 'MarkerEdgeColor', 'k');
% ... continue with other plot calls that should be in axes2 ...
hold off;
axis equal;
axis tight;
grid on;
Remember to replace handles.axes1 and handles.axes2 with the actual names of the axes handles in your GUIDE GUI. These names are defined when you create the axes objects in GUIDE and can be found in the tag property of each axes (e.g., 'tag' might be 'axes1' for the first axes).
If you are not sure about the names of your axes handles, you can check them in the .fig file using GUIDE, or by looking at the handles structure in the OpeningFcn of your GUI's .m file.
Finally, ensure that you have enabled the hold on and hold off commands appropriately for each axes so that multiple plots can be overlaid on the same axes without clearing the previous plots.
Hope this helps.
Regards,
Nipun

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by