Importing Data in MATLAB: Plotting Time vs. Third Column

  • Context: MATLAB 
  • Thread starter Thread starter Nusc
  • Start date Start date
  • Tags Tags
    Data Matlab
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
1 reply · 3K views
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)