Home > Enterprise >  How do I implement surfc in app designer?
How do I implement surfc in app designer?

Time:11-17

I have a problem, in matlab i can code to graph 3D plot of complex function with surfc this is my code for that :\

figure
X=-2*pi:0.2:2*pi; 
Y=-2*pi:0.2:2*pi; 
[x,y]=meshgrid(X,Y);
z=3*x 5i*y; 
sc=surfc(x,y,abs(z)) 
xlabel('Re(x)'); 
ylabel('Im(y)'); 
colormap jet 
colorbar

But here i want to implement this into app designer. I want user to input the complex function they have in cartesian with x and y (not polar), and then showing that plot to them. But when I run with the same code with a little bit of changes, I always get the error message : "Error using surfc. The surface z must contain more than one row or column.".

Here is my app designer callbacks :

% Button pushed function: MakeGraphButton         
function MakeGraphButtonPushed(app, event)
             x=-2*pi:0.2:2*pi;
             y=-2*pi:0.2:2*pi;
             [x,y]=meshgrid(x,y);
             z=app.ComplexFunctionEditField.Value;
             axes(app.UIAxes);
             surfc(x,y,abs(z))
             axis equal
             grid on
         end
     end 

I expect that it will show the graph on the app that i design, but it just showing that error message. How to make this works?

CodePudding user response:

You're taking the .Value of an edit field, so most likely that's a char. You're then using it as if you've evaluated some function of x and y, but you haven't! You might need something like

% Setup...
x = -2*pi:0.2:2*pi;
y = -2*pi:0.2:2*pi;
[x,y] = meshgrid(x,y);

% Get char of the function from input e.g. user entered '3*x 5i*y'
str_z = app.ComplexFunctionEditField.Value; 
% Convert this to a function handle. Careful here, you're converting
% arbitrary input into a function! 
% Should have more sanity checking that this isn't something "dangerous"
func_z = str2func( ['@(x,y)', str_z] );
% Use the function to evaluate z
z = func_z( x, y );

Now you've gone from a char input, via a function, to actually generating values of z which can be used for plotting

  • Related