MATLAB Answers

How to create multiple files with given one file name

조회 수: 18(최근 30일)
Utsav Dobhi
Utsav Dobhi 10 Jul 2020 14:05
편집: Bjorn Gustavsson 15 Jul 2020 18:22
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);
%I want to create a AvgT, StdT, and SlopeT files with given a one output file name, how should I do that? for example, when i give a file name output.csv then it will automattically create three output files as Output_AvgT.csv, Output_StdT.csv, and Output_SlopeT.csv
% 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

  댓글 수: 0

Sign in to comment.

채택된 답변

Bjorn Gustavsson
Bjorn Gustavsson 10 Jul 2020 14:13
I'd do something like this:
fn = '/some/where/output.csv';
[pth,nmn,ext] = fileparts(fn);
nnmn = fullfile(pth,[nmn,'_std',ext]) % just make sure that ext comes with a leading '.'
That should give you the idea.
For more elaborate namings you can obviously use sprintf to include numbering and whatnot.
HTH

  댓글 수: 2

Utsav Dobhi
Utsav Dobhi 15 Jul 2020 17:39
How should i generate three different file with given one file name from above code?
Bjorn Gustavsson
Bjorn Gustavsson 15 Jul 2020 18:22
Should be just as simple as modifying thefilename assignment for the three different filenames you want:
nnmn1 = fullfile(pth,[nmn,'_stdT',ext]);
nnmn2 = fullfile(pth,[nmn,'_avgT',ext]);
nnmn3 = fullfile(pth,[nmn,'_SlopeT',ext]);

Sign in to comment.

추가 답변(0개)

태그

제품


Translated by