How can I add a time stamp to a variable name?

Hi community,
I wanted to know how if I can add time stamps to a variable name. I want to create an standalone application and at the end a variable is saved in a .mat file. The file is named by the user, but I do not want to let the user pick the variable name. I would rather want to add a time stamp to the variable name. Is there a way to do that?
Everytime I try something the variable gets overwritten with the time stamp and not added to the variable name.
Is this even possible or do I have to think of a diiferent approach?
Thanks in advance!
formatDescr = 'ddmmmyyyy_HHMMSS';
datestring = datestr(now, formatDescr);
elseif strcmp(app.regDirection, 'CBCT2CT') == true
warpedCBCT = strcat('warpedCBCT', datestring);
atlasFile = uiputfile;
save(atlasFile, 'warpedCBCT');
end

3 Comments

"Is this even possible.."
It is certainly possible, but just because something is possible does not make it a good way to write code.
Forcing meta-data into variable names makes accessing your data slower and more complex.
"..or do I have to think of a diiferent approach?"
You don't have to, but you should.
Meta-data is data. Data should be stored in a variable, not in its name.
Okay I understand that, but I wanted to make it simpler for the user.
So naming the variable through an input from the user (just like naming the file) would be the best option?
I decided against this option because I did not want the user to have to make too many inputs.
"So naming the variable through an input from the user (just like naming the file) would be the best option?"
That would be about the worst option.
What happens when your user selects the variable name "123 + 567" or "! format C:\" ?

Sign in to comment.

Answers (1)

warpedCBCT = strcat('warpedCBCT', datestring);
savestruct = struct(warpedCBCT, DataToSave);
[atlasFile, filepath] = uiputfile();
if ~ischar(atlasFile)
return; %user cancel
end
filename = fullfile(filepath, atlasfile);
save(filename, '-struct', 'savestruct', '-append')
Each time you save to the same file, a new variable will be added to it.
The keyword -struct tells save not to save "savestruct" as a structure, but to instead save each field in the structure as an individual variable.

4 Comments

Thanks for the fast answer, but the variable will not be added to the same file.
I should have been clearer: The application is supposed to be run 2 times. Thus there will be two differently named files with one variable each. And currently these two files have the same variable name saved.
But the 2 files will be loaded in an other app and I get problems because they are overwriting themself because of the same variable name.
"And currently these two files have the same variable name saved."
I doubt that is really a problem. For example, when processing multiple files in a loop:
Do they overwrite the variables on every loop iteration? (hint: nope, they use indexing).
"But the 2 files will be loaded in an other app and I get problems because they are overwriting themself because of the same variable name."
So instead of asking how to solve that, you asked about something else:
Always LOAD into an output variable. Then you can avoid the problem that you describe in the 2nd app.
Thanks for the links. I didn't know about the xy problem.
Next time I will be clearer!
That's actually pretty obvious but I wasn't aware of that.
Using the -append flag is not required for save -struct to work.

Sign in to comment.

Asked:

on 4 May 2022

Commented:

on 4 May 2022

Community Treasure Hunt

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

Start Hunting!