Effacer les filtres
Effacer les filtres

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

1 vue (au cours des 30 derniers jours)
Ali Y.
Ali Y. le 8 Août 2015
Commenté : Walter Roberson le 10 Août 2015
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?

Réponse acceptée

Walter Roberson
Walter Roberson le 8 Août 2015
save('D Variables.mat', answer, '-append')
  6 commentaires
Ali Y.
Ali Y. le 10 Août 2015
Modifié(e) : Ali Y. le 10 Août 2015
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');
Walter Roberson
Walter Roberson le 10 Août 2015
You probably mean
MYDATA.(answer{1}) = devar;

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Startup and Shutdown dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by