I have an excel file ... which consists of 1st column is dates and time and other n rows and n columns data.... in this file, I have daily 1-hour data, I want day night data separate for the reference I have attached output file. Please find input an

1 回表示 (過去 30 日間)
Please see the input and output file attachment for reference.
  3 件のコメント
Sunil Oulkar
Sunil Oulkar 2017 年 1 月 17 日
from morning 6 am to 5:59 pm day and 6 pm to 5:59 am night
Sunil Oulkar
Sunil Oulkar 2017 年 1 月 17 日
I want day data and night data in two separate file

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

採用された回答

Peter Perkins
Peter Perkins 2017 年 1 月 19 日
I recommend using readtable, using the %D format to create datetimes. Then it's easy to select rows of the table by day and night using timeofday. Then use writetable to write out the separate data.
  1 件のコメント
John Chilleri
John Chilleri 2017 年 1 月 19 日
I'm going to have to check this out for myself! Always enlightening to hear of built in solutions!

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

その他の回答 (1 件)

John Chilleri
John Chilleri 2017 年 1 月 18 日
編集済み: John Chilleri 2017 年 1 月 18 日
Hello,
Here is code that does what you want. You'll need to have two blank files in your directory named "outputfileDAY.csv" and "outputfileNIGHT.csv" - it will write day and night to these files respectively. Furthermore, you need to have "inputfile.csv" in your directory from which the code will pull the data, please have it in csv format. Note that your input/output files have a different number of entries so it was weird to compare, but I checked the results and the code worked:
% Code to separate by time
%
% Acquiring and splitting data
fid = fopen('inputfile.csv','rt');
dayandnight = textscan(fid,'%s %f %f %f %f', 'Delimiter', ',',...
'CollectOutput', 1);
%
% Acquiring times from date
fileid1 = fopen('outputfileDAY.csv','w');
fileid2 = fopen('outputfileNIGHT.csv','w');
for i = 1:size(dayandnight{1},1)
Time = dayandnight{1}{i}(end);
j = 0;
while (Time(1) ~= ' ')
Time = dayandnight{1}{i}(end-j:end);
j = j+1;
end
Time = Time(2:end);
if (Time(2) == ':')
Time = Time(1);
else
Time = Time(1:2);
end
if ((str2num(Time) >= 6)&&(str2num(Time) < 18))
fprintf(fileid1,'%s,%f,%f,%f,%f\n',dayandnight{1}{i},dayandnight{2}(i,1),...
dayandnight{2}(i,2),dayandnight{2}(i,3),dayandnight{2}(i,4));
else
fprintf(fileid2,'%s,%f,%f,%f,%f\n',dayandnight{1}{i},dayandnight{2}(i,1),...
dayandnight{2}(i,2),dayandnight{2}(i,3),dayandnight{2}(i,4));
end
end
fclose(fileid1);
fclose(fileid2);
Please comment if you have any questions!
Hope this helps!
  2 件のコメント
Jan
Jan 2017 年 1 月 29 日
strtim is more efficient and clear than the while (Time(1) ~= ' ') loop.
This is a straight and clear approach, +1.
John Chilleri
John Chilleri 2017 年 1 月 29 日
Never heard of strtrim before, I'll be sure to incorporate that into my programming!
Thanks for the knowledge!

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

カテゴリ

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