Info

La pregunta está cerrada. Vuélvala a abrir para editarla o responderla.

Including name of file to process when calling function

2 visualizaciones (últimos 30 días)
Luc
Luc el 1 de Dic. de 2016
Cerrada: MATLAB Answer Bot el 20 de Ag. de 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];

Respuestas (1)

Image Analyst
Image Analyst el 1 de Dic. de 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

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by