Name must be a text scalar

Hi,
I wan to use the datetime() function to name a folder. Moreover, I want to change the format with the replace() function and later access this folder via dir(). However, I get an error that says "Name must be a text scalar". I suppose the reason lies in the replace function. Is there any other way to do this?
actual_time = datestr(datetime('now')); % holt die aktuelle Zeit
files = dir([actual_time '\*.txt']); % works
actual_time = fullfile(replace(actual_time, ':', '-'))
files = dir([actual_time '\*.txt']); % error

2 commentaires

Stephen23
Stephen23 le 7 Juin 2023
Modifié(e) : Stephen23 le 7 Juin 2023
They both work here (I also replaced deprecated DATESTR with the recommended CHAR).
Note that you should use FULLFILE instead of concatenating text together.
actual_time = char(datetime('now'))
actual_time = '07-Jun-2023 11:45:18'
S1 = dir([actual_time '\*.txt']) % Should use FULLFILE here
S1 = 0×1 empty struct array with fields: name folder date bytes isdir datenum
actual_time = fullfile(replace(actual_time, ':', '-'))
actual_time = '07-Jun-2023 11-45-18'
S2 = dir([actual_time '\*.txt']) % Should use FULLFILE here
S2 = 0×1 empty struct array with fields: name folder date bytes isdir datenum
"Is there any other way to do this?"
Explicitly specify the DATETIME format:
DT = datetime('now','Format','uuuu-MM-dd HH-mm-ss')
DT = datetime
2023-06-07 11-45-18
S = dir(fullfile(char(DT),'*.txt'))
S = 0×1 empty struct array with fields: name folder date bytes isdir datenum
Marco Pedata
Marco Pedata le 7 Juin 2023
Thank you. It seems to work now :)

Connectez-vous pour commenter.

Réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by