Using "fullfile" to add a predefined date to a filename

21 ビュー (過去 30 日間)
Andy Wileman
Andy Wileman 2021 年 3 月 17 日
コメント済み: Andy Wileman 2021 年 3 月 17 日
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
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''';
% TODO: Save files
save( fullfile('D:\NasaPrognostic\Device2', FileName) );


Jan 2021 年 3 月 17 日
編集済み: Jan 2021 年 3 月 17 日
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');
  1 件のコメント
Andy Wileman
Andy Wileman 2021 年 3 月 17 日
Thats genius Jan, problem sorted.
Thank you so much.
Also a big thanks to Cris anyone else who helped with this problem.
What a relief, I've been on the problem five days.


その他の回答 (1 件)

Cris LaPierre
Cris LaPierre 2021 年 3 月 17 日
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''';
fullfile('D:\NasaPrognostic\Device2', FileName)
ans = 'D:\NasaPrognostic\Device2/data-Date'
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)
ans = "D:\NasaPrognostic\Device2/data-20081224T095947Z"
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 件のコメント
Andy Wileman
Andy Wileman 2021 年 3 月 17 日
Hi Cris,
Thanks for getting back to me. To fill you in on the picture, the code is for extracting test data from a structure, see below. Within the structure are a 110 elements, each having a date in the wrong format e.g."12/24/2008 9:59:47 AM.786743640" and a corresponding structure, measurement.transient.timeDomain in which the data I want is saved.
What I want to do for each of the 110 elements, is extract from each element the contents of the measurement.transient.timeDomain structure, also the date and then change the date to a 'yyyyMMdd''T''HHmmss''Z''' format.
Finally, I would like to save the each of the 110 elements with a filename consisting of " IGBTdata-'yyyyMMdd''T''HHmmss''Z''' " containing the contents of measurement.transient.timeDomain (preferably as a double)
When the date for each element is extracted and changed it will make each file name unique as they are at different times.
Hope that makes sense,



Help Center および File ExchangeCalendar についてさらに検索




Community Treasure Hunt

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

Start Hunting!

Translated by