MATLAB Tips and Tricks
Excel is great for one-off activities but repetitive tasks are not easy to execute, and large data sets cause Excel to slow down heavily. This is where MATLAB comes into its own... Embrace it!!!
Manually importing data
One of the quickest ways to get data into MATLAB is to simply copy and paste it in from your external source. We most often save data in text (.txt) files or spreadsheet files (.csv, .xls, .xlsx), so we consider manual import methods for those two types of file here.
Consider the data obtained from a drone motor electronic speed controller (ESC) opened in Notepad as shown above (it has 505 seconds of data in total). Since Notepad doesn't allow you to select columns, and indeed doesn't even know the data it contains is actually arranged into columns, we must copy it all into MATLAB in one go. Follow the steps as follows:
Since MATLAB doesn't allow a mixture of text and numbers in arrays, first temporarily delete the header row in Notepad
Copy all data in the file by selecting all then copying(Ctrl+A then Ctrl+C)
In the MATLAB Workspace, right click New
The process for spreadsheets is very similar, but since spreadsheet software allows you to select columns, you can repeat Steps 3-5 above for each column and omit the array column naming in Step 7.
Using the MATLAB Import Data tool
MATLAB also has a powerful Import Data tool which gives you a lot more options for how to import the table, whether to include headers or not, etc. This can certainly be used, too, but has a few more options to know about compared to the Manual method. If you do go this route, to get the same kind of data type as above, use 'Numeric Matrix' as the Output Type. An advanced, but highly powerful, Output Type is the 'table' which has a lot of benefits that are not needed for this simple data reduction.
It is tempting to write your code in the command line, but this can quickly lead to a mess of variables and forgotten discoveries. Furthermore, it is very difficult to execute loops and other multi-line statements. Instead, start a script and modify that as you go a long, thus keeping an ongoing record of what you've need.
A really cool feature in MATLAB is the 'Live Script' - this is a script that also enables you to insert and format text, equations, images, etc. Moreover... it can be exported to a PDF so it shows all your code and the output plots without you having to format anything. One final nice touch is that you can set up variables as interactive inputs, so you don't have to re-run the script to change parameters... it just automatically updates.
Reducing time series data
Sometimes we will continuously gather data over a long time period, when only certain periods are of interest. Take for example the ESC data from the section... this was a power consumption test of a drone propeller where only the power during periods of static throttle are of interest, not the transient periods. Below you will find a PDF of a simple Live Script used to perform such a time series reduction. The Live Script and data are also included for you to play with if desired.
Reducing binned data
Sometimes we won't collect data over time, instead gathering it in a discrete bin or category. If there are a large number of bins it can take a long time to separate and perform calculations on this data. But fear not... MATLAB eats this sort of problem for breakfast thanks to logical indexing and the ability to loop. In this example, a data set containing noisy atmospheric pressure measurements gathered by 50 weather balloons is binned by altitude. Open the Live Script-exported PDF below to show how to deal with this sort of problem. The data and files are also included for you to explore: