My output text file continues to have a delimiter. How do I change this in my code?
4 次查看(过去 30 天)
显示 更早的评论
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 个评论
Sulaymon Eshkabilov
2024-1-5
Can you share a couple of sample data files so we can simulate and see the results from the code?
采纳的回答
Sulaymon Eshkabilov
2024-1-5
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 个评论
Image Analyst
2024-1-12
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. 😊
更多回答(1 个)
Image Analyst
2024-1-5
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!