Student Lounge

Sharing technical and real-life examples of how students can use MATLAB and Simulink in their everyday projects #studentsuccess

Why Use Apps?

I asked Connell D’Souza, today’s guest blogger, that exact question. His answer was short: “REPETITION. INTERACTION. AUTOMATION.” In the following, Connell will share his experience from a past job and illustrate some options we have. Enjoy!

– –

Most of us use a multitude of apps every day for almost everything from waking up in the morning to watching cat videos at 2 am. But how do apps apply to student competitions? Being an alumnus of a student competition team, something that I learnt over time is that the easiest way to help your team do better every year is to not re-invent the wheel. It is easier to define your goals and work on making upgrades rather than spending months redesigning every component and here is one way in which apps can help.

MATLAB apps are interactive tools to perform technical computing tasks so you could. For example, you can set up an app that does design calculations based on design constraints provided interactively by the user, a calculator of sorts. Now these apps can be kept within the team and used for years allowing future generations of your team to do concentrate on more involved technology upgrades. Coming back to my point on usability, when code is passed down the years, the hardest part is making sure someone using the code in the future is still able to understand what every variable and function in your code does.

MATLAB has a few tools available for you to build your Apps each with their own benefits. You can put almost any kind of MATLAB code into an app. Let’s jump right in to the high-level workflow involved with app building in MATLAB.

Opportunity

MATLAB gives you a lot of flexibility in terms of functionality that can be used to build apps. You could build an app with MATLAB functionality or even Simulink models. My recommendation for picking code that should be put in an app is REPETITION, INTERACTION and AUTOMATION. A good indicator of code that can be wrapped into an app is code that you foresee will be run multiple times and requires user interaction to provide inputs, manipulate parameters and post-process results. Automate repetitive interactive code using apps. An example could be this piece of code below.

%% Clean Up
close all;
clear;
clc;

%%  Create Video Input Object
videoInputNumber = 1;
vidObj = videoinput('winvideo', videoInputNumber);
preview(vidObj); % View the video stream

%% Initialise variables
numOfPictures = 5; 
waitTime = 1; 
mode = 1;
mkdir 'Session Data'

%% Click images
if mode == 1
    img = clickSingleImage(vidObj);
    imshow(ycbcr2rgb(img));
elseif mode == 2
    img = clickBurst(vidObj, numOfPictures, waitTime);
    imshow(ycbcr2rgb(img)); 
end
    
%% Delete Video Object 
delete(vidObj)

 

This script uses a few tunable parameters like numOfPictures, mode, and waitTime to click either single images or a burst of images. Now someone who is not familiar with the code will have a hard time understanding the parameters to change to use this script effectively. When you scale this up to code that contains multiple tunable parameters, this problem gets magnified. There could also be the case where the user changes a non-tunable parameter, thus introducing an unwanted bug. Wrapping such code in an app is an easy way to expose users to only tunable parameters as well as streamlining their experience with using your code.

Designing The App

Now that you have selected your functionality, the next step is to build the app. MATLAB has a couple of different workflows, a programmatic and an interactive workflow. Both these workflows come with their own set of advantages. Let’s look at them in a little detail.

Interactive Workflow

In the interactive workflow you can lay out components interactively and only write code for the functionality. App Designer is a tool that can help you do this. It was introduced in R2016a and is a rich development environment to build apps. App Designer provides a tightly integrated environment to lay out a set of ready to use components like buttons, checkboxes, edit fields, etc. and then write callbacks for these components within the same environment, much like an app to build apps. App inception anyone?

A good use case for App Designer is when you are not particularly concerned with using readymade components. The thing that I like about App Designer is when the components are laid out, the integrated MATLAB editor is automatically populated with the code that defines that component, which means, I as the user, only add the functionality for that component. You can watch this video to see how it took me under 10 minutes to put an app together with App Designer.

[VIDEO] MATLAB and Simulink Robotics Arena: Building Apps with MATLAB and App Designer

GUIDE is another tool that supports the interactive workflow. GUIDE has been around for a while and is like App Designer as it also contains a drag and drop environment to lay out the components of your app, the difference being that you go back to the MATLAB editor to code the behavior of the app. While GUIDE continues to be a supported workflow, App Designer is the future of building Apps with MATLAB. So, if you plan to build new apps in MATLAB, App Designer is our recommended tool. For a complete comparison between App Designer and GUIDE you can view this page from the product documentation.

Programmatic Workflow

This workflow gives users the most flexibility in designing apps. However, it comes at the expense of having to write your own code for every graphical component along with the functionality. Apps designed in this way can be highly customized, so this is the way to go if you intend to build a complex application with a lot of different dependent components. A good example of an app that has been built using this workflow is shown in this video.

[VIDEO] MATLAB and Simulink Robotics Arena: Building Interactive Design Tools

Zachary built an app to help him and his AIAA Design/Build/Fly team design their model aircraft. As you can see the app is very complex and includes a visualization pane that can be used to interactively manipulate the shape of the aircraft and perform the necessary aerodynamic stability calculations. There is also functionality to import Digital DATCOM files and then export the design of the airplane to a simulation environment which makes this an interactive robust design tool for model aircraft, and again all built in MATLAB. This is a great example of how student competition teams can use apps to help your team for generations to come.

Packaging and Sharing

Once the app is built, you can either share the MATLAB files directly with users allowing for future editing of the app or package the app from App Designer

or from the MATLAB toolstrip using the package app option in the Add-Ons dropdown.

Packaging the apps will make sure all the necessary files needed for the app are packaged together automatically. This is the way to go if you have many dependent files and your users are not too familiar with managing the MATLAB search path. Once installed, MATLAB will automatically add all dependent files to the path and load the app to the Apps tab.

You can also add documentation to these apps like you would for any custom toolboxes.

To conclude,

  • Apps are a nice way to make your code interactive and user friendly.
  • There are a few different workflows that you can use depending on the complexity of your application and the programming skills of the developer. There is something for everyone.
  • Apps are a great way of sharing interactive code with future generations of your team.
|
  • print

Comments

To leave a comment, please click here to sign in to your MathWorks Account or create a new one.