User:DukeEgr93/CBApp
Jump to navigation
Jump to search
Steps:
- Select a set and load data
- Add a button and have it simply load Beam01.dat
- Drag button onto app
- Change text to Run
- Since data set will likely be needed in various parts of app, in the Code View, add a public property called Force and a public property called Displacement:
- Add a button and have it simply load Beam01.dat
properties (Access = public) Force % Force applied to beam Disp % Displacement measured at end end
- In component browser, right click app.RunButton and add a RunButtonPushed callback
- Add code in this to load the data, split it, and put it into appropriate variables:
function RunButtonPushed(app, event) Data = load('Beam01.dat'); app.Force = 9.81 * Data(:,1); app.Disp = 0.0254 * Data(:,2); end
- Check this by putting a keyboard in the RunButtonPushed callback; run the app and at the keyboard prompt, see what Data, app.Force, and app.Displ look like. Use dbquit to get out of keyboard and if all is good, delete the keyboard command:
function RunButtonPushed(app, event) Data = load('Beam01.dat'); app.Force = 9.81 * Data(:,1); app.Disp = 0.0254 * Data(:,2); keyboard end
- Results:
K>> Data Data = 0 0.0052 0.1135 0.1587 0.2273 0.3140 0.3408 0.4746 0.4558 0.6368 0.5693 0.7799 0.6836 0.9366 0.7971 1.0000 K>> app.Force ans = 0 1.1135 2.2296 3.3431 4.4715 5.5850 6.7064 7.8199 K>> app.Disp ans = 0.0001 0.0040 0.0080 0.0121 0.0162 0.0198 0.0238 0.0254 K>> dbquit
- Plot the data
- Go into Design View and add a set of axes
- Change the axis labels and titles as needed
- Go into the Code View and add code to the RunButtonPushed function to plot the data and add a grid
function RunButtonPushed(app, event) Data = load('Beam01.dat'); app.Force = 9.81 * Data(:,1); app.Disp = 0.0254 * Data(:,2); plot(app.UIAxes, app.Force, app.Disp, 'ko') grid(app.UIAxes, 'on') end
- At this point, it would be good to take a moment to think about what processes this app will need to access based on making changes to various fields. Later, we are going to add the ability to fit the model with a polynomial of different orders, and as we change the order of the fit, we will probably also want the graph to update. Given that, it might be better to have a process to plot things that is separate from pushing the button.
- In Code View, add a new public function and call it UpdatePlot
function results = UpdatePlot(app) end
- Cut the plotting code from the RunButtonPushed function and paste it into the UpdatePlot function:
function results = UpdatePlot(app) plot(app.UIAxes, app.Force, app.Disp, 'ko') grid(app.UIAxes, 'on') end
- Add a call to the UpdatePlot function at the end of the RunButtonPushed function:
function RunButtonPushed(app, event) Data = load('Beam01.dat'); app.Force = 9.81 * Data(:,1); app.Disp = 0.0254 * Data(:,2); UpdatePlot(app); end
- Next we will hard-code a first-order fit; later, we will make the code flexible for higher order fits.
- In Design View, add a spinner and call it Order
- Click on app.OrderSpinner in the Component Browser; in the Spinner Properties - Configuration, change the minimum value to 0 and change the Display to Integers.
- You will most likely need to calculate the goodness of fit either when you click the Run button or when you change the spinner. This means it would be a good idea to make a separate function that calculates the fit; the callbacks for both the button and the spinner can then call this new function.
- In the Code View, make a new public function called CalcFit
function results = CalcFit(app) end
- Various parts of the app will probably need to know the coefficients of the fit, so add a public property called Coefs.
properties (Access = public) Force % Force applied to beam Disp % Displacement measured at end Coefs % Fit coefficients end
- Add code to the CalcFit program that uses the value in the Order spinner to determine the coefficients of a fit of that order:
function results = CalcFit(app) app.Coefs = polyfit(app.Force, app.Disp, app.OrderSpinner.Value); end
- Add a changed callback to the OrderSpinner to update the plot when the spinner value changes:
function OrderSpinnerValueChanged(app, event) UpdatePlot(app); end
- Add code to the UpdatePlot function to get the new coefficients and use them to plot a model:
function results = UpdatePlot(app) plot(app.UIAxes, app.Force, app.Disp, 'ko') grid(app.UIAxes, 'on') CalcFit(app); FModel = linspace(min(app.Force), max(app.Force), 100); DModel = polyval(app.Coefs, FModel); hold(app.UIAxes, 'on') plot(app.UIAxes, FModel, DModel, 'k-') hold(app.UIAxes, 'off') keyboard end