簡體   English   中英

如何在應用程序設計器中實現 surfc?

[英]How do I implement surfc in app designer?

我有一個問題,在 matlab 中,我可以使用 surfc 編碼為復雜的 function 的圖形 3D plot 這是我的代碼:\

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

但在這里我想將其實現到應用程序設計器中。 我希望用戶輸入復雜的 function 他們在 x 和 y(非極坐標)的笛卡爾坐標系中,然后向他們顯示 plot。 但是當我運行相同的代碼並稍作更改時,我總是會收到錯誤消息:“使用 surfc 時出錯。表面 z 必須包含不止一行或一列。”。

這是我的應用程序設計器回調:

% 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 

我希望它會在我設計的應用程序上顯示圖表,但它只是顯示該錯誤消息。 如何使這個工作?

您正在獲取編輯字段的.Value ,因此很可能是一個字符。 然后你使用它就好像你已經評估了一些 function 的xy ,但你沒有! 你可能需要像

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

現在您已經從 char 輸入,通過 function,實際生成可用於繪圖的z

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM