LoveTW
LoveTW

Reputation: 3832

How can i display date in a 1 by 1 array when i use datestr?

I use datestr to transfer 732154.873773148 into 26-Jul-2004 20:58:14, but when i ouput it to a .csv file, it will be an 1*20 array..., but what i want is to store it in a 1*1 array, how should i do?

And does there exist easy method to transfer 26-Jul-2004 20:58:14 to 2004/07/26 20:58:14?

Thanks:)

data = csvread('4.csv');
date = data(:, 1);
loc = data(:, 2);
record_num = size(loc, 1);

result = zeros(record_num, 2);
new_date = datestr(date);
csvwrite('4_res.txt', new_date);

what i want is 2004/07/26 20:58:14

but it will be 2,0,0,4,/,0,7,/,2,6, ,2,0,:,5,8,:,1,4

Upvotes: 2

Views: 296

Answers (2)

H.Muster
H.Muster

Reputation: 9317

In order to have the date in the format 2004/07/26 20:58:14 you can use:

new_date = datestr(date,'mm/dd/yy HH:MM:SS');

To write a csv file with only a 1x1 cell you can use dlmwrite with the empty string as delimiter :

dlmwrite('4_res.txt',new_date, 'delimiter','')

Upvotes: 4

High Performance Mark
High Performance Mark

Reputation: 78316

Your problem is that, for Matlab, a string IS a character array. As you've discovered csvwrite writes your 'string' as an array of integers (the ASCII codes of the characters in the string). I don't think there is a way to force csvwrite to behave differently.

I think you will have to use fprintf, like this

fprintf(fid,datestr('26-Jul-2004 20:58:14'))

to get the output you want. Here, fid is, of course, a handle for a file which you have opened (using fopen) with write access enabled. As to writing the date string in the format you want, try:

fprintf(fid,datestr('26-Jul-2004 20:58:14', 'yyyy/mm/dd HH:MM:SS'))

Upvotes: 3

Related Questions