Matlab r2015a指南 - 无法将双精度转换为句柄

时间:2016-04-03 14:54:38

标签: matlab-guide

在过去,我使用Matlab 2013,这段代码运行正常,没有错误,但现在我使用matlab r2015a,当我运行代码时,我收到以下错误:Cannot convert double value -10 to a handle.我检查了Matworks网站但是解释不是很清楚。你对如何解决这个问题有什么建议吗?非常感谢 !

function minhhoaxy(action,in1)

global DATA; 

if nargin < 1,
   action='start';
end;

if strcmp(action,'start'),
clf reset;
figure(gcf);
set(gcf,'Units','normalized','NumberTitle','off', ...
        'Name','Minh hoa GUI','backingstore','on');
min_x=-10;     
max_x=10;
x=0; 

uicontrol('Style','text','Units','normalized',...
         'Position',[.03 .03 .1 .05],...
         'BackgroundColor',[0 0.5 .5],...
         'FontSize',12,'Fontname','VNI-Times',...
         'ForegroundColor','white','String','Vò trí x:',...
         'HorizontalAlignment','center');
uicontrol('Style','text','Units','normalized',...
         'Position',[.15 .03 .05 .05],...
         'BackgroundColor',[0 0 .5],...
         'FontSize',12,'Fontname','VNI-Times',...
         'HorizontalAlignment','center',...
         'ForegroundColor','white','String',num2str(min_x));   
x1=uicontrol('Style','slider','Units','normalized',...
         'Position',[.2 .03 .25 .05],...
         'SliderStep',[5.0000e-004 5.0000e-003],...
         'Value',x,'Max',max_x,'Min',min_x,...
         'Callback','minhhoaxy(''setx'',1);minhhoaxy(''redraw'');');
uicontrol('Style','text','Units','normalized',...
         'Position',[.45 .03 .05 .05],...
         'BackgroundColor',[0 0 .5],...
         'FontSize',12,'Fontname','VNI-Times',...
         'HorizontalAlignment','center',...
         'ForegroundColor','white','String',num2str(max_x));   
x2=uicontrol('Style','edit','Units','normalized',...
         'Position',[.55 .03 .09 .05],...
         'FontSize',10','Fontname','VNI-Times',...
         'String',num2str(x),...
         'Callback','minhhoaxy(''setx'',2);minhhoaxy(''redraw'')');
uicontrol('Style','text','Units','normalized',...
         'Position',[.64 .03 .05 .05],...
         'BackgroundColor',[0 0 .5],...
         'FontSize',12,'Fontname','VNI-Times',...
         'ForegroundColor','white','String','cm',...
         'HorizontalAlignment','center');
uicontrol('Style','Pushbutton','Units','normalized',...
         'Position',[.87 .03 .1 .05],...
         'FontSize',12,'Fontname','VNI-Times',...
         'Callback','minhhoaxy(''done'')','String','Thoaùt');
     
min_y=-10;
max_y=10;
y=0;
uicontrol('Style','text','Units','normalized',...
         'Position',[.03 .1 .1 .05],...
         'BackgroundColor',[0 0.5 .5],...
         'FontSize',12,'Fontname','VNI-Times',...
         'ForegroundColor','white','String','Vò trí y:',...
         'HorizontalAlignment','center');
uicontrol('Style','text','Units','normalized',...
         'Position',[.15 .1 .05 .05],...
         'BackgroundColor',[0 0 .5],...
         'FontSize',12,'Fontname','VNI-Times',...
         'HorizontalAlignment','center',...
         'ForegroundColor','white','String',num2str(min_y));
uicontrol('Style','text','Units','normalized',...
         'Position',[.45 .1 .05 .05],...
         'BackgroundColor',[0 0 .5],...
         'FontSize',12,'Fontname','VNI-Times',...
         'HorizontalAlignment','center',...
         'ForegroundColor','white','String',num2str(max_y));
uicontrol('Style','text','Units','normalized',...
         'Position',[.64 .1 .05 .05],...
         'BackgroundColor',[0 0 .5],...
         'FontSize',12,'Fontname','VNI-Times',...
         'ForegroundColor','white','String','cm',...
         'HorizontalAlignment','center');
y1=uicontrol('Style','slider','Units','normalized',...
         'Position',[.2 .1 .25 .05],...
         'SliderStep',[5.0000e-004 5.0000e-003],...
         'Value',y,'Max',max_y,'Min',min_y,...
         'Callback','minhhoaxy(''sety'',1);minhhoaxy(''redraw'');');
y2=uicontrol('Style','edit','Units','normalized',...
         'Position',[.55 .1 .09 .05],...
         'FontSize',10','Fontname','VNI-Times',...
         'String',num2str(y),...
         'Callback','minhhoaxy(''sety'',2);minhhoaxy(''redraw'')');

     
dt=plot(x,y,'r.','EraseMode','xor','Markersize',50);
axis([-10 10 -10 10]);
grid;
DATA=[x;min_x;max_x;x1;x2;dt;y;min_y;max_y;y1;y2];

elseif strcmp(action,'redraw'),
    x=DATA(1);
    dt=DATA(6);
    set(DATA(5),'string',num2str(x));    
    set(DATA(4),'value',x);
    y=DATA(7);
    set(DATA(11),'string',num2str(y));    
    set(DATA(10),'value',y);

   
    set(dt,'XData',x);
    set(dt,'YDATA',y);
    drawnow;



elseif strcmp(action,'setx'),
    if (in1==1),
       DATA(1)=get(DATA(4),'value');   
    else
       min_x=DATA(2);     
       max_x=DATA(3); 
       x=str2double(get(DATA(5),'string'));
       if (x>max_x),
           x=max_x;
       end;
       if (x<min_x),
           x=min_x;
       end;
       DATA(1)=x;
    end
    
elseif strcmp(action,'sety'),
    if (in1==1),
       DATA(7)=get(DATA(10),'value');   
    else
       min_y=DATA(8);     
       max_y=DATA(9); 
       y=str2double(get(DATA(11),'string'));
       if (y>max_y),
           y=max_y;
       end;
       if (y<min_y),
           y=min_y;
       end;
       DATA(7)=y;
    end    
 
elseif strcmp(action,'done'),
    clf reset;
    clear global DATA
    close;
end

2 个答案:

答案 0 :(得分:1)

问题是因为图形处理used to be doubles but are now objects。因此,您无法创建包含两个数字和图形句柄的数组(但您以前可以这样做)。如果这样做,MATLAB将尝试在类型之间进行转换并失败。您的问题出在以下一行

DATA = [x; min_x; max_x; x1; x2; dt; y; min_y; max_y; y1; y2];

中间的dt有一个绘图对象的图形句柄,导致你看到的错误。

dt = plot(x,y,'r.','EraseMode','xor','Markersize',50);

为避免这种情况,您需要使用与数字数组不同的数据结构。

处理数据和图形处理的更好方法是使用struct或更易理解的数据类型。如下所示。

DATA.x = x;
DATA.y = y;
DATA.dt = dt;
...

否则你需要使用一个单元格数组,因为它可以包含两个对象和数字:

DATA = {x; min_x; max_x; x1; x2; dt; y; min_y; max_y; y1; y2};

答案 1 :(得分:0)

我在MATLAB 2016b中有相同的错误消息,即Cannot convert double value 1138.95 to a handle;在图像重建代码中使用具有傅立叶切片定理的双阵列。只需在命令行中键入clear all即可解决此问题。