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!!!
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)
Select the top right cell and paste in your data using Ctrl+C.
You don't have to save the variable, it does so automatically.
Return to Notepad and undo your changes to the header or close the file without saving so you don't accidentally modify it forever.
Unless you want to keep the full ESC
array variable for any reason, delete it by right clicking on it in the Workspace and hitting Delete
.
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.
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.
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 Importing Data for MATLAB 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.
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:
In the MATLAB Workspace, right click New
and give your variable a meaningful name. In this case I chose ESC for obvious reasons. This will create a new scalar number with 0 value.
Double click on ESC and it will open up in the Variables viewer where you can see its value.
In MATLAB, create new variables for each column in the array in the command line: Note that the first item in the parentheses defines the row of interest, with the second defining the column of interest. Using a colon instead of a number means 'select all'.
Save the data in a .mat file so you don't have to repeat the above steps for later use - do this by right-clicking anywhere in the Workspace, hitting Save, and choosing a meaningful name (I called mine ESC
again).
To load the data in the future you can simply double click on the .mat file in Current Folder
tab, having navigated to that folder first.
Alternatively, you can do this from the Command Line or in a Script by calling the load() function: