Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Guide; Edit_text; equation; two variables

Subject: Guide; Edit_text; equation; two variables

From: Arkadiusz Kowalski

Date: 19 Mar, 2013 14:08:09

Message: 1 of 3

Hello,
I'm trying to create GUI ("guide") which will plot function with two variables in 3D.
Function is given by user in Edit_text (e.g. "x.^2-y.^2"),
also range and accuracy of variable "y" and "x" is given in Edit_text.
On push_button:
%----------------------------------------------------------------------
global a b c d e f x y X Y fun2
a=str2num(get(handles.a,'String'));
b=str2num(get(handles.b,'String'));
    e=str2num(get(handles.e,'String'));
c=str2num(get(handles.c,'String'));
d=str2num(get(handles.d,'String'));
    f=str2num(get(handles.f,'String'));

x=a:e:b;
y=c:f:d;
fun2=get(handles.edit_funkcja,'String');
[X,Y]=meshgrid(x,y);
fun2=eval(regexprep(regexprep(fun2,'y','Y'),'x','X'));
mesh(X,Y,fun2)
%----------------------------------------------------------------------
When I try to Run figure I receive an error in line:
"fun2=eval(regexprep(regexprep(fun2,'y','Y'),'x','X'));"
And I don't understand why? I'm replacing vector "y" and "x" to matrix "Y" and "X", and eval to interpret strings containing matlab expressions.

Subject: Guide; Edit_text; equation; two variables

From: Kristin

Date: 19 Mar, 2013 22:43:11

Message: 2 of 3

"Arkadiusz Kowalski" <disciples7@wp.pl> wrote in message <ki9rg9$g5n$1@newscl01ah.mathworks.com>...
> Hello,
> I'm trying to create GUI ("guide") which will plot function with two variables in 3D.
> Function is given by user in Edit_text (e.g. "x.^2-y.^2"),
> also range and accuracy of variable "y" and "x" is given in Edit_text.
> On push_button:
> %----------------------------------------------------------------------
> global a b c d e f x y X Y fun2
> a=str2num(get(handles.a,'String'));
> b=str2num(get(handles.b,'String'));
> e=str2num(get(handles.e,'String'));
> c=str2num(get(handles.c,'String'));
> d=str2num(get(handles.d,'String'));
> f=str2num(get(handles.f,'String'));
>
> x=a:e:b;
> y=c:f:d;
> fun2=get(handles.edit_funkcja,'String');
> [X,Y]=meshgrid(x,y);
> fun2=eval(regexprep(regexprep(fun2,'y','Y'),'x','X'));
> mesh(X,Y,fun2)
> %----------------------------------------------------------------------
> When I try to Run figure I receive an error in line:
> "fun2=eval(regexprep(regexprep(fun2,'y','Y'),'x','X'));"
> And I don't understand why? I'm replacing vector "y" and "x" to matrix "Y" and "X", and eval to interpret strings containing matlab expressions.

What does the error message say?

You could try putting a breakpoint right after meshgrid(x,y) and evaluate the inner REGEXPREP expression.

You could also try converting the string to an anonymous function, if you always know the number of arguments. Then you could pass X and Y into the function as variables.

Subject: Guide; Edit_text; equation; two variables

From: Arkadiusz Kowalski

Date: 20 Mar, 2013 08:27:07

Message: 3 of 3

I don't know why but errors disappeared when I created new GUI with the same Edit_text objects and one Pushbutton with:
%---------------------------------
global fun2 x y h1
global x_min x_acc x_max y_min y_acc y_max

x_min=str2num(get(handles.edit_x_min,'String'));
    x_acc=str2num(get(handles.edit_x_acc,'String'));
x_max=str2num(get(handles.edit_x_max,'String'));
y_min=str2num(get(handles.edit_y_min,'String'));
    y_acc=str2num(get(handles.edit_y_acc,'String'));
y_max=str2num(get(handles.edit_y_max,'String'));

x=x_min:x_acc:x_max;
y=y_min:y_acc:y_max;
[X,Y]=meshgrid(x,y);

fun2=get(handles.edit_fun,'String');
h1=eval(regexprep(regexprep(fun2,'y','Y'),'x','X'));
surf(X,Y,h1)
grid on
%---------------------------------
I only changed tags and created new variable h1.
Thank you for your hints and efforts. Everything works fine.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us