Saving and uploading data with any file name

13 vues (au cours des 30 derniers jours)
MHS
MHS le 18 Oct 2019
Modifié(e) : MHS le 18 Oct 2019
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 commentaires
Stephen23
Stephen23 le 18 Oct 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.
MHS
MHS le 18 Oct 2019
Modifié(e) : MHS le 18 Oct 2019
Thank you. I have done it again now :)
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)
tempval = data.(uictagnames{i}).(uicparams{j});
end
end

Connectez-vous pour commenter.

Réponse acceptée

Stephen23
Stephen23 le 18 Oct 2019
Modifié(e) : Stephen23 le 18 Oct 2019
"...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.
  2 commentaires
MHS
MHS le 18 Oct 2019
When I use the save filename function, it gives me an error that the filename is undefinedl
Stephen23
Stephen23 le 18 Oct 2019
Modifié(e) : Stephen23 le 18 Oct 2019
save(fullFileName,'data')
Or if you want a bit more context:
...
fullFileName = fullfile(folder, baseFileName);
data = handles.mystructdata;
data.MaturityLevel = handles.MaturityLevel;
save(fullFileName,'data')

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by