0

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

1 Answer 1

1

Notice that according to the inline function documentation this function will be removed in future release; you can use, instead, anonymous functions (see below).

The inline function require, as input a string of characters while the get function returns the text of the edit box as a cellarray, therefore you have to convert it using the char function.

Also, once you have generated the inline object, it is you function, so you have to use it directly.

Using inline

You have to change your code this way:

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)

Using an anonymous function

You can achieve the same result by using anonymous functions this way:

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)

Edit

You can fix the problem with the setting of the line color and style this way:

  • modify the call to plot by adding the return value (it is the handle to the plot (see above: ph=plot(x,y))
  • chance the calls to set by replacing the call to plot with the handle of the plot itself (the ph variable as above)

So, to change the color and the line style, in your switch section:

set(ph,'color','r')
set(ph,'linestyle','--')

Hope this helps,

Qapla'

Sign up to request clarification or add additional context in comments.

11 Comments

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 ?
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.
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.
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 ?
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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.