Using "fullfile" to add a predefined date to a filename
18 views (last 30 days)
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
0 Comments
Accepted Answer
Jan
on 17 Mar 2021
Edited: Jan
on 17 Mar 2021
You code will be faster if you omit the useless "clear all", which removed all loaded functions from the memory. Afterwards all functions must be loaded again from the slow disk.
I do not think that your code freezes. Maybe it takes more time than you expect. Insert some output to show the progress.
Calling load without output arguments stores trhe variable in the worksapce. Then you an Matlab cannot know, which variables are existing. This slows down the processing and impedes the debugging. Catch the output instead:
Data = load('Device2_13032021.mat');
measurement = Data.measurement;
L = length(measurement.transient);
for i = 1:L
fprintf('File %d of %d\n', i, L); % Show progress
...
This is a pile of conversions:
dateName = {measurement.transient(i).date}; % Array to cell
date=cell2mat(dateName); % Cell to array
N = date; % New name
T = table(string(N), 'VariableNames', {'Dates'}); % Array to table
S = "12/24/2008 9:59:47 AM.786743640"; % Now you do not use the original data anymore?
D = datetime(S, 'InputFormat','MM/dd/yyyy h:m:s a.SSSSSSSSS'); % String to datetime object
D.Format = 'yyyyMMdd''T''HHmmss''Z'''; % Format change
Date=string(D); % datetime to string
There must by a more direct way.
timeDom = measurement.transient(i).timeDomain; % Without { }
dateName = measurement.transient(i).date; % 12/24/2008 9:59:47 AM.786743640
% Wanted 20130325T004512Z
D = datetime(dateName, 'InputFormat', 'MM/dd/yyyy h:m:s a.SSSSSSSSS', ...
'Format', 'yyyyMMdd''T''HHmmss''Z''')
FileName = fullfile('D:\NasaPrognostic\Device2', "data-" + string(D));
save(FileName, 'timeDom');
end
More Answers (1)
Cris LaPierre
on 17 Mar 2021
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
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.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!