Uploading a value with a Matrix into MATLAB GUI

4 views (last 30 days)
Hello Guys. I hope you are doing well. I have a question related to the uploading the value with a matrix. My code for the upload_ callback is working for all values except for the edit text box with a matrix and a value. How can I change the code below so that I can upload the value with the matrix as well? Any help will be greatly appreciated. I get the following errors when I use the pushbutton upload code shown below. These errors only come when I try to upload the value with the matrix edit box and not for the other edit text boxes.
Error using sprintf
Function is not defined for 'cell' inputs.
Error in GUI_ParametersFinal>pushbutton_Upload_Callback (line 149)
evalstr = sprintf('set(handles.%s,''%s'',''%s'')',uictagnames{i}, uicparams{j},tempval);
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in GUI_ParametersFinal (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in @(hObject,eventdata)GUI_ParametersFinal('pushbutton_Upload_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback
The code is:
function pushbutton_Upload_Callback(hObject, eventdata, handles)
startingFolder = pwd
defaultFileName = fullfile(startingFolder, '*.mat');
[baseFileName, folder] = uigetfile(defaultFileName, 'Select a mat file');
if baseFileName == 0
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)
tempval = data.(uictagnames{i}).(uicparams{j});
if ~isnumeric(tempval)
evalstr = sprintf('set(handles.%s,''%s'',''%s'')',uictagnames{i}, uicparams{j},tempval);
else
evalstr = sprintf('set(handles.%s,''%s'',%d)',uictagnames{i}, uicparams{j}, tempval);
end
evalc(evalstr);
end
end
For edit callbacks, I am using this code:
str=get(hObject,'String');
tagname=get(hObject,'tag');
handles.mystructdata.(tagname).string = str;
guidata(hObject,handles)
The value in the edit text box is being generated from another GUI in the button down function. The value is getting saved, but once I click on the upload button, all the values get uploaded except for the value shown below because it is a value with matrix.
For all other structs, I have 1 fields. Only for this struct, I have 2 fields. Any help will be greatly admired as I am working on a big project. I had taken the help for uploading the values of the edit text boxes from this link. Therefore, you can see the link as well. I just need to upload the value with the matrix as shown above. Everthing else is done.
  1 Comment
Stephen23
Stephen23 on 21 Oct 2019
Edited: Stephen23 on 21 Oct 2019
The link you gave uses really very badly writen code: in general, I would not recommend copying code from people asking questions: the reason they are asking questions is because they don't know what they are doing. And anyone using evalc to set the field of a handles structure really doesn't know what they are doing.

Sign in to comment.

Accepted Answer

Stephen23
Stephen23 on 21 Oct 2019
Edited: Stephen23 on 21 Oct 2019
Clearly tempval is a cell array, which as the error message makes quite clear, sprintf cannot handle. But regardless, that is a very inadvisable approach to setting an object property.
You would easily avoid this error if you had followed my earlier advice to NOT use that awful evalc code. I have given you the link to the documentation several times, and also given you examples of how to use dynamic fieldnames:
set(handles.(uictagnames{i}), uicparams{j}, tempval)
Of course this assumes that the object accepts a cell array for that property: if that is not the case, then you will need to fix your code so that you do not try to set the property with a cell array.
  16 Comments
Stephen23
Stephen23 on 5 Nov 2019
Edited: Stephen23 on 5 Nov 2019
@MHS: look at what your code is doing. For example, when I search for the text MaturityLevelSaved in your two M-files, nowhere is this property actually defined for the GUI_MaturityLevel GUI itself, so what do you expect this to return?:
h=GUI_MaturityLevel(..);
..
.. = getappdata(h, 'MaturityLevelSaved'); % Retrieve variables from figure's workspace.
Without saving the property MaturityLevelSaved in the GUI, what arrays or values do you expect to get when you getappdata that GUI property?
Although your comment states "Retrieve variables from figure's workspace", I can't find anywhere in your code where you store that property in the figure. Note that I am not the GUI designer, I cannot decide which callback should be storing that property value, or under which circumstances.
You need to know that, and check that.
Probably you should do these two steps:
  1. review all of the properties that you require, check that they are actually defined in the appropriate locations (so far I have found that the two main properties were never even created, something you could easily have found out yourself).
  2. use breakpoints to stop the code on particular lines and/or on error. Check the values. Add new breakpoints accordingly, work backwards until you know where those values come from and why.
Learning to debug is an importnat part of learning how to write code. You need to learn how to do this yourself.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!