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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
% Basic Text File Read, Search, and Plot % Phil R % 13/5/2012 % 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))... :(matchIndex(i)+length(temperatureDelimiter))+widthOfTempdata-1)); end % 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))... :(matchIndex(i)+length(humidityDelimiter))+widthOfTempdata-1)); end % Plot The Resulting Temperatures figure(1);clf; subplot(2,1,1) plot(loggedTemps) title('Room Temperature','fontsize',13,'fontweight','bold'); xlabel('Record Number','fontsize',10,'fontweight','bold'); ylabel('Temperature [DegC]','fontsize',10,'fontweight','bold'); axis([0 250 16 24]) % Plot The Resulting Humidity subplot(2,1,2) plot(loggedHumidity,'color','r') title('Room Humidity','fontsize',13,'fontweight','bold'); xlabel('Record Number','fontsize',10,'fontweight','bold'); ylabel('Temperature [DegC]','fontsize',10,'fontweight','bold'); axis([0 250 60 70]) |