MATLAB: Saving and uploading data with any file name

saving with any name

Hello Guys. I have a question about saving and uploading data with any file name and in any folder. My code is giving me errors and not saving the filename with any name that I want. You can have a look at my edit_callback, upload_callback and pushbutton_callback. Help will be greatly appreciated on how I can fix this.
% --- Executes on button press in pushbutton_Save.
function pushbutton_Save_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton_Save (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB

% handles structure with handles and user data (see GUIDATA)
% get the string data from edit
startingFolder = pwd % Or "pwd" or wherever you want.
defaultFileName = fullfile(startingFolder, '*.mat*');
[baseFileName, folder] = uiputfile(defaultFileName, '*.mat*');
if baseFileName == 0
% User clicked the Cancel button.

return;
end
fullFileName = fullfile(folder, baseFileName);
fid = fopen(fullFileName, 'wt');
if fid ~= -1
data = handles.mystructdata;
data.Level=handles.Level;
save('data')
fclose('all')
end
function pushbutton_Upload_Callback(hObject, eventdata, handles)
startingFolder = pwd
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)
temp = load(fullFileName);
data=temp.data;
uictagnames = fieldnames(data);
for i =1:numel(uictagnames)
uicparams = fieldnames(data.(uictagnames{i}));
for j = 1:numel(uicparams)
evalstr = sprintf('tempval = data.%s.%s',uictagnames{i},uicparams{j});
evalc(evalstr);
end
end
function edit_TemperatureValue_Callback(hObject, eventdata, handles)
% hObject handle to edit_RotorTemperatureValue (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edit_RotorTemperatureValue as text
% str2double(get(hObject,'String')) returns contents of edit_RotorTemperatureValue as a double
if regexp(hObject.String,'^[+-]?[0-9]*(\.(\d)+)?([eE]([+-])?(\d)+)?$|^[0-9]\*10\^[0-9]$')==1
else
set(hObject,'Enable','inactive') % Edit auf inactive setzen
set(hObject, 'String','Enter a number')
uicontrol(hObject)
end
str=get(hObject,'String');
tagname=get(hObject,'tag');
% USE TAGNAME AND VALUE OF THE PROPERTY AS FIELDNAMES FOR THE STRUCTURE
handles.mystructdata.(tagname).string = str;
guidata(hObject,handles)

Best Answer

"...on how I can fix this"
That is easy: fix your save syntax.
You seem to have confused high-level file importing/exporting functions (e.g. load) and low-level file operations which require to fopen and fclose the file. Low-level file function names start with f, e.g. fprintf, fscanf, fseek, etc. High-level functions, such as save (and load and readtable etc.) do NOT require that you explicitly obtain a handle to an open file, because all of the file handling is done automagically inside the function. That is rather the point of them.
In any case, neither of the terms fopen or "file ID" or similar are mentioned anywhere in the save documentation. In fact, the save documentation states exactly what inputs it requires...
"...and not saving the filename with any name that I want"
Well, in fact MATLAB is saving the file with exactly the filename that you specified: in your code you use a hardcoded filename 'data', so that is what you told MATLAB to use. Every single syntax shown in the save documentation has the filename as its first input argument: if you don't want the filename to be 'data', then you need to specify it to be something else.
All of that fopen and fclose stuff serves no purpose, so get rid of it.
Guessing and making things up is nowhere near as effective as actually reading the documentation.