Save variable as string from user input
显示 更早的评论
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 个评论
Azzi Abdelmalek
2015-6-26
This is not clear, can you post an example?
Stephen23
2015-6-29
This question is based on this discussion:
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Structures 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!