GUI Help, Pop-Up Menu Help
10 views (last 30 days)
Show older comments
Code for Lab2Track
Edit: After further investigation I got some sort of basics for the GUI and was able to get my track plotted. Ive made the following adjustments to my old GUI code. Instead of having my previous code as a function I just copy-pasted sections of it into the Opening Function of the GUI. The code I have so far is below. Velocity, tangential acceleration, and normal acceleration seem to be calculated right as I did a comparison on arrays with the base "Lab2Track" code. The GUI works fine but the graphs don't seem to look the way they're supposed to. Upon doing a comparison of arrays (comparing my time array with handles.t) I got an output of zeros but I'm not sure why it is the handles.t values are incorrect because when I looked through them manually they all seemed to be exactly the same as the 'time' values from the Lab2Track code.
I'm also having trouble with the pop-up menu callback function. The code I made with 'var =' and 'str =' I got from the MATLAB guide I saw on making a GUI. The pop-up menu should change the currentXData and currentYData to the selective track points. I.E Track 1 would set currentXData and currentYData to handles.xtotal1 and handles.ytotal1, respectively. Track 2 would do the same but with the xtotal2 and ytotal2. When I attempt to switch the data sets to Track 2 I get an error.
FURTHER EDIT: Again, upon further investigation I noticed that my handles.t array had individual time values whereas I needed the time to be cumulatively summed, so I added the 'handles.time = cumsum(handles.t);'
line in the code below. The graphs outputted the same plots as my Lab2Track code. Now the only issue I'm having is getting the Pop-Up menu to work in changing what data is in currentXData/currentYData.
ANOTHER EDIT: I saw that the brackets around the switch statement under the pop-up menu callback had to be curly brackets so I switched those. I no longer get an error when attempting to select 'Track 2' under the pop-up menu, but when I change to 'Track 2' the Track on the GUI doesn't change and neither do the graphs. I'm assuming it is because currentXData and currentYData aren't getting updated but I'm not 100% sure.
clc
clear all
close all
% Ramp part of the track which starts at a height of 2
x = [0:0.28:pi+pi/50];
y = 1 + cos(x);
% Flat part after the ramp
x2 = [pi:pi/5:6];
y2 = zeros(1,length(x2));
% Loop
rCirc = 0.5;
[x3,y3] = circle(x2(end),y2(end),rCirc);
% Flat part after loop
x4 = [x3(end)+ 0.1:0.5:9];
y4 = zeros(1,length(x4));
% Concatenate all the points generated above into one array
xtotal = horzcat(x, x2, x3, x4);
ytotal = horzcat(y, y2, y3, y4);
%Set initial conditions
v = 0;
g = 9.81;
eTot = g * ytotal(1);
% Make loop run length of xtotal or ytotal - 2 loops.
for i = 1:length(xtotal) - 2
% Find the lenght of each segment
s(i) = sqrt((xtotal(i+1) - xtotal(i))^2 + (ytotal(i+1) - ytotal(i))^2);
% Calculate the difference between the next point and the last
delX = xtotal(i+1) - xtotal(i);
delY = ytotal(i+1) - ytotal(i);
% Calculate the angle of each segment, use atan2 to return values from
% zero to 2pi
alpha(i) = atan2(delY, delX);
% Calculate tangential acceleration using angle and gravity
% (draw a FBD on a ramp to find)
a_t(i) = -g * sin(alpha(i));
end
for i = 1: length(xtotal) - 2
% Call mycurvature function to find the curvature between three
% adjacent points
curvature(i) = abs(mycurvature([xtotal(i), xtotal(i+1), xtotal(i+2)], [ytotal(i), ytotal(i+1), ytotal(i+2)]));
% Calculate the radius of curvature by finding the inverse of the
% curvature between three points
rho(i) = 1./ curvature(i);
% Calculate the velocity at each segment using conservation of energy
v(i) = sqrt(2 * (eTot - g * ytotal(i)));
end
% There's a weird rho value for the 17th element, right before the circle
% begins. Account for it with the line below.
rho(17) = rho(18);
% Calculate the centripetal acceleration at each segment using the
% velocity at each segment
% Account for the fact that if there is no radius of curvature,
% centripetal acceleration = 0
for i = 1:length(xtotal) - 2
if rho(i) ~= 0
a_c(i) = v(i).^2 / rho(i);
else
a_c(i) = 0;
end
end
for i = 1:length(xtotal) - 2
if a_t(i) == 0
t(i) = s(i) / v(i);
else
t(i) = (v(i+1) - v(i)) / a_t(i);
end
end
t(1) = 0;
t(17) = t(16);
time = cumsum(t);
totalT = sum(t)
lengthTot = sum(s)
max_velocity = max(v)
max_tangential_acceleration = max(a_t)
max_normal_acceleration = max(a_c)
subplot(3, 2, [1, 2])
plot(xtotal,ytotal, 'linewidth',1.5)
b = sprintf('Track 1, Total Length = %.4f', lengthTot);
title(b)
xlabel('x')
ylabel('y')
axis equal
subplot(3, 2, 3)
plot(time, v)
title('Velocity vs. Time')
subplot(3, 2, 4)
plot(time, a_c)
title('Normal Acceleration vs. Time')
subplot(3, 2, 5)
plot(time, a_t)
title('Tangential Acceleration vs. Time')
T = sprintf('Total Time = %.4f s', totalT);
xlabel(T)
function [x3,y3] = circle(x,y,R)
% To increase resolution, decrease the step size of theta
theta = (-pi / 2):0.3:(3 * pi / 2);
xUnit = R * cos(theta);
yUnit = R * sin(theta);
x3 = x + xUnit;
y3 = y + yUnit + R;
end
5 Comments
Rik
on 13 May 2019
Most of your problems are likely due to GUIDE making it difficult to understand what is happening. The OpeningFcn is a GUIDE thing. It is a function that is run on the startup of your GUI, making it the ideal place to initialize graphical object and store the handles as fields in the guidata struct.
A callback function is run when you click on a button, or make a selection from a dropdown. In the case of a dropdown (or popupmenu as Matlab calls it) the options are stored as char vectors in a cell array. Cell arrays are containers where you can access the contents by indexing with curly braces. That is why you saw curly braces in val=get(hObject,'Value');str_list=get(hObject,'String');str=str_list{val};.
Also, because GUIDE makes ridiculously verbose code, next time please put the code in an m file and attach it (also attach the fig file, as without it we cannot run your GUI). Because of all those downsides I would encourage you to leave GUIDE while you have little invested into it. You should use either App Designer, or native Matlab.
My small guide to avoid GUIDE:
- Make a figure (with f=figure;) and look into the doc for figure which properties you want to turn off (you probably want to set Menu and Toolbar to 'none')
- Create buttons and axes and everything you need with functions like uicontrol and axes. Save the handles to each element to fields of a struct (like handles.mybutton=uicontrol(___);)
- When you've finished loading all data (and saving it to fields of your handles struct), and creating all the buttons, save your handles struct to the guidata of your figure like this guidata(handles.f,handles);. (You can also use getappdata and setappdata)
- You can set the Callback property of many objects. If you do, use a function name with an @ in front, or a char array that can be evaluated to valid code. (like @MyFunction or 'disp(''you pushed the button'')')
- Callback functions will be called with two arguments: the first is a handle to the callback object, the second is eventdata that may contain special information. To get access to your data, just use handles=guidata(gcbo);. You can replace the gcbo function with the name of the first input to your callback function if you prefer.
- More information about callbacks can be found in multiple places in the doc, for example here.
Answers (0)
See Also
Categories
Find more on Shifting and Sorting Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!