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

21 visualizaciones (últimos 30 días)
Andy Wileman
Andy Wileman el 17 de Mzo. de 2021
Comentada: Andy Wileman el 17 de Mzo. de 2021
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

Respuesta aceptada

Jan
Jan el 17 de Mzo. de 2021
Editada: Jan el 17 de Mzo. de 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
  1 comentario
Andy Wileman
Andy Wileman el 17 de Mzo. de 2021
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.

Iniciar sesión para comentar.

Más respuestas (1)

Cris LaPierre
Cris LaPierre el 17 de Mzo. de 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)
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 comentarios
Cris LaPierre
Cris LaPierre el 17 de Mzo. de 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
Andy Wileman el 17 de Mzo. de 2021
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,
Andy

Iniciar sesión para comentar.

Categorías

Más información sobre MATLAB en Help Center y File Exchange.

Productos


Versión

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by