| Creating Graphical User Interfaces | ![]() |
Running the Simulation from the GUI
The GUI Simulate and store results button callback runs the model simulation and stores the results in the handles structure. Storing data in the handles structure simplifies the process of passing data to other subfunction since this structure can be passed as an argument.
When a user clicks on the Simulate and store results button, the callback executes the following steps:
sim, which runs the simulation and returns the data that is used for plotting.
handles structure.
String to list the most recent run.
Here is the Simulate and store results button callback.
function SimulateButton_Callback(hObject, eventdata, handles) [timeVector,stateVector,outputVector] = sim('f14');% Retrieve old results data structureif isfield(handles,'ResultsData') & ~isempty(handles.ResultsData) ResultsData = handles.ResultsData;% Determine the maximum run number currently used.maxNum = ResultsData(length(ResultsData)).RunNumber; ResultNum = maxNum+1; else% Set up the results data structureResultsData = struct('RunName',[],'RunNumber',[],... 'KiValue',[],'KfValue',[],'timeVector',[],'outputVector',[]); ResultNum = 1; end if isequal(ResultNum,1),% Enable the Plot and Remove buttonsset([handles.RemoveButton,handles.PlotButton],'Enable','on') end% Get Ki and Kf values to store with the data and put in the results list.Ki = get(handles.KiValueSlider,'Value'); Kf = get(handles.KfValueSlider,'Value'); ResultsData(ResultNum).RunName = ['Run',num2str(ResultNum)]; ResultsData(ResultNum).RunNumber = ResultNum; ResultsData(ResultNum).KiValue = Ki; ResultsData(ResultNum).KfValue = Kf; ResultsData(ResultNum).timeVector = timeVector; ResultsData(ResultNum).outputVector = outputVector;% Build the new results list string for the listboxResultsStr = get(handles.ResultsList,'String'); if isequal(ResultNum,1) ResultsStr = {['Run1',num2str(Kf),' ',num2str(Ki)]}; else ResultsStr = [ResultsStr;... {['Run',num2str(ResultNum),' ',num2str(Kf),' ',num2str(Ki)]}]; end set(handles.ResultsList,'String',ResultsStr);% Store the new ResultsDatahandles.ResultsData = ResultsData; guidata(hObject, handles)
| Programming the Slider and Edit Text Components | Removing Results from the List Box | ![]() |