Importing Data in MATLAB

  • MATLAB
  • Thread starter Nusc
  • Start date
  • #1
753
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 seperated by the commas?

How would I be able to plot time vs. the third column?
 

Answers and Replies

  • #2
Wrichik Basu
Insights Author
Gold Member
2020 Award
1,710
1,555
Would I still have to run textscan to recognize the entries seperated 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.
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)
 

Related Threads on Importing Data in MATLAB

  • Last Post
Replies
1
Views
6K
Replies
1
Views
3K
Replies
5
Views
6K
Replies
1
Views
11K
Replies
6
Views
12K
  • Last Post
Replies
1
Views
4K
  • Last Post
Replies
1
Views
13K
  • Last Post
Replies
1
Views
6K
  • Last Post
Replies
4
Views
1K
Top