how can i save a state name by name input from user

1 次查看(过去 30 天)
look at my code, m trying to name a file by accepting an input from the user.
if true
function saveState(~)
global destX;
global destY;
global strtX;
global strtY;
global obx;
global oby;
state.startx=strtX;
state.starty=strtY;
state.destx=destX;
state.desty=destY;
state.obsx=obx;
state.obsy=oby;
state.dx=rand(1,length(obx));
state.dy=rand(1,length(obx));
ggwp=get(handles.name,'string
');
%save state.mat state
save([ggwp '.mat'], state)
end
  3 个评论
Borison ningthoujam
编辑:Borison ningthoujam 2018-6-12
from the edit text i want to get the name of the file i want to save.....and using the recieved name i want to save the state as the this name.....suppose i have entered abcd then i want my file name to be abcd.mat
Dennis
Dennis 2018-6-12
编辑:Dennis 2018-6-12
In that case you need to pass handles to your function aswell, because it does not know handles.name. Or you could use findobj to find it. If saveState is a callback function you need another ~. As far is i know Matlab will always pass objecthandle and event data.
function saveState(~,~)

请先登录,再进行评论。

回答(2 个)

Jan
Jan 2018-6-12
function saveState(hObject, EventData, handles)
global destX;
global destY;
global strtX;
global strtY;
global obx;
global oby;
state.startx=strtX;
state.starty=strtY;
state.destx=destX;
state.desty=destY;
state.obsx=obx;
state.obsy=oby;
state.dx=rand(1,length(obx));
state.dy=rand(1,length(obx));
ggwp=get(handles.name,'string');
save([ggwp '.mat'], 'state') % With quotes around: state !!!
end
The save command requires the 2nd input to be a string (char vector), which contains the name of the variables to be saved. You cannot provide the variables directly - a bad decision from the early history of Matlab.
It is a bad design to use global variables. They are prone to errors and hard to debug. It would be easier and cleaner to store the parameters in the handles struct.
With providing a file name for the save command only, the current folder is used. Remember that this can be changed by any GUI or timer callback. For reliable code define the output folder also, e.g.:
save(fullfile(handles.ExportFolder, [ggwp '.mat']), 'state')
and a proper definition of the field ExportFolder e.g. in the OpeningFcn of the GUI.

Image Analyst
Image Analyst 2018-6-12
You can ask the user for a filename like this:
% Get the name of the file that the user wants to save.
% Note, if you're saving an image you can use imsave() instead of uiputfile().
startingFolder = userpath % Or "pwd" or wherever you want.
defaultFileName = fullfile(startingFolder, '*.*');
[baseFileName, folder] = uiputfile(defaultFileName, 'Specify a file');
if baseFileName == 0
% User clicked the Cancel button.
return;
end
fullFileName = fullfile(folder, baseFileName)
If you want, you could make it more robust by getting the extension of the file, throw it away (if it's even there), and making the extension .mat. Once you have the filename, continue to call save(fullFileName, ......)

类别

Help CenterFile Exchange 中查找有关 Environment and Settings 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by