Matlab 图形用户界面绘图

Matlab 图形用户界面绘图,matlab,graphing,Matlab,Graphing,使用 在基本的m文件工作良好 但现在我正试图使用GUI绘制一个图,并输入一个函数。 它给了我很多错误。有人能解释一下,当我设置了x的范围时,如何给y赋值吗 x=-10:0.1:10 f=x+2 请注意,根据当前版本,此功能将在将来的版本中删除;您可以使用匿名函数(见下文) inline函数需要输入一个字符串,而get函数将编辑框的文本返回为字符,因此必须使用char函数对其进行转换 此外,一旦生成了inline对象,它就是您的函数,因此您必须直接使用它 使用内联 您必须以这种方式更改代码: %

使用

在基本的m文件工作良好

但现在我正试图使用GUI绘制一个图,并输入一个函数。 它给了我很多错误。有人能解释一下,当我设置了x的范围时,如何给y赋值吗

x=-10:0.1:10
f=x+2

请注意,根据当前版本,此功能将在将来的版本中删除;您可以使用匿名函数(见下文)

inline
函数需要输入一个字符串,而
get
函数将编辑框的文本返回为
字符
,因此必须使用
char
函数对其进行转换

此外,一旦生成了
inline
对象,它就是您的函数,因此您必须直接使用它

使用内联

您必须以这种方式更改代码:

% --- Executes on button press in zimet.
function zimet_Callback(hObject, eventdata, handles)
% hObject    handle to zimet (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
x=-10:0.1:10;
f=inline(get(handles.vdj,'string'))
y(x)=f

axes(handles.axes)
plot(x,y)







color=get(handles.listbox, 'value')
switch color
    case 2
       set(plot(x,y),'color', 'r')
    case 3
        set(plot(x,y),'color', 'g')
    case 4
        set(plot(x,y),'color', 'b')
end

style=get(handles.popupmenu, 'value')
switch style
    case 2
        set(plot(x,y), 'linestyle','--')
    case 3
        set(plot(x,y), 'linestyle','-.')
    case 4
        set(plot(x,y), 'linestyle',':')
end
rezgis=get(handles.grid, 'value')
switch rezgis
    case 1
        grid on
    case 2
        grid off
end
x=-10:0.1:10;
% f=inline(get(handles.vdj,'string'))
% y(x)=f
f=inline(char(get(handles.vdj,'string')))
y=f(x)
axes(handles.axes)
ph=plot(x,y)
使用匿名函数

您可以通过以下方式获得相同的结果:

% --- Executes on button press in zimet.
function zimet_Callback(hObject, eventdata, handles)
% hObject    handle to zimet (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
x=-10:0.1:10;
f=inline(get(handles.vdj,'string'))
y(x)=f

axes(handles.axes)
plot(x,y)







color=get(handles.listbox, 'value')
switch color
    case 2
       set(plot(x,y),'color', 'r')
    case 3
        set(plot(x,y),'color', 'g')
    case 4
        set(plot(x,y),'color', 'b')
end

style=get(handles.popupmenu, 'value')
switch style
    case 2
        set(plot(x,y), 'linestyle','--')
    case 3
        set(plot(x,y), 'linestyle','-.')
    case 4
        set(plot(x,y), 'linestyle',':')
end
rezgis=get(handles.grid, 'value')
switch rezgis
    case 1
        grid on
    case 2
        grid off
end
x=-10:0.1:10;
% f=inline(get(handles.vdj,'string'))
% y(x)=f
f=inline(char(get(handles.vdj,'string')))
y=f(x)
axes(handles.axes)
ph=plot(x,y)
编辑

可以通过以下方式解决线条颜色和样式设置的问题:

% --- Executes on button press in zimet.
function zimet_Callback(hObject, eventdata, handles)
% hObject    handle to zimet (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
x=-10:0.1:10;
f=inline(get(handles.vdj,'string'))
y(x)=f

axes(handles.axes)
plot(x,y)







color=get(handles.listbox, 'value')
switch color
    case 2
       set(plot(x,y),'color', 'r')
    case 3
        set(plot(x,y),'color', 'g')
    case 4
        set(plot(x,y),'color', 'b')
end

style=get(handles.popupmenu, 'value')
switch style
    case 2
        set(plot(x,y), 'linestyle','--')
    case 3
        set(plot(x,y), 'linestyle','-.')
    case 4
        set(plot(x,y), 'linestyle',':')
end
rezgis=get(handles.grid, 'value')
switch rezgis
    case 1
        grid on
    case 2
        grid off
end
x=-10:0.1:10;
% f=inline(get(handles.vdj,'string'))
% y(x)=f
f=inline(char(get(handles.vdj,'string')))
y=f(x)
axes(handles.axes)
ph=plot(x,y)
  • 通过添加返回值修改对
    plot
    的调用(它是plot的句柄(参见上文:
    ph=plot(x,y)
  • 通过将对绘图的调用替换为绘图本身的句柄(如上所述的
    ph
    变量),尝试调用
    set
因此,要更改颜色和线条样式,请在
开关
部分:

x=-10:0.1:10;
% Get the function as string
f_str=char(get(handles.vdj,'string'))
% add @(x) to the string you've got
f_str=['@(x) ' f_str ];
% Create the anonymous function
fh = str2func(f_str)
% Evaluate the anonymous function
y=fh(x)

axes(handles.axes)
ph=plot(x,y)
希望这有帮助


Qapla'

谢谢!但是,例如,如果我键入x^2,它将不再工作。我必须让它理解为。^2我假设。有没有一种方法可以这样做而不是自己键入它?我认为唯一的方法是您以正确的方式在文本框中定义函数。您可以在回调中添加一些代码来执行一些检查字符串以验证它。我还编辑了答案:
内联
函数将在未来的MatLab版本中删除。我使用
匿名函数
添加了一个可行的解决方案。谢谢!如果你不介意的话。也许你也可以知道为什么使用列表框、弹出菜单等。我已经设置了一个来设置图形的样式,另一个表示颜色。最后一个表示覆盖第一个。例如,我选中颜色绿色和虚线样式。它将是蓝色(默认)和虚线。如何阻止它覆盖?不清楚“覆盖”是什么意思。您应该发布代码的该部分,否则很难识别可能的错误。