Plotting Logged Serial Data (MATLAB/FreeMat)

From EmbeddedSand

Jump to: navigation, search
Temperature and Humidity plotted using FreeMat. Data is extracted from a log of the serial data coming from an Arduino captured using MegunoLinks serial logging tool.

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 build-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 jump over to the logging tab. Enter the filename and path for a textfile that you want to store the data in. Also decide if you want to overwrite or append. To begin logging click the logging tool icon. MegunoLink will then begin to write any serial data it receives to the text file.

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. % Basic Text File Read, Search, and Plot
  2. % Phil R
  3. % 13/5/2012
  4.  
  5. % When a text file is loaded the spaces are removed. Originally I had temperature and humidity logging
  6. % which was printed to the serial port like this "H: 63 % T: 22 *C". When its loaded in to freemat
  7. % it appears like this "H:63%T:22*C" so we will look for the "%T:" to indicate that the next two digits
  8. % are the logged temperature. These will then be extracted and plotted.
  9.  
  10. % User Settings
  11. clear all %start with fresh workspace
  12. dataPath = 'C:\Users\Phil\Desktop\';
  13. textFilename = 'arduinolog1.txt';
  14. widthOfTempdata = 2; %eg arduino prints T: 22*C, we are interested in the 22 so that is 2 digits
  15. temperatureDelimiter = '%T:'; %This appears before each temperature reading
  16.  
  17. % % Open the file and load it in to
  18. fp = fopen([dataPath textFilename]);
  19. loadedText = fscanf(fp,'%s');
  20.  
  21. % Find Indicies Where TemperatureDelimiter Exists In Loaded String
  22. matchIndex = strfind(loadedText, temperatureDelimiter);
  23.  
  24. % Loop Through and Store Found Temperatue Readings
  25. for i = 1:length(matchIndex)
  26.     loggedTemps(i) = str2num(loadedText((matchIndex(i)+length(temperatureDelimiter))...
  27.     :(matchIndex(i)+length(temperatureDelimiter))+widthOfTempdata-1));
  28. end
  29.  
  30. % Loop Through and Store Found Humidity Readings
  31. humidityDelimiter = 'H:'; %This appears before each temperature reading
  32. matchIndex = strfind(loadedText, humidityDelimiter);
  33. for i = 1:length(matchIndex)
  34.     loggedHumidity(i) = str2num(loadedText((matchIndex(i)+length(humidityDelimiter))...
  35.     :(matchIndex(i)+length(humidityDelimiter))+widthOfTempdata-1));
  36. end
  37.  
  38. % Plot The Resulting Temperatures
  39. figure(1);clf;
  40. subplot(2,1,1)
  41. plot(loggedTemps)
  42. title('Room Temperature','fontsize',13,'fontweight','bold');
  43. xlabel('Record Number','fontsize',10,'fontweight','bold');
  44. ylabel('Temperature [DegC]','fontsize',10,'fontweight','bold');
  45. axis([0 250 16 24])
  46.  
  47. % Plot The Resulting Humidity
  48. subplot(2,1,2)
  49. plot(loggedHumidity,'color','r')
  50. title('Room Humidity','fontsize',13,'fontweight','bold');
  51. xlabel('Record Number','fontsize',10,'fontweight','bold');
  52. ylabel('Temperature [DegC]','fontsize',10,'fontweight','bold');
  53. axis([0 250 60 70])
Personal tools