Using "fullfile" to add a predefined date to a filename
Show older comments
Hi,
I have one last question concerning the code I'm writing. I'm trying to use the "fullfile" and "save" functions so I can save each iteration with the filename "data-" and the extracted date appended to the filename so it looks like for example "data-20081224T095947Z".
I hope to end up 110 files called data- with the extracted date as their file name and contents of data inside that file for the loop iteration.
I've looked at some examples and came up with the following to save the files (see below % TODO: Save files), it looks correct but just seems freeze Matlab, saying it "busy". Does anyone know if this it correct or is there a better, more efficient way of doing this?
clear all
load Device2_13032021.mat;
L = length(measurement.transient);
for i = 1:L
dateName = {measurement.transient(i).date};
timeDom = {measurement.transient(i).timeDomain};
%Current file format{'12/24/2008 9:59:47 AM.786743640'}
%Fileformat to convert to = data-20130325T004512Z.mat
date=cell2mat(dateName);
data=cell2mat(timeDom);
N = date;
T = table(string(N), 'VariableNames', {'Dates'});
S = "12/24/2008 9:59:47 AM.786743640";
D = datetime(S, 'InputFormat','MM/dd/yyyy h:m:s a.SSSSSSSSS');
D.Format = 'yyyyMMdd''T''HHmmss''Z''';
Date=string(D);
% TODO: Save files
FileName=['data-','Date'];
save( fullfile('D:\NasaPrognostic\Device2', FileName) );
end
Accepted Answer
More Answers (1)
You have put 'Date' in quotes, meaning it is appending the text "Date" to your filename, not the date string you have created.
S = "12/24/2008 9:59:47 AM.786743640";
D = datetime(S, 'InputFormat','MM/dd/yyyy h:m:s a.SSSSSSSSS');
D.Format = 'yyyyMMdd''T''HHmmss''Z''';
Date=string(D);
FileName=['data-','Date'];
fullfile('D:\NasaPrognostic\Device2', FileName)
Remove the quotes to use the variable values and use string concatenation to get the full path you intended.
FileName="data-" + Date;
fullfile('D:\NasaPrognostic\Device2', FileName)
Make sure you are using the correct slash (forward or back) for your OS. It might be good to include a file extension as well, but I leave that to you.
3 Comments
Andy Wileman
on 17 Mar 2021
Cris LaPierre
on 17 Mar 2021
Can you provide a couple examples of what the 110 unique filenames might be?
You will need to create unique filenames for each file. Right now, you have hardcoded the date (your variable S), so every file has the same name, and therefore overwrites itself each time.
Andy Wileman
on 17 Mar 2021
Categories
Find more on Dates and Time in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
