uicontrol handle error using set

11 vues (au cours des 30 derniers jours)
WahooSalty
WahooSalty le 2 Août 2016
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
% SIMPLE_GUI2 Select a data set from the pop-up menu, then
% click one of the plot-type push buttons. Clicking the button
% plots the selected data in the axes.
% http://www.mathworks.com/help/matlab/creating_guis/about-the-simple-programmatic-gui-example.html
close all
fh=findobj('tag','Simple GUI'); if(~isempty(fh))
figure(fh);
return
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)
% Display surf plot of the currently selected data.
surf(current_data);
end
function meshbutton_Callback(source,eventdata)
% Display mesh plot of the currently selected data.
mesh(current_data);
end
function contourbutton_Callback(source,eventdata)
% Display contour plot of the currently selected data.
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 % User selects Peaks.
plotData = data(:,1);
ytext = 'X';
case 2 % User selects Membrane.
plotData = data(:,2);
ytext = 'Y';
case 3 % User selects Sinc.
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)
%plot(ones(size(plotData))*plotDataAve,'r-')
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
%uiputfile;
end
function data = readLockin()
noise = 45e-3;
X = 120 + noise*randn(1);
Y = -50 + noise*randn(1);
data = [X, Y];
% try
% if(strcmp(computer,'MACI'))
% handles.LockInObj=visa('ni','GPIB0::6::INSTR');
% else
% handles.LockInObj = gpib('ni',0,6);
% end
% fopen(handles.LockInObj);
% fprintf(handles.LockInObj,'*IDN?');
% idn = fscanf(handles.LockInObj);
% catch
% disp('Could not communicate with lock in amp. Echoing commands to prompt.');
% handles.LockInObj=1;
%
% end
%fprintf(handles.LockInObj,'SNAP?1,2')
%pt=(eval(['[' fscanf(handles.LockInObj,'%s') ']']));
end
end
end

Réponses (1)

Benjamin Kraus
Benjamin Kraus le 2 Août 2016
There was a major change in how graphics are treated in MATLAB in R2014b. For details you can see this page: Graphics Changes in R2014b
This particular issue (handles vs. doubles) is described here: Graphics Handles Are Now Objects, Not Doubles
With a little care you should be able to write code that will work in both MATLAB R2013a/b and MATLAB R2016a.
One big difference is that you cannot use "dot notation" to access properties in MATLAB prior to R2014b.
For example:
handles.fig.Units = 'normalized';
Would need to be changed to:
set(handles.fig,'Units','normalized');
Using 'set' is the old mechanism of accessing properties, but it continues to work in MATLAB R2014b and newer, and is the only mechanism that worked before R2014b. You are using the "dot notation" several times in your code, those will all need to be changed to use "set" if you want your code to work in older releases.
I suspect this is the biggest issue you would need to fix before your code will work in R2013a/b. Once you have made those changes, test your code again and see if it works. If not, try reformatting your code above so it is a bit easier to read. I tried copying/pasting into MATLAB but the text wrapping was broken, so I had trouble figuring out what was happening.

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by