My output text file continues to have a delimiter. How do I change this in my code?
4 views (last 30 days)
Show older comments
Jonathon Klepatzki
on 4 Jan 2024
Commented: Jonathon Klepatzki
on 16 Jan 2024
I have to create a text file of results across a 40 year period. The output file should not have a delimiter. However, even with a couple tweeks, it continues to produce the same result. How do I fix this?
close all;
clear all;
clc;
Datafiles = fileDatastore("temp_summary*.txt","ReadFcn",@readMonth,"UniformRead",true);
dataAll = readall(Datafiles)
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
[minT,idxMn] = min(minT_tbl{:,3:end},[],2);
minT_yr = yrs(idxMn);
% find max
[maxT,idxMx] = max(maxT_tbl{:,3:end},[],2);
maxT_yr = yrs(idxMx);
% find low high
[lowMaxT,idxMx] = min(maxT_tbl{:,3:end},[],2);
LowMaxT_yr = yrs(idxMx);
% find high low
[highlowMnT,idxMn] = max(minT_tbl{:,3:end},[],2);
HighLowT_yr = yrs(idxMn);
% find avg high
AvgMxT = mean(table2array(maxT_tbl),2);
% find avg low
AvgMnT = mean(table2array(minT_tbl),2);
% Results
tempTbl = [maxT_tbl(:,["Month","DD"]), table(maxT,maxT_yr,AvgMxT,lowMaxT,LowMaxT_yr,minT,minT_yr,AvgMnT,highlowMnT,HighLowT_yr)]
tempTbl2 = splitvars(tempTbl)
writetable(tempTbl2,'Meda 05 Temperature Climatology.txt','Delimiter',' ')
type 'Meda 05 Temperature Climatology.txt'
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
2 Comments
Sulaymon Eshkabilov
on 5 Jan 2024
Can you share a couple of sample data files so we can simulate and see the results from the code?
Accepted Answer
Sulaymon Eshkabilov
on 5 Jan 2024
Hi Jonathon,
Your code is working as it should :).
...
% One delimeter is put here and therefore, it is outputting one empty space delimeter betweend data points
writetable(tempTbl2,'Meda 05 Temperature Climatology.txt','Delimiter',' ')
type 'Meda 05 Temperature Climatology.txt'
...
A couple of ideas here to make the output file better looking and structured.
(1) Using a tab as a delimeter:
...
writetable(tempTbl2,'Meda 05 Temperature Climatology.txt','Delimiter','\t')
type 'Meda 05 Temperature Climatology.txt'
...
(2) Using MS Excel file for the augmented data output.
...
writetable(tempTbl2,'Meda 05 Temperature Climatology0.xlsx')
winopen('Meda 05 Temperature Climatology0.xlsx')
...
That makes the whole augmented data better structured and better looking :)
17 Comments
Image Analyst
on 12 Jan 2024
And since the idea of using fprintf was originally mine, could you please "Vote" for my answer below to award reputation points? Thanks in advance. 😊
More Answers (1)
Image Analyst
on 5 Jan 2024
You can go through each row of the table using fprintf to write whatever you want into a text file. Just don't write delimiters if you don't want them but I'd recommend using one, such as a space, tab, or comma so all the data doesn't run together in one long string.
0 Comments
See Also
Categories
Find more on Matrix Indexing 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!