MATLAB Importing Data in MATLAB: Plotting Time vs. Third Column

  • Thread starter Thread starter Nusc
  • Start date Start date
  • Tags Tags
    Data Matlab
AI Thread Summary
The discussion focuses on importing and manipulating data from a CSV file in MATLAB, specifically for plotting time against a third column of values. Users are guided on how to read the data using `textscan` and how to process it to extract date and time information. Once the data is formatted correctly, string operations can be used to separate the components without needing to run `textscan` again. The provided code demonstrates how to convert the extracted strings into a datetime format suitable for plotting. Ultimately, the conversation emphasizes the importance of data manipulation for effective visualization in MATLAB.
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

Back
Top