필터 지우기
필터 지우기

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

조회 수: 1 (최근 30일)
Borison ningthoujam
Borison ningthoujam 2018년 6월 12일
답변: Image Analyst 2018년 6월 12일
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일
편집: 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에서 Interactive Control and Callbacks에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by