MegunoLink’s data logging tool can be used to save serial data sent by your Arduino to a text file. Matlab and FreeMat are helpful tools for further processing the stored data. This could be a simple as plotting your results (if you don’t want to use the built-in plotting tool) or a more complex data analysis.
In this example, an Arduino was set-up to measure temperature and humidity, sending the measurements to a host computer every minute. MegunoLink stored the data in a text file and now we will plot it with FreeMat. The same process can be used in Matlab too.
Logging Serial Data
Once a serial connection has been established in MegunoLink add a Log to File visualiser. Enter the filename and path for a textfile that you want to store the data in. To begin logging click the logging tool icon. MegunoLink will then begin to write any serial data it receives to the text file.
MegunoLink Pro’s message logging feature makes this even easier. Simply send comma separated text to the message logger which will log to its own file ignoring all other serial data. This can then be directly imported using Matlab and FreeMat’s built in CSVRead function. We will update this tutorial showing this functionality shortly.
Plotting Data (MATLAB/FreeMat)
For more information about the plotting software click on either MATLAB or FreeMat. MATLAB is a commercial application (student versions are often available at a reduced rate for non-commercial use) but FreeMat is completly free and can be immediately downloaded from FreeMat’s website here.
Download the MATLAB/FreeMat script and data file to plot data logged by an Arduino here.
% Basic Text File Read, Search, and Plot
% Phil R
% When a text file is loaded the spaces are removed. Originally I had temperature and humidity logging
% which was printed to the serial port like this "H: 63 % T: 22 *C". When its loaded in to freemat
% it appears like this "H:63%T:22*C" so we will look for the "%T:" to indicate that the next two digits
% are the logged temperature. These will then be extracted and plotted.
% User Settings
clear all %start with fresh workspace
dataPath = 'C:\Users\Phil\Desktop\';
textFilename = 'arduinolog1.txt';
widthOfTempdata = 2; %eg arduino prints T: 22*C, we are interested in the 22 so that is 2 digits
temperatureDelimiter = '%T:'; %This appears before each temperature reading
% % Open the file and load it in to
fp = fopen([dataPath textFilename]);
loadedText = fscanf(fp,'%s');
% Find Indicies Where TemperatureDelimiter Exists In Loaded String
matchIndex = strfind(loadedText, temperatureDelimiter);
% Loop Through and Store Found Temperatue Readings
for i = 1:length(matchIndex)
loggedTemps(i) = str2num(loadedText((matchIndex(i)+length(temperatureDelimiter))...
% Loop Through and Store Found Humidity Readings
humidityDelimiter = 'H:'; %This appears before each temperature reading
matchIndex = strfind(loadedText, humidityDelimiter);
for i = 1:length(matchIndex)
loggedHumidity(i) = str2num(loadedText((matchIndex(i)+length(humidityDelimiter))...
% Plot The Resulting Temperatures
axis([0 250 16 24])
% Plot The Resulting Humidity
axis([0 250 60 70])