MATLAB practice | APP design

 

01、应用刞战

[Example 1]Generate an App for observing the impact of the viewpoint elevation angle and coordinate axis coloring method on the display effect of three-dimensional graphics. The interface is shown in Figure 1. The list box in the upper right part of the interface is used to select drawing data, the switch button group is used to select the drawing method, the knob in the middle is used to set the viewpoint azimuth and elevation, and the binning knob in the lower right part is used to set the coordinate axis coloring method and rocker switch. Used to display grid lines.

■ Figure 1 App running interface

The steps are as follows.

1. Open the App design tool window and add components

Select the "axes" component in the App Design Tool Component Library, drag it to the editing area, and adjust the size and position. Add another list box, a toggle button group, two knobs, a gear knob and a rocker switch, and then adjust the position and size of the components as shown in Figure 2.

■ Figure 2 App interface design

 

The steps are as follows.

1. Open the App design tool window and add components

Select the "axes" component in the App Design Tool Component Library, drag it to the editing area, and adjust the size and position. Add another list box, a toggle button group, two knobs, a gear knob and a rocker switch, and then adjust the position and size of the components as shown in Figure 11-9.

2. Use the property panel to set the properties of the component object

Select each component object in sequence in the editing area of ​​the design view, and set the properties of the component object in the corresponding property panel according to Table 1.

■ Table 1 Main properties of component objects

3. Write code to implement component functions

(1) Write custom functions, including the my_plot function for drawing graphics and the my_view function for adjusting the viewpoint.

① my_plot function. Switch to the code view of the App Design Tool, select the "Editor" tab in the ribbon, and click the "Add Function" button in the "Insert" command group. At this time, a private function framework is added to the code, with the following structure : 

function results = func(app.
end

You can also select the "Function" tab in the code browser of the App Design Tool and click the "Add Function" button at the right end of the "Search" bar to add a private function framework. If you need to add a public function, click the "Add Function" drop-down button and select the "Public Function" option from the expanded list.

Change the name of the above function func to my_plot. Since there is no need to return a value, delete the "results=" in the function header. Then add the following code to the my_plot function body:

%根据在列表框中的选择项目,确定绘图数据
switch app.ListBox.Value
case'Sinc
[x,y] = meshgrid( - 8:0.3:8);
r= sqrt(x.^2 +y.^2);
z= sin(r)./r;
case'Peaks!
[x,Y,z]= peaks;
Spherecase
[x,Y,z]= sphere;
end
%根据在切换按钮组中按下的按钮,确定绘图方法
switch app.ButtonGroup.SelectedObject
case app.Button
surf(app.UIAxes,x,y,z)
app.Knob3.Enable ='On';
case app.Button2
mesh(app.UIAxes,x,Y,z)
app.Knob3.Enable ='Off';
case app.Button3
contour3(app.UIAxes,x,Y,z)
app.Knob3.Enable = 'Off';
end

②my_view function. Create the my_view function framework for updating the coordinate axis viewpoint in the same way, and then add the following code to the my_view function body:

app.Knob2.Value;
az =app.Knob.Value;
view(app.UIAxes,az,el)

(2) Write the component object callback function.

① Write the response code for opening the user interface window. Right-click the blank space of the graphics window in the design view and select the "Add StartupFcn Callback" command under the "Callback" menu item from the shortcut menu. At this time, you will switch to the code view and add the StartupFcn function frame to the code. The structure is as follows:

function startupEcn(app
% Code that executes after component creation
...
end

 You can also select the "Callback" tab in the code browser of the code view, click the "Add callback function to respond to user interaction" button on the right end of the search bar, select the component, callback, and modify it in the pop-up "Add Callback Function" dialog box. Callback function name (the default name is the same as the callback), and then click the "OK" button to add the StartupFcn function frame. To draw graphics using default data and drawing functions when the user interface window is opened during operation, add the following code to the StartupFcn function body:

my_plot(app)

②Write response code for the list box and toggle button group. In the design view, right-click the list box object ListBox and select the "Add ValueChangedFcn Callback" command under the "Callback" menu item from the shortcut menu. At this time, you will switch to the code view and add ListBoxValueChanged in the methods section of the code. Function framework, as shown below:

% Value changed function: ListBox
function ListBoxValueChanged(app,event)
end

When the program is running, the user selects a drawing data source in the list box, and the my_plot function will be called to draw the graph, so enter the following code in the ListBoxValueChanged function body:

my_plot(app)

Clicking a button in the switch button group will also redraw the graphics, so create the callback function ButtonGroupSelectionChanged of the button group in the same way, and enter the following code in the function body:

my_plot(app)

③ Write response code for the knob object. Establish the callback function KnobValueChanged of the knob object used to set the viewpoint azimuth angle and the callback function Knob2ValueChanged of the knob object used to set the viewpoint elevation angle, and enter the following code in the function bodies of the two functions:

(dde)MTA Aw

④ Write the response code for the shift knob. The baffle knob is used to set the coloring mode, establish the callback function Knob3ValueChanged of the object, and enter the following code in the function body:

shading(app.UIAxes,app.Knob3.Value)

⑤ Write the response code for the rocker switch.

The rocker switch is used to show/hide the grid. Create the callback function SwitchValueChanged of the object and enter the following code in the function body:

switch app.Switch.Value
case'On'
grid(app.UIAxes,On')
caseOff'
grid(app.UIAxesOff')
end

4. Run the App

Click the "Run" button in the "Design Tools" tab of the Ribbon of the App Design Tool window, or the "Run" button on the quick access toolbar, or press the F5 key to run the program. The results are shown in Figure 1.

5. Package App

After the App is successfully designed, it can be packaged as a MATLAB application module. In the design view, click the MATLAB App option under the "Share" button in the "Design Tools" tab, and the dialog box shown in Figure 3 will pop up.

■ Figure 3 Application packaging dialog box

In the "Describe your App" area of ​​the dialog box, enter the icon name and other information, specify the output folder of the packaged file in the "Output Folder" column of the "Package as Installation File" area on the right side of the dialog box, and then click the "Package" button .

After packaging is completed, the "Open output folder" link appears on the right side of the dialog box. Click this link and you can see that two files are generated in the output folder: app1.prj and app1.mlappinstall.

Find the file app1.mlappinstall in the "Current Folder" of the MATLAB desktop, double-click this file, and the "Installation" dialog box will pop up as shown in Figure 4.

■ Figure 4 App “Installation” dialog box

 

 

 

 

 

 

 

 

 

Guess you like

Origin blog.csdn.net/qq_41640218/article/details/134701493