Question about headerlines commend
3 views (last 30 days)
Show older comments
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 Comments
Siddharth Bhutiya
on 27 Oct 2023
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.
Answers (1)
Voss
on 24 Oct 2023
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 Comments
See Also
Categories
Find more on Time Series Objects in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!