Functions inputs/outputs

10 ビュー (過去 30 日間)
itai zilcha
itai zilcha 2021 年 3 月 8 日
回答済み: Helene Cheung 2021 年 3 月 16 日
Hi i've been coding with matlab for a while now and i ran into a problem with passing inputs to function from main.
For example :
function [cellAr] = MathematicalFunction()
%UNTITLED Summary of this function goes here
% Detailed explanation goes here
x = randi([-10,10] , 1,50);
y = randi([-10,10] , 1,50);
disp(x);
[xx,yy] = meshgrid(x,y);
z = sin(sqrt(xx.^2+yy.^2));
cellAr = {xx , 'x' , yy , 'y' , z , 'z'};
end
i wrote this code to make a meshgrid out of 2 random vectors and calculated a z axis.
For the next step i wanted to make another new function to plot the 3D graph. So i created a new void function that receives 2 inputs both are being sent from the main script and i was unable to send the cellAr and another input from the main script as new inputs to the 3D plot function to use and there were two problems that i couldn't solve :
1) "Too many output arguments". How can i have too many output arguments in a void function?
2)i was unable to send the newly generated cellAr and another string input to my Plot3 function through my main script.
my main looks like this :
clear clc
Array32 = MathematicalFunction();
ThreeDim = Plot3D(Array32,'surfc');
This isn't the first time i have a problem passing elements from main to another function so i must be doing something wrong.
  3 件のコメント
James Tursa
James Tursa 2021 年 3 月 8 日
Please show us the code you wrote for the Plot3D( ) function.
itai zilcha
itai zilcha 2021 年 3 月 8 日
編集済み: itai zilcha 2021 年 3 月 10 日
This is my plot3D function :
function Plot3D(Array32 ,str)
%UNTITLED3 Summary of this function goes here
% Detailed explanation goes here
switch str
case surfc
surfc(Array32(1),Array32(3),Array32(5))
disp('surfc')
case surf
surf(Array32(1),Array32(3),Array32(5))
disp('surf')
case contour
contour(Array32(1),Array32(3),Array32(5))
disp('contour')
otherwise
disp('non')
end
end

サインインしてコメントする。

採用された回答

James Tursa
James Tursa 2021 年 3 月 8 日
This is the signature of your Plot3D function:
function Plot3D(Array32 ,str)
As you can see, it doesn't return any outputs. But you are requesting an output when you call it like this:
ThreeDim = Plot3D(Array32,'surfc');
Hence the error. So call it without the requested output:
Plot3D(Array32,'surfc');
Also, inside this function you need to supply strings for the comparisons. E.g.,
case 'surfc'
  1 件のコメント
itai zilcha
itai zilcha 2021 年 3 月 8 日
thanks alot!

サインインしてコメントする。

その他の回答 (1 件)

Helene Cheung
Helene Cheung 2021 年 3 月 16 日
And except as James Tursa mentioned, your input Array32(i) of surfc(Array32(1),Array32(3),Array32(5)) are cells.
You should change your type of inputs as follows,
surfc(Array32{1},Array32{3},Array32{5})
Array32(1) is a cell, but Array{1} will be a double array.

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by