Importing Data in MATLAB: Plotting Time vs. Third Column

  • Context: MATLAB 
  • Thread starter Thread starter Nusc
  • Start date Start date
  • Tags Tags
    Data Matlab
Click For Summary
SUMMARY

This discussion focuses on importing and plotting data in MATLAB from a CSV file, specifically extracting time and a third column of values. The user demonstrates how to read data using the fopen and textscan functions, and subsequently manipulates the data to extract relevant time and value entries. The provided code effectively converts string data into datetime objects for plotting, utilizing functions such as extractBefore, extractBetween, and extractAfter. The final output is a plot of time versus the third column values.

PREREQUISITES
  • Familiarity with MATLAB programming
  • Understanding of CSV file structure and data manipulation
  • Knowledge of MATLAB string functions
  • Basic plotting techniques in MATLAB
NEXT STEPS
  • Learn about MATLAB's datetime function for date and time manipulation
  • Explore advanced data import techniques using readtable in MATLAB
  • Investigate MATLAB's plotting functions for enhanced data visualization
  • Study string manipulation functions in MATLAB for more complex data processing
USEFUL FOR

This discussion is beneficial for MATLAB users, data analysts, and researchers who need to import, manipulate, and visualize time-series data from CSV files.

Nusc
Messages
752
Reaction score
2
Hi. Suppose I load data from a .csv file:

Blah
@Blah
2Blah
Blah
f Blah
fa Blah
Blahasd
Blahasda
vasf Blah
as Blah
asdas Blah



Date Time Ch1:
07/25/2012 42:46.0 96.385
07/25/2012 42:46.0 -177.608
07/25/2012 42:46.0 155.481
07/25/2012 42:46.0 64.15

an attempt would be to manipulate the data first before I read it, things would be much more subtle:

fid = fopen('PULL1.CSV');

data = textscan(fid, '%s' ,'delimiter', '\b');

data2 = data{:,:}

data3 = data2(13:end,:);

fclose(fid);

-----

The output then is:

'07/25/2012,17:49:56.080,37.288'
'07/25/2012,17:49:56.085,37.288'
'07/25/2012,17:49:56.090,53.405'Would I still have to run textscan to recognize the entries separated by the commas?

How would I be able to plot time vs. the third column?
 
Physics news on Phys.org
Nusc said:
Would I still have to run textscan to recognize the entries separated by the commas?
Once you have the data in data3, you don't have to run textscan again. Just use string operations to take out each substring.
Nusc said:
How would I be able to plot time vs. the third column?
The following code will work:
Matlab:
data3 = string(data3);
for i = 1:size(data3)
    index = strfind(data3(i), ',');
    date = extractBefore(data3(i), index(1));
    t = extractBetween(data3(i),index(1)+1,index(2)-1);
    val(i) = extractAfter(data3(i),index(2)+1);
    total = convertStringsToChars(strcat(date," ",t));
    time(i) = datetime(total, 'InputFormat', 'MM/dd/yyyy HH:mm:ss.SSS');
end
plot(time,val)
 

Similar threads

  • · Replies 8 ·
Replies
8
Views
3K
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
5K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 9 ·
Replies
9
Views
5K