How to allow a GUI user to defined a variable name
7 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Tiarnan Murphy
am 16 Apr. 2019
Kommentiert: Stephen23
am 17 Apr. 2019
I've written a GUI that loads data from a file, applies various treatments to said data based upon what uicontrols the user chooses to use, and then saves the treated data as a variable in a .mat file to a user specified folder.
However, I have found it desireable to allow the user to specify the variable name before saving to the folder.
My initial idea was to ask the user for their prefered name and then assign the value to that using eval.
[filename,foldername] = uigetfile;
[~,myData] = loadHSI(fullfile(folder,file)); % This function loads the data from an unusual format
% Apply treatments
% ................
newName = char(inputdlg('What would you like to call the variable?'));
eval(sprintf('%s = TreatedData;',newName));
[newFile,newPath] = uiputfile('*','Save As:');
save(fullfile(newPath,newFile),sprintf('%s',newName))
Aside from being bad practice to use eval like that (a habit i've found difficult to break), this is not possible, as it would require dynamically adding a variable to a static workspace. My question then is: Is there any work around for allowing a user to define the name of a variable before being saved by a gui?
0 Kommentare
Akzeptierte Antwort
Walter Roberson
am 16 Apr. 2019
temp.(variableName) = value
save(filename, '-struct', 'temp')
3 Kommentare
Stephen23
am 17 Apr. 2019
"Aside from being bad practice to use eval like that (a habit i've found difficult to break)..."
This is exactly why you should break with the bad habit of using eval: you can easily write simpler, neater, more efficient code which wastes less of your time writing it.
Do not get stuck thinking "I cannot think of any other way to solve this": asking on this forum is always a good approach, or do some reading on the topic:
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Creating, Deleting, and Querying Graphics Objects 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!