Save variable as string from user input
Mostrar comentarios más antiguos
I have a gui which let the user create a test. I store the user input as a new structure because the user should be able to save the test parameters. When the user pushes the save button i want to save the new structure as a .mat file containing this structure. The problem is naming the saved structure. I'm using "save(filename, varname)" which let me name the .mat file whatever I want to. But the structure name which is saved will be named after the variable 'varname'. But i want the structure to be named after what the user named the test. I hope I'm making sence. From what i have understood it's bad practise to create a variable with the name of a string. Is there another way of achieving this?
Edit* Example:
%returns the structure with test %parameters
editedTest = getNewTest(handles,editedTestName);
%Save structure
save(fileName,'editedTest');
This saves my structure in a .mat file which I want, but the structure in the .mat file is named 'editedTest', I want to name this variable after a string (I want the saved structure to be named as what the user named the test to).
The whole function looks like this:
% --- Executes on button press in pushbutton_saveTest.
function pushbutton_saveTest_Callback(hObject, ~, handles)
listboxItems = get(handles.listbox_tests,'String');
editedTestName = char(get(handles.edit_testName,'String'));
editedTest = getNewTest(handles,editedTestName);
currentFolderPath = pwd;
if ismember({'-Empty-'},listboxItems)
listboxItems{1,:} = editedTestName;
elseif ismember({editedTestName}, listboxItems)
% Construct a questdlg with three options
choice = questdlg('Test already exists, do you want to overwrite?', ...
'Test already exists', ...
'default');
% Handle response
switch choice
case 'Yes'
fileName = [currentFolderPath '\Tests\' editedTestName];
case 'No'
answer = char(inputdlg({'Enter new test name'},'Save new test',1,{'untitled test'}));
fileName = [currentFolderPath '\Tests\' answer];
listboxItems(end+1,:) = {answer};
case 'Cancel'
%Do nothing
fileName=[];
end
else
fileName = [currentFolderPath '\Tests\' editedTestName];
listboxItems(end+1,:) = {editedTestName};
end
if ~isempty(fileName)
save(fileName,'editedTest');
handles.tests = setfield(handles.tests, editedTestName, editedTest);
end
set(handles.listbox_tests,'String',listboxItems);
guidata(hObject, handles);
2 comentarios
Azzi Abdelmalek
el 26 de Jun. de 2015
This is not clear, can you post an example?
Stephen23
el 29 de Jun. de 2015
This question is based on this discussion:
Respuesta aceptada
Más respuestas (0)
Categorías
Más información sobre Structures en Centro de ayuda y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!