GUI Help, Pop-Up Menu Help

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
Rik on 13 May 2019
You can put this code in a callback, or put it in a script or function your call from a callback. But you will face an important issue: because you are using clear and close, you are clearing all inputs from your gui and you're closing the gui itself.
Also, you should be using explicit handles when creating axes, so you don't interfere with any other figure your user might have open.
I apreciate the help but I'm not 100% familiar with the MATLAB syntax. I've removed the clc, clear all, and close all lines and made the code into a function that returns the values I need (a_c, a_t, v, t, totalT, lengthTot, max_tangential_acceleration, max_normal_acceleration, xtotal, and ytotal). I tried putting the line below into the "Lab2GUI_OpeningFcn(hObject, eventdata, handles, varargin)" part of my GUI. I've also attached my GUI code the guide generated for me. It has an axis, three push buttons that each correspond to three different graphs, and a pop-up menu I'd like to use as a means to plot 2 different tracks. The x and y points of my track are stored in the xtotal and ytotal arrays.
I apologize for the constant issues but I'm not 100% familiar with MATLAB and my knowledge extends as far as what I learned in my class.
% Line I put into the opening function of the GUI
% It didn't work
[a_t, a_n, v, t, totalT, lengthTot, max_velocity, max_tangential_acceleration, max_normal_acceleration, xtotal, ytotal] = Lab2Track
%GUI Code
function varargout = Lab2GUI(varargin)
% LAB2GUI MATLAB code for Lab2GUI.fig
% LAB2GUI, by itself, creates a new LAB2GUI or raises the existing
% singleton*.
%
% H = LAB2GUI returns the handle to a new LAB2GUI or the handle to
% the existing singleton*.
%
% LAB2GUI('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in LAB2GUI.M with the given input arguments.
%
% LAB2GUI('Property','Value',...) creates a new LAB2GUI or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before Lab2GUI_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to Lab2GUI_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help Lab2GUI
% Last Modified by GUIDE v2.5 12-May-2019 21:55:33
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @Lab2GUI_OpeningFcn, ...
'gui_OutputFcn', @Lab2GUI_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before Lab2GUI is made visible.
function Lab2GUI_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to Lab2GUI (see VARARGIN)
[a_t, a_n, v, t, totalT, lengthTot, max_velocity, max_tangential_acceleration, max_normal_acceleration, xtotal, ytotal] = Lab2Track
% Choose default command line output for Lab2GUI
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes Lab2GUI wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = Lab2GUI_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes on button press in velocity_push.
function velocity_push_Callback(hObject, eventdata, handles)
% hObject handle to velocity_push (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --- Executes on button press in tangential_push.
function tangential_push_Callback(hObject, eventdata, handles)
% hObject handle to tangential_push (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --- Executes on button press in normal_push.
function normal_push_Callback(hObject, eventdata, handles)
% hObject handle to normal_push (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --- Executes on selection change in popupmenu1.
function popupmenu1_Callback(hObject, eventdata, handles)
% hObject handle to popupmenu1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns popupmenu1 contents as cell array
% contents{get(hObject,'Value')} returns selected item from popupmenu1
% --- Executes during object creation, after setting all properties.
function popupmenu1_CreateFcn(hObject, eventdata, handles)
% hObject handle to popupmenu1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: popupmenu controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
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.
function varargout = Lab2GUI(varargin)
% LAB2GUI MATLAB code for Lab2GUI.fig
% LAB2GUI, by itself, creates a new LAB2GUI or raises the existing
% singleton*.
%
% H = LAB2GUI returns the handle to a new LAB2GUI or the handle to
% the existing singleton*.
%
% LAB2GUI('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in LAB2GUI.M with the given input arguments.
%
% LAB2GUI('Property','Value',...) creates a new LAB2GUI or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before Lab2GUI_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to Lab2GUI_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help Lab2GUI
% Last Modified by GUIDE v2.5 13-May-2019 02:54:53
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @Lab2GUI_OpeningFcn, ...
'gui_OutputFcn', @Lab2GUI_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before Lab2GUI is made visible.
function Lab2GUI_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to Lab2GUI (see VARARGIN)
handles.xtotal1 = [0,0.280000000000000,0.560000000000000,0.840000000000000,1.12000000000000, ...
1.40000000000000,1.68000000000000,1.96000000000000,2.24000000000000,2.52000000000000, ...
2.80000000000000,3.08000000000000,3.14159265358979,3.76991118430775,4.39822971502571, ...
5.02654824574367,5.65486677646163,5.65486677646163,5.80262687979230,5.93718801315915,...
6.04653023127537,6.12088631944524,6.15361426976366,6.14179059190073,6.08647145978607, ...
5.99259836673720,5.86855671657854,5.72542678049156,5.57599392939000,5.43360655481420, ...
5.31098369686964,5.21907889025483,5.16610171762908,5.15678447204371,5.19195943529776, ...
5.26848453268363,5.37952400516281,5.51515902736217,5.61515902736216,6.11515902736216, ...
6.61515902736216,7.11515902736216,7.61515902736216,8.11515902736216,8.61515902736216];
handles.ytotal1 = [2,1.96105543831077,1.84725511101342,1.66746282584131,1.43568244627671, ...
1.16996714290024,0.891013247760129,0.620548235211845,0.379638387987320,0.187047962900110, ...
0.0577776593313419,0.00189622790485433,0,0,0,0,0,0,0.0223317554371970,0.0873321925451608, ...
0.189195015864668,0.318821122761663,0.464631399166149,0.613601047346543,0.752423052299929, ...
0.868696857770623,0.952036071008531,0.994996248300223,0.993739884954432,0.948379208167073, ...
0.862966152100070,0.745130410670350,0.605397899715390,0.456250508280277,0.311011128643510, ...
0.182653562028683,0.0826436075804201,0.0199148566748170,0,0,0,0,0,0,0];
handles.xtotal2 = [0,0.300000000000000,0.600000000000000,0.900000000000000,1.20000000000000, ...
1.50000000000000,1.80000000000000,2.10000000000000,2.40000000000000,2.70000000000000,3, ...
3.14159265358979,3.64159265358979,4.14159265358979,4.64159265358979,5.14159265358979, ...
5.64159265358979,5.64159265358979,5.88130542289190,6.06232814599374,6.14034014689182, ...
6.09624136700263,5.94082872564177,5.71215265761973,5.46620103974498,5.26319140593583, ...
5.15282759475725,5.16213051625822,5.28882249080460,5.50188490449033,5.70188490449033, ...
6.20188490449033,6.70188490449033,7.20188490449033,7.20188490449033,7.63336788923411, ...
7.95920879081744,8.09963039243398,8.02025258863344,7.74050983418389,7.32889291174421, ...
6.88617999956967,6.52076265871320,6.32210779859174,6.33885305729351,6.56689861147698, ...
6.95041095611130,7.25041095611130,7.35041095611130,7.45041095611130,7.55041095611130, ...
7.65041095611130,7.75041095611130,7.85041095611130,7.95041095611130,8.05041095611130, ...
8.15041095611130,8.25041095611130,8.35041095611130,8.45041095611130,8.55041095611130, ...
8.65041095611130,8.75041095611130,8.85041095611130,8.95041095611130,9.05041095611130, ...
9.15041095611130,9.25041095611130,9.35041095611130,9.35041095611130,9.42477796076938, ...
9.42477796076938,9.52477796076938,9.62477796076938,9.72477796076938,9.82477796076938, ...
9.92477796076938,10.0247779607694,10.1247779607694,10.2247779607694,10.3247779607694, ...
10.4247779607694,10.5247779607694,10.6247779607694,10.7247779607694,10.8247779607694, ...
10.9247779607694,11.0247779607694,11.1247779607694,11.2247779607694,11.3247779607694, ...
11.4247779607694,11.5247779607694,11.6247779607694,11.7247779607694,11.8247779607694, ...
11.9247779607694,12.0247779607694,12.1247779607694,12.2247779607694,12.3247779607694, ...
12.4247779607694,12.5247779607694,12.5247779607694,12.6247779607694,12.7247779607694, ...
12.8247779607694,12.9247779607694,13.0247779607694,13.1247779607694,13.2247779607694, ...
13.3247779607694,13.4247779607694,13.5247779607694,13.6247779607694,13.7247779607694, ...
13.8247779607694,13.9247779607694];
handles.ytotal2 = [2,1.95533648912561,1.82533561490968,1.62160996827066,1.36235775447667,...
1.07073720166770,0.772797905306913,0.495153895400142,0.262606284458755,0.0959278579829388, ...
0.0100075033995546,0,0,0,0,0,0,0,0.0612087190548137,0.229848847065930,0.464631399166149, ...
0.708073418273571,0.900571807773467,0.994996248300223,0.968228343645398,0.826821810431806, ...
0.605397899715390,0.358168907268387,0.145665112854370,0.0199148566748170,0,0,0,0,0, ...
0.110175694298665,0.413727924718674,0.836336518499067,1.27453215289243,1.62102925399224, ...
1.79099324694040,1.74281101856172,1.48827925877725,1.08971621948770,0.644704033083096, ...
0.262197203137866,0.0358467420146705,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, ...
0.00449625124977682,0.0179400799428824,0.0401971597869546,0.0710451053974034, ...
0.110175694298664,0.157197946581289,0.211642031443960,0.272963961587551,0.340551028556402, ...
0.413727924718674,0.491763490716980,0.573878020970993,0.659251054237872,0.747029571389783, ...
0.836336518499067,0.926279570071159,1.01596004486597,1.10448188522378,1.19096061017715, ...
1.27453215289243,1.35436149413987,1.42965100552981,1.49964841915184,1.56365434398712, ...
1.62102925399224,1.67119987803205,1.71366492781535,1.74800010660179,1.77386234863463, ...
1.79099324694040,1.79922163524595,1.79922163524595,1.79922163524595,1.79922163524595, ...
1.79922163524595,1.79922163524595,1.79922163524595,1.79922163524595,1.79922163524595, ...
1.79922163524595,1.79922163524595,1.79922163524595,1.79922163524595,1.79922163524595, ...
1.79922163524595,1.79922163524595];
handles.currentXData = handles.xtotal1;
handles.currentYData = handles.ytotal1;
%plot(handles.currentXData, handles.currentYData, 'linewidth', 1.5)
%axis equal
%Set initial conditions
v = 0;
g = 9.81;
eTot = g * handles.currentYData(1);
for i = 1:length(handles.currentXData) - 2
% Find the lenght of each segment
s(i) = sqrt((handles.currentXData(i+1) - handles.currentXData(i))^2 +...
(handles.currentYData(i+1) - handles.currentYData(i))^2);
% Calculate the difference between the next point and the last
delX = handles.currentXData(i+1) - handles.currentXData(i);
delY = handles.currentYData(i+1) - handles.currentYData(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)
handles.a_t(i) = -g * sin(alpha(i));
end
for i = 1: length(handles.currentXData) - 2
% Call mycurvature function to find the curvature between three
% adjacent points
curvature(i) = abs(mycurvature([handles.currentXData(i), ...
handles.currentXData(i+1), handles.currentXData(i+2)], ...
[handles.currentYData(i), handles.currentYData(i+1), handles.currentYData(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
handles.v(i) = sqrt(2 * (eTot - g * handles.currentYData(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(handles.currentXData) - 2
if rho(i) ~= 0
handles.a_c(i) = (handles.v(i)).^2 / rho(i);
else
handles.a_c(i) = 0;
end
end
for i = 1:length(handles.currentXData) - 2
if handles.a_t(i) == 0
handles.t(i) = s(i) / handles.v(i);
else
handles.t(i) = (handles.v(i+1) - handles.v(i)) / handles.a_t(i);
end
end
handles.t(1) = 0;
handles.t(17) = handles.t(16);
handles.time = cumsum(handles.t);
plot(handles.currentXData, handles.currentYData, 'linewidth', 1.5)
xlabel('x (m)')
ylabel('y (m)')
axis equal
sprintf(['Total Time: %.4f s\nTotal Track Length: %.4f meters\n' ...
'Maximum Velocity: %.4f meters/s\nMaximum Tangential Acceleration:' ...
'%.4f meters/s^2\nMaximum Normal Acceleration: %.4f units/s^2'] ...
, sum(handles.t), sum(s), max(handles.v), max(handles.a_t), max(handles.a_c))
% Choose default command line output for Lab2GUI
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes Lab2GUI wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = Lab2GUI_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes on button press in velocity_push.
function velocity_push_Callback(hObject, eventdata, handles)
% hObject handle to velocity_push (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
Velocity = figure('Name', 'Velocity');
plot(handles.time, handles.v)
title('Velocity vs. Time')
xlabel('Time (s)')
ylabel('Velocity (m/s)')
%Pressing anywhere on the graph will close it
% waitforbuttonpress;
% close(Velocity)
% --- Executes on button press in tangential_push.
function tangential_push_Callback(hObject, eventdata, handles)
% hObject handle to tangential_push (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
Tangential = figure('Name', 'Tangential Acceleration');
plot(handles.time, handles.a_t)
title('Tangential Acceleration vs. Time')
xlabel('Time (s)')
ylabel('Tangential Acceleration (m/s^2)')
%Pressing anywhere on the graph will close it
% waitforbuttonpress;
% close(Tangential)
% --- Executes on button press in normal_push.
function normal_push_Callback(hObject, eventdata, handles)
% hObject handle to normal_push (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
Normal = figure('Name', 'Normal Acceleration');
plot(handles.time, handles.a_c)
title('Normal Acceleration vs. Time')
xlabel('Time (s)')
ylabel('Normal Acceleration (m/s^2)')
%Pressing anywhere on the graph will close it
% waitforbuttonpress;
% close(Normal)
% --- Executes on button press in close_push.
function close_push_Callback(hObject, eventdata, handles)
% hObject handle to close_push (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
close all
close(gcf)
% --- Executes on selection change in popupmenu1.
function popupmenu1_Callback(hObject, eventdata, handles)
% hObject handle to popupmenu1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
str = get(hObject, 'String');
val = get(hObject, 'Value');
switch str{val}
case 'Track 1'
handles.currentXData = handles.xtotal1;
handles.currentYData = handles.ytotal1;
case 'Track 2'
handles.currentXData = handles.xtotal2;
handles.currentYData = handles.ytotal2;
end
guidata(hObject, handles);
% Hints: contents = cellstr(get(hObject,'String')) returns popupmenu1 contents as cell array
% contents{get(hObject,'Value')} returns selected item from popupmenu1
% --- Executes during object creation, after setting all properties.
function popupmenu1_CreateFcn(hObject, eventdata, handles)
% hObject handle to popupmenu1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: popupmenu controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
Rik
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.
Daniel Ortiz
Daniel Ortiz on 13 May 2019
Edited: Daniel Ortiz on 13 May 2019
I apologize about the long code, I'm new to this site so I appreciate your insight. I've attached the .fig file and the GUI code. The code also calls a mycurvature function which I've attached as well. Everything works the way I want it to except my Pop-Up Menu which doesn't switch my current data set. Since I'm not too familiar with all of the other functions I'm able to use to build a GUI the guide was the easiest and best way for me to set up this code. The GUI I've made functions the way its intended but the plot types don't change when I try to change which track I'm plotting.
Again, I greatly appreciate your help.

Sign in to comment.

Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Asked:

on 12 May 2019

Edited:

on 13 May 2019

Community Treasure Hunt

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

Start Hunting!