Info
此问题已关闭。 请重新打开它进行编辑或回答。
I want my code to read from a csv file, the csv file (dee1) has 2 columns which the first one is Time_stamps and second one is load value . but the output of the function only read time_stamps. what should I do to fix it ?
1 次查看(过去 30 天)
显示 更早的评论
function [load_time_stamp, load_value]=get_the_actual_demand_data()
filename = 'dee1.csv';
delimiter = ',';
startRow = 2;
formatSpec = '%{MM-dd-yy HH:mm}D%*s%[^\n\r]';
fileID = fopen(filename,'r');
dataArray = textscan(fileID, formatSpec, 'Delimiter', delimiter, 'HeaderLines' ,startRow-2);
a = datestr(dataArray{1,1},'yyyy-mm-dd HH:MM');
load_time_stamp = cellstr(a);
fclose(fileID);
formatSpec = '%*s%f%[^\n\r]';
fileID = fopen(filename,'r');
dataArray = textscan(fileID, formatSpec, 'Delimiter', delimiter, 'HeaderLines' ,startRow-2);
load_value = dataArray{:, 1};
fclose(fileID);
end
4 个评论
Stephen23
2018-3-10
"I uploaded the file too."
Nope. You uploaded a screen shot. A screen shot is useless to us, because we cannot load it. Please upload a sample file, as requested.
回答(1 个)
dpb
2018-3-10
编辑:dpb
2018-3-10
The format string doesn't match the record; particularly the datetime format...
>> fmt = '%{M/d/yyyy HH:mm}D %f';
>> dat = textscan(fid,fmt, 'Delimiter',',','HeaderLines',1);
>> dat
dat =
1×2 cell array
[76660×1 datetime] [76660×1 double]
>> dat{1}(1:4)
ans =
4×1 datetime array
1/23/2015 08:00
1/24/2015 08:00
1/25/2015 08:00
1/26/2015 08:00
>>
You must pay attention to detail...
1 个评论
dpb
2018-3-10
编辑:dpb
2018-3-10
Just showed you what needs changing; doesn't help the learning process to rely on somebody else to do what you can do for yourself...what you do in the function specifically will be dependent upon how you want to treat the data later; if it were me I'd probably return a table but that would also depend on what is needed to be done with the data going forward.
>> t=table(dat{1},dat{2},'VariableNames',{'Time','Demand'});
>> whos t
Name Size Bytes Class Attributes
t 76660x2 1840998 table
>> t(1:4,:)
ans =
Time Demand
_______________ ______
1/23/2015 08:00 254.4
1/24/2015 08:00 259.2
1/25/2015 08:00 265.6
1/26/2015 08:00 267.2
>>
Alternatively, return just the datetime and associated demand data as arrays of native types...
>> Time=dat{1};
Demand=dat{2};
>> whos dt dmnd
Name Size Bytes Class Attributes
Demand 76660x1 613280 double
Time 76660x1 1226589 datetime
>> Time(1:4)
Time =
10×1 datetime array
1/23/2015 08:00
1/24/2015 08:00
1/25/2015 08:00
1/26/2015 08:00
>> Demand(1:4)
Demand =
254.4000
259.2000
265.6000
267.2000
>>
ADDENDUM
And if decide to use table (highly recommended in general) then it's even simpler with start with that goal in mind--
>> t=readtable(filename,'ReadVariableNames',false,'Format',fmt);
>> t.Properties.VariableNames={'Time','Demand'};
>> whos t
Name Size Bytes Class Attributes
t 76661x2 1841022 table
>> t(1:4,:)
ans =
Time Demand
_______________ ______
1/22/2015 08:00 257.6
1/23/2015 08:00 254.4
1/24/2015 08:00 259.2
1/25/2015 08:00 265.6
>>
This is simple enough really don't need a function; just including the few (four or five, roughly) lines of code in line in the main routine isn't much distraction but, if you do decide to do so, do NOT hardcode the name of the file inside the routine...putting such "magic numbers" or constants inside code defeats the entire purpose of having arguments available; you have to actually change the code itself to use a different file. Separate data from code and the input file is data.
此问题已关闭。
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!