How to write data into .txt file

How should i write the AVG and standard deviation data into .txt file?
d = 'E:\\ \\ DummyFile\\';
filePattern = fullfile(d, 'Run*');
file = dir(filePattern)
n = length(file)
for k = 1:n
baseFileName = file(k).name;
FullFileName = fullfile(d, baseFileName, 'dummy.csv');
temp = readtable(FullFileName, 'ReadVariableNames' true, 'ReadRowsNames' true);
x(:, :, k) = temp(:, :);
end
%Finding Average and Std
Avg = mean(x,3);
Standard_deviation = std(x,0,3)

 Accepted Answer

% Ask for the name of the file that the user wants to save.
startingFolder = pwd % or wherever you want.
defaultFileName = fullfile(startingFolder, '*.*');
[baseFileName, folder] = uiputfile(defaultFileName, 'Specify a file');
if baseFileName == 0
% User clicked the Cancel button.
return;
end
fullFileName = fullfile(folder, baseFileName)
% Open file for writing.
fid = fopen(fullFileName, 'wt');
if fid ~= -1
% Was able to open it successfully, now write stuff into it.
fprintf(fid, 'Average = %f.\nStandard Deviation = %f.\n', Avg, Standard_deviation);
fclose(fid);
else
errorMessage = sprintf('Error opening %s for output.', fullFileName);
uiwait(errordlg(errorMessage));
end

3 Comments

d = 'E:\\ \\ DummyFile\\';
filePattern = fullfile(d, 'Run*');
file = dir(filePattern)
n = length(file)
for k = 1:n
baseFileName = file(k).name;
FullFileName = fullfile(d, baseFileName, 'dummy.csv');
temp = readtable(FullFileName, 'ReadVariableNames' true, 'ReadRowsNames' true);
for k ==1:
x = Nan(size(temp,1), size(temp, 2), n);
rowNames = temp.Properties.RowNames;
colNames = temp.Properties.VariableNames;
end
x(:, :, k) = temp{:, :};
end
%Finding Average and Std
Avg = mean(x,3);
Standard_deviation = std(x,0,3)
index_val = 1:size(x,3) % gives 1-30 numbers bez its total 30 files
for i=1:size(x,1) %looping over 96 of rows
for j=1:size(x,2) %looping over 20 of cols
y(:) = x(i, j, :)
p = polyfit(index_val, y, 1)
slope(i,j) = p(1);
end
end
%Convert Calculation back to table format for easier display
AvgT = array2table(Avg, 'VariableNames', colNames, 'RowNames', rowNames);
StdT = array2table(standard_deviation, 'VariableNames', colNames, 'RowNames', rowNames);
SlopeT = array2table(slope, 'VariableNames', colNames, 'RowNames', rowNames);
% Ask for the name of the file that the user wants to save.
startingFolder = d
defaultFileName = fullfile(startingFolder, '*.*');
[baseFileName, folder] = uiputfile(defaultFileName, 'Specify a file');
if baseFileName == 0
% User clicked the Cancel button.
return;
end
fullFileName = fullfile(folder, baseFileName)
% Open file for writing.
fid = fopen(fullFileName, 'wt');
if fid ~= -1
% Was able to open it successfully, now write stuff into it.
writetable(AvgT, fullFileName, 'Delimiter', ',', 'WriteRowNames', true);
writetable(StdT, fullFileName, 'Delimiter', ',', 'WriteRowNames', true);
writetable(SlopeT, fullFileName, 'Delimiter', ',', 'WriteRowNames', true);
fclose(fid);
else
errorMessage = sprintf('Error opening %s for output.', fullFileName);
uiwait(errordlg(errorMessage));
end
% How should i generate three different files with given one file name. SO i want to create three different output.csv for AVGT, stdT, and SlopeT data.
How should i generate three different files with given one file name. SO i want to create three different output.csv for AVGT, stdT, and SlopeT data.
Simply change the filename before each call to writetable().
fullFileName = 'AvgT.csv';
writetable(AvgT, fullFileName, 'Delimiter', ',', 'WriteRowNames', true);
fullFileName = 'StdT.csv';
writetable(StdT, fullFileName, 'Delimiter', ',', 'WriteRowNames', true);
fullFileName = 'SlopeT.csv';
writetable(SlopeT, fullFileName, 'Delimiter', ',', 'WriteRowNames', true);

Sign in to comment.

More Answers (1)

madhan ravi
madhan ravi on 6 Jul 2020
Edited: madhan ravi on 6 Jul 2020
doc writematrix % after converting your data into a table
%or
doc dlmwrite

Categories

Find more on Statistics and Machine Learning Toolbox in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!