I am new to creating GUIs in Matlab, but was able to create a simple GUI in Matlab 2016. However, when I attempted to run the code on another computer using Matlab 2013 I get the error,
Error using setappdata Value must be a handle
Error in SimpleGuiTest (line 77) setappdata(handles.fig,'handles',handles)
After using the debugger it seem like 'handles.fig' is a handle when run using version 2016, but only double in the 2013 version. A similar situation is observed for all uicontrol handles as well. Furthermore, another GUI code that a previous colleague wrote works fine with handles as doubles. Why is this happening? Code follows.
if true
function SimpleGuiTest
close all
fh=findobj('tag','Simple GUI'); if(~isempty(fh))
end
% Create and then hide the UI as it is being constructed. handles.fig = figure('Visible','off','Units','Normalized','Position',[0.1,0.5,0.5,0.5],'Name','SimpleGuiTest');
% Construct the components.
htext = uicontrol('Style','text','String','Select Data',... 'Units','Normalized',... 'Position',[0.7,0.1,0.2,0.15]); %xsize, xposition, ysize, yposition handles.hpopup = uicontrol('Style','popupmenu',... 'String',{'X','Y','R','Phase','Ratio'},... 'Units','Normalized',... 'Position',[0.7,0.1,0.2,0.15],... 'Callback',@popup_menu_Callback); hcollect = uicontrol('Style','pushbutton',... 'String','Collect Data',... 'FontSize',14,... 'Units','Normalized',... 'Position',[0.8,0.4,0.2,0.08],... 'Callback',@collect_Callback); hlive = uicontrol('Style','togglebutton',... 'String','Read Live',... 'FontSize',14,... 'Units','Normalized',... 'Position',[0.7,0.3,0.2,0.08],... 'Callback',@live_Callback); handles.hMean = uicontrol('Style','text','String','Mean',... 'FontSize',14,... 'Units','Normalized',... 'Position',[0.75,0.7,0.2,0.15]); handles.hStd = uicontrol('Style','text','String','Stdev',... 'FontSize',14,... 'Units','Normalized',... 'Position',[0.75,0.65,0.2,0.15]); ha = axes('Units','pixels','Units','Normalized','Position',[0.1,0.15,0.6,0.8]); align([htext,handles.hpopup, hcollect, hlive],'Center','None');
% Initialize the UI. % Change units to normalized so components resize automatically. handles.fig.Units = 'normalized'; ha.Units = 'normalized'; htext.Units = 'normalized'; handles.hpopup.Units = 'normalized';
tau = 1:10;
% % Create a plot in the axes. % current_data = peaks_data; % surf(current_data);
% Assign the a name to appear in the window title. handles.fig.Name = 'Simple GUI';
% Move the window to the center of the screen. movegui(handles.fig,'center')
% Make the window visible. handles.fig.Visible = 'on'; setappdata(handles.fig,'handles',handles)
% Pop-up menu callback. Read the pop-up menu Value property to % determine which item is currently displayed and make it the % current data. This callback automatically has access to % current_data because this function is nested at a lower level. function popup_menu_Callback(source,eventdata) % Determine the selected data set. str = get(source, 'String'); val = get(source,'Value'); end
% Push button callbacks. Each callback plots current_data in the % specified plot type.
function surfbutton_Callback(source,eventdata)
surf(current_data);
end
function meshbutton_Callback(source,eventdata)
mesh(current_data);
end
function contourbutton_Callback(source,eventdata)
contour(current_data);
end
function collect_Callback(source,eventdata)
for i = 1:length(tau)
x = tau(1:i);
y = tau(1:i).^2;
plot(x,y,'d')
xlabel('test')
ylabel('test')
pause(0.1);
end
end
function live_Callback(source,eventdata)
fh=get(source,'parent');
handles=getappdata(fh,'handles');
data = [];
numPoints = 1;
while get(source,'value')
data = [data; readLockin()];
switch handles.hpopup.Value;
case 1
plotData = data(:,1);
ytext = 'X';
case 2
plotData = data(:,2);
ytext = 'Y';
case 3
plotData = sqrt(data(:,1).^2 + data(:,2).^2);
ytext = 'R';
case 4
plotData = atand(data(:,2)./data(:,1));
ytext = 'Phase';
case 5
plotData = -data(:,2)./data(:,1);
ytext = 'Ratio';
end
maxPoints = 100;
if length(plotData) > maxPoints
plotData = plotData(end-maxPoints+1:end);
numPoints = numPoints(end-maxPoints+1:end);
end
plotDataAve = mean(plotData);
plotDataStd = std(plotData);
hplot = plot(numPoints,plotData,'d',...
numPoints,ones(size(plotData))*plotDataAve,'r-',...
numPoints,ones(size(plotData))*(plotDataAve + plotDataStd),'r--',...
numPoints,ones(size(plotData))*(plotDataAve - plotDataStd),'r--');
set(hplot(2),'LineWidth',1.5)
set(hplot(3),'LineWidth',1.5)
set(hplot(4),'LineWidth',1.5)
xlabel('Number')
ylabel(ytext)
xmin = min(numPoints);
xmax = max(numPoints);
ymin = min(plotData);
ymax = max(plotData);
if xmin == xmax
xmin = 0;
end
if ymin == ymax
if ymin > 0
ymin = 0;
else
ymax = 0
end
end
axis([xmin xmax ymin ymax])
textMean = sprintf('Mean = %f', plotDataAve);
set(handles.hMean, 'String', textMean);
textStd = sprintf('Stdev = %f', plotDataStd);
set(handles.hStd, 'String', textStd);
pause(0.1);
numPoints = [numPoints, numPoints(end) + 1];
end
end
function data = readLockin()
noise = 45e-3;
X = 120 + noise*randn(1);
Y = -50 + noise*randn(1);
data = [X, Y];
end
end
end