Clear Filters
Clear Filters

how to add formatspec in writematrix

21 views (last 30 days)
adhi dermawan
adhi dermawan on 21 Mar 2023
Commented: Rik on 21 Mar 2023
Dear matlab expert, I have these script
clc;clear; close all;
% Specify the folder where the files live.
myFolder = 'C:\ZTD\Wetz dan Dryz\DryZ\DryZ';
% Check to make sure that folder actually exists. Warn user if it doesn't.
if ~isfolder(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s\nPlease specify a new folder.', myFolder);
uiwait(warndlg(errorMessage));
myFolder = uigetdir(); % Ask for a new one.
if myFolder == 0
% User clicked Cancel
return;
end
end
Allfile = fullfile(myFolder, '*.txt'); % Change to whatever pattern you need.
List = dir(Allfile);
for k = 1 : length(List)
sites = List(k).name;
num = readmatrix(sites);
num(:,end)=[];
% % % sum up dryz+wetz in txt file
zwd1 = num(1:end,2);
zwd2 = num(1:end,3);
zwd3 = num(1:end,4);
% Seconds since 2000-01-01 00:00:00.0
time_sec2000 = num(1:end,1);
% % standard deviation values
% stdev = num(1:2:end,4);
% % mjd corresponding to 2000-01-01 00:00:00.0
mjd20000 = 51544;
% % Decimal days since 2000-01-01 00:00:00.0
time_days = time_sec2000/86400;
% % time vector in mjd
MJD = time_days + mjd20000;
% % compute decimal year
[year, month, day, hour, minute, second] = datevec(MJD + 678942);
% % time vector in decimal year
YYF = decyear([year, month, day, hour, minute, second]);
% PWV = zwd*0.15;
newdata = [MJD YYF zwd1 zwd2 zwd3];
% %save data
writematrix (newdata,sites,'Delimiter','space')
end
I want my output file have formatspec using fprintf with MJD %15.8f YYF %14.8f and zwd1,zwd2,zwd3 %18.16f and I want to change the output filename to filename_out using writematrix. How can I do it?
  1 Comment
Rik
Rik on 21 Mar 2023
If you have a strong opinion on the formatting, why don't you use fprintf instead of writematrix?
(just remember that MatrixWithData(:) will go through your array column by column, while you will read your text file row by row)

Sign in to comment.

Answers (0)

Categories

Find more on Data Import and Export in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!