Effacer les filtres
Effacer les filtres

Info

Cette question est clôturée. Rouvrir pour modifier ou répondre.

Including name of file to process when calling function

1 vue (au cours des 30 derniers jours)
Luc
Luc le 1 Déc 2016
Clôturé : MATLAB Answer Bot le 20 Août 2021
Hi, I have a function that processes .dat data files that have a date in the file name(see below). I would like to have the possibility of entering the date when calling the function in the command window ex: Function(2016_11_28_0000) instead of having to modify the script every times I want to process data for a new date. I have the feeling it's not a complicated thing to do but could not find what I am looking for in the ask/answer community.
Thanks
fid = fopen('TOA5_TGA.RawData_55_2016_11_28_0000.dat', 'r');
T = textscan(fid, '%s%f%f%f%f%f%f%f%f%f%f%f%f%f','Delimiter',',','HeaderLines',4);
fclose(fid);
D1=regexprep(T{1}, '"', '');
D2=regexprep(D1, '(:\d\d)$', '$1.0', 'lineanchors');
formatIn = 'yyyy-mm-dd HH:MM:SS.FFF';
Date=datenum(D2,formatIn);
T{1} = [];
Tmat = cell2mat(T);
Data = [Date Tmat];

Réponses (1)

Image Analyst
Image Analyst le 1 Déc 2016
Inside the function, use sprintf() to build the filename:
function Data = MyFunction(dateSuffix)
folder = pwd; % Wherever....
baseFileName = sprintf('TOA5_TGA.RawData_55_%s.dat', dateSuffix);
fullFileName = fullfile(folder, baseFileName);
if exist(fullFileName, 'file')
fid = fopen(fullFileName, 'r');
% etc. More code to do whatever you want to do.
else
% Warn user file does not exist
warningMessage = sprintf('Warning: file does not exist:\n%s', fullFileName);
uiwait(errordlg(warningMessage));
end

Cette question est clôturée.

Tags

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by