Filter löschen
Filter löschen

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

1 Ansicht (letzte 30 Tage)
Borison ningthoujam
Borison ningthoujam am 12 Jun. 2018
Beantwortet: Image Analyst am 12 Jun. 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 Kommentare
Borison ningthoujam
Borison ningthoujam am 12 Jun. 2018
Bearbeitet: Borison ningthoujam am 12 Jun. 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 am 12 Jun. 2018
Bearbeitet: Dennis am 12 Jun. 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(~,~)

Melden Sie sich an, um zu kommentieren.

Antworten (2)

Jan
Jan am 12 Jun. 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 am 12 Jun. 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, ......)

Kategorien

Mehr zu Interactive Control and Callbacks finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by