How to write datetime(char array 1058*19), latitude(1*1058 double) and longitude (1*1058 double) in txt file using fprintf ???

1 回表示 (過去 30 日間)
I have written the following code but I didn't get datetime in text file.
datetime = {'2018-05-16 07:57:12','2018-05-16 07:57:13'......} (char array 1058*19),
latitude = [23.0347 23.0351 23.0353.....] (1*1058 double),
longitude = [72.5399 72.5398 7253.93 ....] (1*1058 double)
for y = 1:1:1058
AB(y,1) = datetime(y);
AB(y,2) = Lat(y);
AB(y,3) = Lon(y);
end
v = fopen('filename.txt','w');
fprintf(v,'\t%s\t\t%s\t\t%s\n','datetime','Lat','Lon');
%fprintf(v,'%s, %f, %f ',AB);
formatSpec = '%20s \t %2.2f \t %2.2f\n';
[nrows,ncols] = size(AB);
for row = 1:nrows
fprintf(v,formatSpec,AB(row,:));
end
  4 件のコメント
Stephen23
Stephen23 2018 年 5 月 25 日
@vishnu dhaker: the data really is in a char array, so your example should be:
['2018-05-16 07:57:12';'2018-05-16 07:57:13';...]
You can use the cellstr code I gave in my answer.
Vishnu Dhakad
Vishnu Dhakad 2018 年 5 月 25 日
Oh, sorry boss !!
Thanks for the correction.

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

採用された回答

Stephen23
Stephen23 2018 年 5 月 25 日
編集済み: Stephen23 2018 年 5 月 25 日
Assuming that the date-time char vectors are in a cell array:
>> fmt = '%20s \t %2.2f \t %2.2f\n';
>> DT = {'2018-05-16 07:57:12','2018-05-16 07:57:13','2018-05-16 07:57:14'};
>> LA = [23.0347 23.0351 23.0353];
>> LO = [72.5399 72.5398 7253.93];
>> C = DT;
>> C(2,:) = num2cell(LA);
>> C(3,:) = num2cell(LO);
>> fprintf(fmt,C{:})
2018-05-16 07:57:12 23.03 72.54
2018-05-16 07:57:13 23.04 72.54
2018-05-16 07:57:14 23.04 7253.93
Note that a loop is not required. If you really do have a char array with size 1058x19 then use this:
C = cellstr(DT).';
  3 件のコメント
Stephen23
Stephen23 2018 年 5 月 25 日
編集済み: Stephen23 2018 年 5 月 25 日
@vishnu dhaker: the data you uploaded is a column vector (1058x1 cell). The code that I gave you in my answer converts your char array to a row vector (1x1058 cell), so clearly you did not transpose the cell vector using the code given in my answer. My answer requires that all of the data are in row vectors (exactly like your example data).

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by