I created a text file but, need it to include a title and time of when it was created.

6 ビュー (過去 30 日間)
This is my code. It works however, does not include the title on the new text file and doesn't include when it was created. Please help.
close all;
clear all;
clc;
Datafiles = fileDatastore("temp_summary*.txt","ReadFcn",@readMonth,"UniformRead",true);
opts =
DelimitedTextImportOptions with properties: Format Properties: Delimiter: {'\t'} Whitespace: '\b ' LineEnding: {'\n' '\r' '\r\n'} CommentStyle: {} ConsecutiveDelimitersRule: 'split' LeadingDelimitersRule: 'keep' TrailingDelimitersRule: 'ignore' EmptyLineRule: 'skip' Encoding: 'UTF-8' Replacement Properties: MissingRule: 'fill' ImportErrorRule: 'fill' ExtraColumnsRule: 'addvars' Variable Import Properties: Set types by name using setvartype VariableNames: {'Var1', 'Var2', 'Var3' ... and 6 more} VariableTypes: {'double', 'char', 'double' ... and 6 more} SelectedVariableNames: {'Var1', 'Var2', 'Var3' ... and 6 more} VariableOptions: [1-by-9 matlab.io.VariableImportOptions] Access VariableOptions sub-properties using setvaropts/getvaropts VariableNamingRule: 'modify' Location Properties: DataLines: [2 Inf] VariableNamesLine: 0 RowNamesColumn: 0 VariableUnitsLine: 0 VariableDescriptionsLine: 0 To display a preview of the table, use preview
Tbl = 31×4 table
Day MaxT MinT AvgT ___ ____ ____ ____ 1 65 12 38.5 2 68 28 48 3 65 17 41 4 57 22 39.5 5 46 24 35 6 61 18 39.5 7 62 25 43.5 8 58 12 35 9 64 11 37.5 10 65 14 39.5 11 54 22 38 12 58 40 49 13 64 27 45.5 14 65 19 42 15 59 19 39 16 62 23 42.5
year_str = '1981'
Tbl = 31×4 table
Day MaxT MinT AvgT ___________ ____ ____ ____ 01-May-0001 65 12 38.5 02-May-0001 68 28 48 03-May-0001 65 17 41 04-May-0001 57 22 39.5 05-May-0001 46 24 35 06-May-0001 61 18 39.5 07-May-0001 62 25 43.5 08-May-0001 58 12 35 09-May-0001 64 11 37.5 10-May-0001 65 14 39.5 11-May-0001 54 22 38 12-May-0001 58 40 49 13-May-0001 64 27 45.5 14-May-0001 65 19 42 15-May-0001 59 19 39 16-May-0001 62 23 42.5
dataAll = readall(Datafiles);
opts =
DelimitedTextImportOptions with properties: Format Properties: Delimiter: {'\t'} Whitespace: '\b ' LineEnding: {'\n' '\r' '\r\n'} CommentStyle: {} ConsecutiveDelimitersRule: 'split' LeadingDelimitersRule: 'keep' TrailingDelimitersRule: 'ignore' EmptyLineRule: 'skip' Encoding: 'UTF-8' Replacement Properties: MissingRule: 'fill' ImportErrorRule: 'fill' ExtraColumnsRule: 'addvars' Variable Import Properties: Set types by name using setvartype VariableNames: {'Var1', 'Var2', 'Var3' ... and 6 more} VariableTypes: {'double', 'char', 'double' ... and 6 more} SelectedVariableNames: {'Var1', 'Var2', 'Var3' ... and 6 more} VariableOptions: [1-by-9 matlab.io.VariableImportOptions] Access VariableOptions sub-properties using setvaropts/getvaropts VariableNamingRule: 'modify' Location Properties: DataLines: [2 Inf] VariableNamesLine: 0 RowNamesColumn: 0 VariableUnitsLine: 0 VariableDescriptionsLine: 0 To display a preview of the table, use preview
Tbl = 31×4 table
Day MaxT MinT AvgT ___ ____ ____ ____ 1 65 12 38.5 2 68 28 48 3 65 17 41 4 57 22 39.5 5 46 24 35 6 61 18 39.5 7 62 25 43.5 8 58 12 35 9 64 11 37.5 10 65 14 39.5 11 54 22 38 12 58 40 49 13 64 27 45.5 14 65 19 42 15 59 19 39 16 62 23 42.5
year_str = '1981'
Tbl = 31×4 table
Day MaxT MinT AvgT ___________ ____ ____ ____ 01-May-0001 65 12 38.5 02-May-0001 68 28 48 03-May-0001 65 17 41 04-May-0001 57 22 39.5 05-May-0001 46 24 35 06-May-0001 61 18 39.5 07-May-0001 62 25 43.5 08-May-0001 58 12 35 09-May-0001 64 11 37.5 10-May-0001 65 14 39.5 11-May-0001 54 22 38 12-May-0001 58 40 49 13-May-0001 64 27 45.5 14-May-0001 65 19 42 15-May-0001 59 19 39 16-May-0001 62 23 42.5
opts =
DelimitedTextImportOptions with properties: Format Properties: Delimiter: {'\t'} Whitespace: '\b ' LineEnding: {'\n' '\r' '\r\n'} CommentStyle: {} ConsecutiveDelimitersRule: 'split' LeadingDelimitersRule: 'keep' TrailingDelimitersRule: 'ignore' EmptyLineRule: 'skip' Encoding: 'UTF-8' Replacement Properties: MissingRule: 'fill' ImportErrorRule: 'fill' ExtraColumnsRule: 'addvars' Variable Import Properties: Set types by name using setvartype VariableNames: {'Var1', 'Var2', 'Var3' ... and 6 more} VariableTypes: {'double', 'char', 'double' ... and 6 more} SelectedVariableNames: {'Var1', 'Var2', 'Var3' ... and 6 more} VariableOptions: [1-by-9 matlab.io.VariableImportOptions] Access VariableOptions sub-properties using setvaropts/getvaropts VariableNamingRule: 'modify' Location Properties: DataLines: [2 Inf] VariableNamesLine: 0 RowNamesColumn: 0 VariableUnitsLine: 0 VariableDescriptionsLine: 0 To display a preview of the table, use preview
Tbl = 31×4 table
Day MaxT MinT AvgT ___ ____ ____ ____ 1 63 12 38.5 2 70 28 48 3 67 17 41 4 61 22 39.5 5 58 20 35 6 63 18 39.5 7 60 23 43.5 8 60 20 35 9 62 11 37.5 10 64 12 39.5 11 57 NaN NaN 12 60 23 49 13 66 30 45.5 14 63 19 42 15 61 16 39 16 64 21 42.5
year_str = '1982'
Tbl = 31×4 table
Day MaxT MinT AvgT ___________ ____ ____ ____ 01-May-0001 63 12 38.5 02-May-0001 70 28 48 03-May-0001 67 17 41 04-May-0001 61 22 39.5 05-May-0001 58 20 35 06-May-0001 63 18 39.5 07-May-0001 60 23 43.5 08-May-0001 60 20 35 09-May-0001 62 11 37.5 10-May-0001 64 12 39.5 11-May-0001 57 NaN NaN 12-May-0001 60 23 49 13-May-0001 66 30 45.5 14-May-0001 63 19 42 15-May-0001 61 16 39 16-May-0001 64 21 42.5
opts =
DelimitedTextImportOptions with properties: Format Properties: Delimiter: {'\t'} Whitespace: '\b ' LineEnding: {'\n' '\r' '\r\n'} CommentStyle: {} ConsecutiveDelimitersRule: 'split' LeadingDelimitersRule: 'keep' TrailingDelimitersRule: 'ignore' EmptyLineRule: 'skip' Encoding: 'UTF-8' Replacement Properties: MissingRule: 'fill' ImportErrorRule: 'fill' ExtraColumnsRule: 'addvars' Variable Import Properties: Set types by name using setvartype VariableNames: {'Var1', 'Var2', 'Var3' ... and 6 more} VariableTypes: {'double', 'char', 'double' ... and 6 more} SelectedVariableNames: {'Var1', 'Var2', 'Var3' ... and 6 more} VariableOptions: [1-by-9 matlab.io.VariableImportOptions] Access VariableOptions sub-properties using setvaropts/getvaropts VariableNamingRule: 'modify' Location Properties: DataLines: [2 Inf] VariableNamesLine: 0 RowNamesColumn: 0 VariableUnitsLine: 0 VariableDescriptionsLine: 0 To display a preview of the table, use preview
Tbl = 31×4 table
Day MaxT MinT AvgT ___ ____ ____ ____ 1 67 10 38.5 2 65 24 44 3 65 16 40.5 4 59 21 40 5 53 22 37 6 59 16 32.5 7 63 21 41 8 58 10 32 9 62 13 35.5 10 65 17 40 11 59 24 41 12 58 38 48 13 63 25 45.5 14 65 17 42 15 56 17 39 16 58 21 42.5
year_str = '1983'
Tbl = 31×4 table
Day MaxT MinT AvgT ___________ ____ ____ ____ 01-May-0001 67 10 38.5 02-May-0001 65 24 44 03-May-0001 65 16 40.5 04-May-0001 59 21 40 05-May-0001 53 22 37 06-May-0001 59 16 32.5 07-May-0001 63 21 41 08-May-0001 58 10 32 09-May-0001 62 13 35.5 10-May-0001 65 17 40 11-May-0001 59 24 41 12-May-0001 58 38 48 13-May-0001 63 25 45.5 14-May-0001 65 17 42 15-May-0001 56 17 39 16-May-0001 58 21 42.5
dataAll.Year = year(dataAll.Day);
dataAll.Month = month(dataAll.Day);
dataAll.DD = day(dataAll.Day);
% Unstack variables
minT_tbl = unstack(dataAll,"MinT","Year","GroupingVariables", ["Month","DD"],"VariableNamingRule","preserve");
maxT_tbl = unstack(dataAll,"MaxT","Year","GroupingVariables", ["Month","DD"],"VariableNamingRule","preserve");
yrs =str2double(minT_tbl.Properties.VariableNames(3:end))';
% find min
[Tmin,idxMn] = min(minT_tbl{:,3:end},[],2,'omitnan');
Tmin_yr = yrs(idxMn);
% find max
[Tmax,idxMx] = max(maxT_tbl{:,3:end},[],2,'omitnan');
Tmax_yr = yrs(idxMx);
% find low high
[lowTMax,idxMx] = min(maxT_tbl{:,3:end},[],2,'omitnan');
LowTMax_yr = yrs(idxMx);
% find high low
[highlowTMn,idxMn] = max(minT_tbl{:,3:end},[],2,'omitnan');
HighLowT_yr = yrs(idxMn);
% find avg high
AvgTMx = round(mean(table2array(maxT_tbl(:,3:end)),2,'omitnan'));
% find avg low
AvgTMn = round(mean(table2array(minT_tbl(:,3:end)),2,'omitnan'));
% Results
tempTbl = [maxT_tbl(:,["Month","DD"]), table(Tmax,Tmax_yr,AvgTMx,lowTMax,LowTMax_yr,Tmin,Tmin_yr,AvgTMn,highlowTMn,HighLowT_yr)];
tempTbl2 = splitvars(tempTbl);
time = datestr(clock,'YYYY/mm/dd HH:MM');
FID = fopen('Meda 05 Temperature Climatology.txt','w');
title('Meda 05 Temperature Climatology');
fprintf(FID,"Month DD Tmax Tmax_yr AvgTMx lowTMax LowTMax_yr Tmin Tmin_yr AvgTMn highlowTMn HighlowT_yr \n");
fprintf(FID,'%3d %6d %5d %8d %8d %8d %11d %9d %9d %9d %9d %11d \n', tempTbl2{:,1:end}');
fclose(FID);
winopen('Meda 05 Temperature Climatology.txt')
Error using winopen
The WINOPEN function is available only on Microsoft Windows.
function Tbl = readMonth(filename)
opts = detectImportOptions(filename)
opts.ConsecutiveDelimitersRule = 'join';
opts.MissingRule = 'omitvar';
opts = setvartype(opts,'double');
opts.VariableNames = ["Day","MaxT","MinT","AvgT"];
Tbl = readtable(filename,opts);
Tbl = standardizeMissing(Tbl,{999,'N/A'},"DataVariables",{'MaxT','MinT','AvgT'})
[~,basename] = fileparts(filename);
nameparts = regexp(basename, '\.', 'split');
dateparts = regexp(nameparts{end}, '_','split');
year_str = dateparts{end}
d = str2double(extract(filename,digitsPattern));
Tbl.Day = datetime(d(3),d(2),Tbl.Day)
end

採用された回答

Walter Roberson
Walter Roberson 2024 年 1 月 17 日
Change
title('Meda 05 Temperature Climatology');
to
report_date = datetime('now', 'Format', 'yyyy-MM-dd');
fprintf(FID, 'Meda 05 Temperature Climatology at %s\n', report_date);
Change the Format to whatever is appropriate for your situation.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeData Import and Analysis についてさらに検索

タグ

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by