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

1 visualización (últimos 30 días)
Borison ningthoujam
Borison ningthoujam el 12 de Jun. de 2018
Respondida: Image Analyst el 12 de Jun. de 2018
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 comentarios
Borison ningthoujam
Borison ningthoujam el 12 de Jun. de 2018
Editada: Borison ningthoujam el 12 de Jun. de 2018
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 el 12 de Jun. de 2018
Editada: Dennis el 12 de Jun. de 2018
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(~,~)

Iniciar sesión para comentar.

Respuestas (2)

Jan
Jan el 12 de Jun. de 2018
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 el 12 de Jun. de 2018
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, ......)

Categorías

Más información sobre Interactive Control and Callbacks en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by