Filter löschen
Filter löschen

I want to save a mat. file each time the program runs with a different name.

5 Ansichten (letzte 30 Tage)
for example, when I run program for the first time i want to save it as ' text1.mat' second time 'text2.mat' etc...

Akzeptierte Antwort

Jan
Jan am 28 Nov. 2017
Bearbeitet: Jan am 28 Nov. 2017
Folder = tempdir; % Define accordingly
FileList = dir(fullfile(Folder, 'text*.mat'));
NewFile = fullfile(Folder, sprintf('text%d.mat', numel(FileList) + 1));
This fails, if you delete some of the files afterwards. Then this helps:
NameList = natsort({FileList.name});
LastIndex = sscanf(NameList{end}, 'text%d.mat'));
NewFile = fullfile(Folder, sprintf('text%d.mat', LastIndex + 1));
Or prefer:
nowStr = datestr(now, 'yyyy-mm-dd_HHMMSS');
NewFile = fullfile(Folder, ['text', nowStr, '.mat']);
But remember that this fails, if you create more than 1 file per second or during the change of the daylight-saving-time.
  2 Kommentare
arvind ramasamy
arvind ramasamy am 28 Nov. 2017
the answers are useful.
I wish to know, I am giving a constant current everytime to predict the battery discharge .
so I am running a script which calculates lots of values. i am taking only 4 values out of it to plot my graph for different amps and compare it.
I save each one manually from workspace and load it again.
But i want now instead of manually doing it. the script has to automatically generate a name like this ' Test_1amp.mat, the next time when i give 2 amps test_2amp.mat
could you please help out in this??
Walter Roberson
Walter Roberson am 28 Nov. 2017
NameList = natsort({FileList.name});
LastIndex = sscanf(NameList{end}, 'test_%damp.mat'));
NewFile = fullfile(Folder, sprintf('test_%damp.mat', LastIndex + 1));

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu MATLAB finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by