How do you load a .csv file, skip lines within it, and plot the values?

Hi. I seem to have problems loading a .csv file (see attached):
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
%Here is my code:
fid = fopen('PULL1.CSV');
% I need to skip the 16 lines. header = textscan(fid, '%s',3,'delimiter', ',',... 'headerLines',16);
data = textscan(fid, '%s %s %s' ,'delimiter', ',');
The problem is that it only recognizes the parameters as strings. Date should be a string. Time should be a floating number. The Ch1 column is a set of values that I want to plot vs. time.
fclose(fid);

1 Comment

Also bare in mind that under column Ch1: the time 42:46.0 represents 5:42:46 PM but I am not sure why Excel automatically displays it as shown in the first post.

Sign in to comment.

 Accepted Answer

data_to_plot = str2double(data{3});

15 Comments

I would like to plot these values vs. time. When I perform your suggested command, it changes the value.
Which value was changed?
Anyhow, to plot versus time,
datatimes = datenum(data{2}, 'mm:ss.f');
plot(datetimes, data_to_plot);
So,
fid = fopen('PULL1.CSV');
% read column headers
header = textscan(fid, '%s',3,'delimiter', ',',... 'headerLines',16);
% read numeric data
data = textscan(fid, '%s %s %s' ,'delimiter', ',');
data_to_plot = str2double(data{3});
datatimes = datenum(data{2}, 'mm:ss.f');
plot(datetimes, data_to_plot);
fclose(fid);
------
Gives the following output:
Error using datenum (line 181) DATENUM failed.
Error in ABL (line 20) datatimes = datenum(data{2}, 'mm:ss.f');
Caused by: Error using dtstr2dtnummx Failed on converting date string to date number.
-------
Nevertheless,
perhaps if I 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?
Try with 'mm:ss.fff' instead of 'mm:ss.f'
Is the 17: present in the file? If so then use 'hh:mm:ss.fff'
Sorry, which code are you referring to?
The first or the second? Because with the second, I have not yet implemented 'mm:ss.f' other than what you already provided, which did not work for 'hh:mm:ss.fff' either.
Your second remark, take one cell for example. It would show up as 42:46.0
When you click on this cell it shows up as 5:42:46 PM
When I run it through the matlab code it shows up as 17:42:46.000
It did not work for 'hh:mm:ss.fff' either. Here is the code and it's output:
fid = fopen('PULL1.CSV');
% read column headers
header = textscan(fid, '%s',3,'delimiter', ',',...
'headerLines',16);
% read numeric data
data = textscan(fid, '%s %s %s' ,'delimiter', ',');
data_to_plot = str2double(data{3});
datatimes = datenum(data{2}, 'hh:mm:ss.fff');
plot(datetimes, data_to_plot);
fclose(fid);
------output-----
Error using datenum (line 181)
DATENUM failed.
Error in ABL (line 22)
datatimes = datenum(data{2}, 'hh:mm:ss.fff');
Caused by:
Error using dtstr2dtnummx
Failed on converting date string to date number.
Please put a breakpoint in at the datatimes calculation, and execute the code. When it stops, please show the value of data{2}{1} and try
datenum(data{2}{1}, 'hh:mm:ss.fff')
I have tried with the data you have shown and it works fine for me. If this works fine for you as well, then the implication would be that one of your data{2} values is not in the expected format, and you would then need to track down why not. If it does not work for you with the one string data{2}{1} then we need to see that string and we need to know your MATLAB version.
data{1}{2} returns:
ans =
07/25/2012
When you say breakpoint do you mean tbreak(t0) or tbreak t0 or evbreak(evid) or dbstop?
What MATLAB version are you using?
I didn't try the breakpoint but my version is R2011b.
I found out why, it should have been HH:MM:SS.FFF the lower case doesn't work. But now I get the following error:
Undefined function or variable 'datetimes'.
Error in ABL (line 33) plot(datetimes, data_to_plot);
Nevermind. I should have used datatimes not datetimes.
My x-axis shows the value 7.3487 x10^5 repeatedly. What is the significance of using datenum if what I want is to show the time as it is in the Excel sheet?
Use datetick(). You will want to be selective about which time fields you display, as otherwise the tick labels will run into each other.

Sign in to comment.

More Answers (1)

Try
data = textscan( fid, '%s%s%f', 'Headerlines', 16 );
Delimiter is obviously one or many "space" or tab. Default handles that.

Categories

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!