Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

Graphing with GUI

Using

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

in basic m-file works fine.

But now I am trying to draw a plot using GUI, and by inputing a function. It gives me bunch of errors. Could anyone explain how I can give values to the y when I have the range of x set?

% --- 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

Answer*

Cancel
11
  • Thanks ! But if I , for example, type in x^2 , it doesnt work again. I must get it to understand it as .^2 I assume. is there a way of doing that instead of typing it myself ? Commented Apr 1, 2017 at 15:41
  • I think the only way is you to take care of define the function in the textbox in the right way. You can add, in the callback some code to perform some checks on the string in order to validate it. Commented Apr 1, 2017 at 15:57
  • I've also edited the answer: the inline function will be removed in future release of MatLab. I've added a posible solution using anonymous functions. Commented Apr 1, 2017 at 15:59
  • Thanks! If you dont mind.. perhaps you could also know why using listboxes, popupmenus etc.. I have set one to set the style of the graph, the other for color. The last one overwrites the first one. For example I check color green and dotted style. It will be blue (default) and dotted. How can I stop it from overwriting ? Commented Apr 1, 2017 at 16:16
  • It is not clear what you mean with "overwritting". You should post that part of your code otherwise it is very difficult to identify the possible errors. Commented Apr 1, 2017 at 16:32