date format

13 ビュー (過去 30 日間)
ravi
ravi 2011 年 3 月 25 日
Hello All,
I have a data in a file with yyyy.mm.dd format. I want to read it and write it in a new file. This data is one column in the matrix of the data. any suggestions?
thanks

回答 (2 件)

Fangjun Jiang
Fangjun Jiang 2011 年 3 月 25 日
help datenum, datestr, for example datenum('2000.05.19','yyyy.mm.dd')
  3 件のコメント
Fangjun Jiang
Fangjun Jiang 2011 年 3 月 25 日
Can you provide an example of your data (a small sample maybe) and what end result you expect?
Jan
Jan 2011 年 3 月 25 日
@Ravi: DATENUM is the best method. Please explain with more details what "does not work" mean.

サインインしてコメントする。


Walter Roberson
Walter Roberson 2011 年 3 月 25 日
Do you just need to copy it to the new file, or do you need to change the format of the date as you go?
If you need to change the format beyond just re-arranging the parts (e.g., moving the year to the end) then datenum() is the way to go.
If you need to change the format with just re-arranging the parts and the parts are fixed length, then array indexing can be much more efficient.
If you are just copying without any re-arranging, then the biggest trick is in knowing how to output cell strings.
What output format do you need? Just a single column of dates? If so then sometimes xlswrite() is the easiest way: on Windows machines that have Excel installed it can write cell arrays into columns of output. If you are not using Windows or do not have Excel installed, then the easiest way to do the output can vary according to exactly what you want to output.
  2 件のコメント
ravi
ravi 2011 年 3 月 26 日
dear walter,
I have text file which has column with a format yyyy.mm.dd. I am using some other data from this text file to calculate something and then save the answer in the other text file. now i need to put this date column with the calculated answers. I hope that answer your querry about the format. I am using fprintf to write to the text file but I don't know how do I write this column. for example for floting ploint values %f, for string %s etc. what format I need to use to write a variable with yyyy.mm.dd format. Let me know if you still dont get it I can send you the file.thanks
Walter Roberson
Walter Roberson 2011 年 3 月 26 日
The easiest thing to do with that column is read it as a string, '%s' format, and write it with a %s format to the output file. If, that is, you do not need to use the data in your calculations.
If you need to use the date in your calculations and so have variables for the parts, then use
fprintf('%04d.%02d.%02d', year, month, day)

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeDates and Time についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by