Effacer les filtres
Effacer les filtres

My output text file continues to have a delimiter. How do I change this in my code?

4 vues (au cours des 30 derniers jours)
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

Réponse acceptée

Sulaymon Eshkabilov
Sulaymon Eshkabilov le 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 commentaires
Image Analyst
Image Analyst le 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. 😊

Connectez-vous pour commenter.

Plus de réponses (1)

Image Analyst
Image Analyst le 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.

Catégories

En savoir plus sur Just for fun dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by