Saving and uploading data with any file name
Ältere Kommentare anzeigen
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)
2 Kommentare
Stephen23
am 18 Okt. 2019
Note that this inefficient, complex, obfuscated, very inadvisable code:
evalstr = sprintf('tempval = data.%s.%s',uictagnames{i},uicparams{j});
evalc(evalstr);
should be replaced by simpler, neater, more efficient dynamic fieldnames:
tempval = data.(uictagnames{i}).(uicparams{j});
Note that tempval is overwritten on each loop iteration, and is unused anyway.
Akzeptierte Antwort
Weitere Antworten (0)
Kategorien
Mehr zu File Operations finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!