1

I have the following sample from a CSV file. Structure is:

Date      ,Time(Hr:Min:S:mS), Value
2015:08:20,08:20:19:123     , 0.05234
2015:08:20,08:20:19:456     , 0.06234

I then would like to read this into a matrix in MATLAB.

Attempt :

Matrix = csvread('file_name.csv');

Also tried an attempt formatting the string.

fmt = %u:%u:%u %u:%u:%u:%u %f
Matrix = csvread('file_name.csv',fmt);

The problem is when the file is read the format is wrong and displays it differently.

Any help or advice given would be greatly appreciated!

EDIT

When using @Adriaan answer the result is

2015 -11 -9
8    -17 -1

So it seems that MATLAB thinks the '-' is the delimiter(separator)

1 Answer 1

2
Matrix = csvread('file_name.csv',1,0);

csread does not support a format specifier. Just enter the number of header rows (I took it to be one, as per example), and number of header columns, 0.

You file, however, contains non-numeric data. Thus import it with importdata:

 data = importdata('file_name.csv')

This will get you a structure, data with two fields: data.data contains the numeric data, i.e. a vector containing your value. data.textdata is a cell containing the rest of the data, you need the first two column and extract the numerics from it, i.e.

for ii = 2:size(data.textdata,1)
    tmp1 = data.textdata{ii,1};
    Date(ii,1) = datenum(tmp1,'YYYY:MM:DD');
    tmp2 = data.textdata{ii,2};
    Date(ii,2) = datenum(tmp2,'HH:MM:SS:FFF');
end

Thanks to @Excaza it turns out milliseconds are supported.

Sign up to request clarification or add additional context in comments.

4 Comments

Please see the edit when your solution was attempted :)
@user3536870 I do not understand how you got your numbers from the provided sample data using the method provided here
I got the result by using Matrix = csvread('filename.csv',1,0); The loop wasn't in the answer when I attempted this and then it gave the result in my edit for the question. And no headers
@user3536870 the looped version seems to work OK. Note that in case your header is not included you need to run the for loop from 1, and not 2. Everything else can remain as put

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.