How to save a mat.file from GUI to a Folder chosen by the user !

8 views (last 30 days)
Hello,
i am interested in Programming with GUI. One Problem i am currently facing is how to save data from a GUI to a chosen Folder. It doesnt need to be necessarily saved in the workspace first.
Assume i have to save this Data:
handles.myMatrix=[1,2,3;4,5,6]
And i found this script to use:
myMatrix = handles.myMatrix;
myMatFullFileName = fullfile('C:','Users','busta','Desktop', 'myBaseMatFileName.mat');
save(myMatFullFileName, 'myMatrix');
This works fine but the user does not have the possibility of chooosing the directory.
For loading data i got this, that works fine! this is what i would like to have for saving too. I cannot adapt it:(
function load__Callback(hObject, eventdata, handles)
% hObject handle to load_ (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
startingFolder = 'C:\Program Files\MATLAB'
if ~exist(startingFolder, 'dir')
% If that folder doesn't exist, just start in the current folder.
startingFolder = pwd;
end
% Get the name of the mat file that the user wants to use.
defaultFileName = fullfile(startingFolder, '*.mat')
[baseFileName, folder] = uigetfile(defaultFileName, 'Select a mat file')
if baseFileName == 0
% User clicked the Cancel button.
return;
end
fullFileName = fullfile(folder, baseFileName)
storedStructure = load(fullFileName)
I would be very glad for your help!
best regards, busta x
  1 Comment
John
John on 11 Sep 2015
Works like that:
startingFolder = 'C:\Program Files\MATLAB'
if ~exist(startingFolder, 'dir')
% If that folder doesn't exist, just start in the current folder.
startingFolder = pwd;
end
% Put in the name of the mat file that the user wants to save.
% defaultFileName = fullfile(startingFolder, '*.mat')
[file,path] = uiputfile('myMatrix.mat','Save file name');
if file == 0
% User clicked the Cancel button.
return;
end
fullFileName = fullfile(path, file)
save(fullFileName)

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!