1

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?

Aji Wibowo
  • 23
  • 4
  • 1
    we don't know what `app.ComplexFunctionEditField.Value;` is, so we can't help. But whatever it is, its not the same as in your first code – Ander Biguri Nov 16 '22 at 14:53

1 Answers1

0

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

Wolfie
  • 27,562
  • 7
  • 28
  • 55
  • really thank you for your suggestion it works really well. I have try to make it into double with str2double but never thought about str2func exist. Thank you very much. And also want to ask, why is the graph popping up on another figure window instead of in the app designer UIAxes figure ??? – Aji Wibowo Nov 16 '22 at 16:54
  • From the [documentation](https://uk.mathworks.com/help/matlab/ref/surfc.html) You can give `surfc` a parent input, did you try removing the `axes(obj.UIAxes)` line and instead using `surfc(obj.UIAxes,x,y,z)`? Try commenting out `axis equal` and `grid on` to check which of the commands is creating the new figure – Wolfie Nov 16 '22 at 17:35