フィルターのクリア

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 Center および File ExchangeInteractive Control and Callbacks についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by