Question about headerlines commend
3 次查看(过去 30 天)
显示 更早的评论
Hi, everyone.
this is my code.
This code is originally intended to handle raw data in 14 columns.
I want to process raw data in two columns in this code.
When running this code, the Header = textscan (FID, '%s', HeaderLines); %GetHeader section says, "Invalid file ID. The error occurs with the phrase "Please use fopen to generate a valid file ID."
Which part should I revise?
Note that the data I want to apply to this code is in .str format and consists of two columns in total.
% READ AND PROC DATA
T_ObsTime = [];
T_DMSMixr = [];
a1 = double(input("Enter a slope of STD CAL: "));
b1 = double(input("Enter a Y-intercept of STD CAL: "));
for n = 1:length(DataList)-1
% READ
HeaderLines = 1; % the number of header lines
FID = fopen([DataFolder,'\',DataList(n)], 'r') ; % Set File ID
Header = textscan(FID, '%s', HeaderLines); % Get Header
% Header = textscan(FID, repmat(' %s', 1, 2), HeaderLines); % Get Header
% StartTime = datenum([char(Header{1}),' ',char(Header{2})],'mm/dd/yyyy HH:MM:SS');
T_Raw = textscan(FID, repmat(' %n', 1, 2)); % Get RAW data
fclose(FID);
% PROCESSING
GetTime = datetime(datevec(datenum(0,0,0,0,0,Tildas_Raw{1})...
+ datenum(1904,1,1,0,0,0) - datenum(0,0,0,9,0,0)));% convert UNIX time to MAT time + UTC (-9 hr)
T_ObsTime = [T_ObsTime; GetTime];
T_DMSMixr = [T_DMSMixr; ((Tildas_Raw{2}-b1)./a1)]; %cal factor 값 계산..
end
%% GET HOURLY MEAN DMS
% REMOVE OUTLIERS
% T_DMSMixr(TFrm) = NaN;
% SET HOURLY TIME
Get_DateVec = datevec(Tildas_ObsTime);
Get_DateVec(:,5:6) = 0;
SetTime = datevec([datenum(Get_DateVec(1,:)):datenum(0,0,0,1,0,0):datenum(Get_DateVec(end,:))]);
% PROC
for n = 1 : length(SetTime)
i = find(Get_DateVec(:,1) == SetTime(n,1) &...
Get_DateVec(:,2) == SetTime(n,2) &...
Get_DateVec(:,3) == SetTime(n,3) &...
Get_DateVec(:,4) == SetTime(n,4));
Get_1HDMS(n,1) = nanmean(T_DMSMixr(i).*1000,'all');
end
2 个评论
Siddharth Bhutiya
2023-10-27
I see that you are using datenum and datevec and doing manual conversions between different time representations and timezones. This is error prone and your life would be much easier if you do all this in datetime. For example, in the last for loop you seem to be converting datenums to datevecs and comparing individual fields, you could do this in one comparision if you use datetimes.
回答(1 个)
Voss
2023-10-24
Check why fopen in this line
FID = fopen([DataFolder,'\',DataList(n)], 'r')
returns -1 for FID.
Better yet, use the second output from fopen to have your code tell you what's wrong:
filename = [DataFolder,'\',DataList(n)];
[FID,ERRMSG] = fopen(filename, 'r');
if FID < 0
error('Error opening "%s":\n%s',filename,ERRMSG)
end
Check that the file name reported is what you expect and that the file exists.
The problem may be that DataList is not what you think it is. What is it?
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Time Series Objects 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!