Dynamic (non-string) variable name in Matlab 'save' function, using inputdlg

Hi everybody,
Is it possible to define the name of 'save' function's variable dynamically by using inpudlg. I want to save and append my derived variables to a workspace, table, or text file. So, at the end I have my derived variables.
prompt = {'Enter a name for the derived variable:'};
dlg_title = 'Input';
num_lines = 1;
defAns = {''};
answer = inputdlg(prompt,dlg_title,num_lines,defAns);
answer = devar; % answer and devar are going to change
save('D Variables.mat', 'answer', '-append')
In the last line of the above code, I want the 'answer', that is a string, to be changed as the result of inputdlg result, i.e. answer that is dynamic.
Though, combination of 'array2table' and 'writetable' functions let me to have my variable (with its header), I am not able to append the new variable to the previous storied variables.
How can I fix this code?

 Accepted Answer

save('D Variables.mat', answer, '-append')

6 Comments

Thank you for your reply, Whalter. That is not working. Because the variable/argument name should be a string; so when you put something without quotes around it, it gives the error 'Argument must contain a string'. I even tried, say:
MYDATA.answer= devar
save('DVariables.mat', '-struct', 'MYDATA', '-append')
But the same problem arises. How to trick this function, so put variable name not as string but act as string, within the function?
save('D Variables.mat', '-struct', answer{1}, '-append')
Well, that gives an error that I do not even know what it refers to.
Error: 'The argument to -STRUCT must be the name of a scalar structure variable'.
I am going to give a self-dependent code, that shows the error.
devar = ones(10,1); %devar 1
% devar = zeros(10,1); % devar 2
prompt = {'Enter a name for the derived variable:'};
dlg_title = 'Input';
num_lines = 1;
defAns = {''};
asnwer = inputdlg(prompt,dlg_title,num_lines,defAns);
% answer = devar % #1 alternative
MYDATA.asnwer = devar;% #2 alternative
save('D Variables.mat', '-struct', asnwer{1}, '-append')
When you use the -struct option, the variable you name to save must be a struct, and the result of the save is to save each field of the struct as if it were an individual variable. If you want to save() a variable that is not a struct or if you do not want the struct broken into individual variables, then do not use the -struct option.
Also note that when you use the -append option in that form, then what it means is that all existing variables in the .mat that are not the named variable are to be retained, and that if the variable named already exists in the .mat then the old value is to be replaced, and that if the variable named did not already exist in the .mat then it is to be added to the .mat. A lot of people have the mistaken idea that if the variable already exists in the .mat then the named variable will be added on to the end of what is already there. For example if ABC is already there with value [1 2 3] and you ask to -append an ABC with value [4 5 6] then the result is that just the [4 5 6] is saved and not that [1 2 3 4 5 6] or [1 2 3; 4 5 6] are saved. If you do not use the -append option then everything that is already in the .mat is discarded.
Thank you Walter, for your help. For those having my way of cognition, and expression, I would suggest having a look at the concept of 'dynamic structure name' and 'dynamic field name', in addition to the tips of Walters.
My code got functioning as
devar = ones(10,1);
prompt = {'Enter a name for the derived variable:'};
dlg_title = 'Input';
num_lines = 1;
defAns = {''};
answer = inputdlg(prompt,dlg_title,num_lines,defAns);
MYDATA(answer{1}) = devar;
save('DVariabels.mat', '-struct', 'MYDATA', '-append');

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!