structure inside a function

16 Ansichten (letzte 30 Tage)
Ashlianne Sharma
Ashlianne Sharma am 12 Okt. 2020
Kommentiert: Ameer Hamza am 12 Okt. 2020
Hey all!
I am having a ton of issues with this line of code. So I have this equation in a function in which I have made the structure "inputs" with the different aircraft properties. in a separate script, I have called the function and labeled again the inputs from my function page. Along with this, I have named my vmax.
I keep getting the same error message of too many input arguments. What am I getting wrong?
D = (0.5*rho0*((vmax*1.687810)^2)*inputs.S*inputs.Cd0)+(((inputs.WS*inputs.S)^2)/(0.5*rho0*((vmax*1.687810)^2)*inputs.E*inputs.AR*inputs.S*pi));
  9 Kommentare
Stephen23
Stephen23 am 12 Okt. 2020
Bearbeitet: Stephen23 am 12 Okt. 2020
"I am really concerned about my equation having too many input values."
As Ameer Hamza explained, the cause of the error is your inadvertent use of comma-separated lists generated from a non-scalar structure:
Why do you define inputs as an input to the function hw_dragPower_sharmaAshlianne but then overwrite this with data inside the function? What is the point of that?
Ashlianne Sharma
Ashlianne Sharma am 12 Okt. 2020
Just as I have commented below, this is the whole error message in my command window.
>> hw_dragPower_sharmaAshlianne(vmax, inputs)
Error using *
Too many input arguments.
Error in hw_dragPower_sharmaAshlianne (line 50)
D =
(0.5*rho0*((vmax*1.687810)^2)*179*inputs.Cd0)+(((inputs.WS.*inputs.S)^2)/(0.5*rho0*((vmax*1.687810)^2)*inputs.E.*inputs.AR.*inputs.S.*pi));
% [lbf]

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Ameer Hamza
Ameer Hamza am 12 Okt. 2020
Bearbeitet: Ameer Hamza am 12 Okt. 2020
In your code 'inputs' is a 3-element array of struct. When you write
inputs.Cd0
It returns 3 values. If you want to multiply all three values with a number then you need to write it like this
a*[inputs.Cd0]; % 'a' is used as an example here.
In that case, you may also need to use element-wise operators: https://www.mathworks.com/help/matlab/matlab_prog/array-vs-matrix-operations.html
  6 Kommentare
Ashlianne Sharma
Ashlianne Sharma am 12 Okt. 2020
For all of you that are helping me, you all are the smartest people ive ever talked to!
Ameer Hamza, sir, I think the way you explained this really helped! Now I understand that you must use brackets when using structure values in a function. You all taught me that the extra period goes between structures when multiplying or dividing the two (but not INSIDE the brackets) that is where I found one of my mistakes. Thanks to you all, I was able to fix my V equation aswell.
I just need to learn how to display the three output values as a vector. Thank you all so much for your help. I think you saved me from a mental breakdown.
Ameer Hamza
Ameer Hamza am 12 Okt. 2020
I am glad to be of help! :)
You can display the outputs as 3 sepeate vectors instead of combining in a single array
disp(V);
disp(D);
disp(P);
or you can combine them as column vectors in the matrix
outputs = [V.' D.' P .'];

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

KSSV
KSSV am 12 Okt. 2020
Bearbeitet: KSSV am 12 Okt. 2020
If you have two equal vectors, like:
a = rand(1,3) ;
b = rand(1,3) ;
m = a.*b % this should be used. This iscalled element by element multiplication
d = a./b % element by element divison
p = a*b % this will trhow error.
l = a/b % this is not desired
In your case: Try the below. If any error, try using .*, ./ at vectors multiplication and divison.
D = (0.5*rho0*((vmax*1.687810)^2)*inputs.S.*inputs.Cd0)+(((inputs.WS.*inputs.S).^2)./(0.5*rho0*((vmax*1.687810)^2)*inputs.E.*inputs.AR.*inputs.S*pi));
  2 Kommentare
Ashlianne Sharma
Ashlianne Sharma am 12 Okt. 2020
I am still getting that there are too many input arguments..
I tried using the input.WS.
and I tried [input.WS.]
and [input.WS]
all of which gave me the same error code of too many input arguments
KSSV
KSSV am 12 Okt. 2020
Try like this. If not working check the size of each S, Cd0, WS etc.....If still there is a error. Share your code. There should be some other problem.
S = [input.S] ;
Cd0 = [inputs.Cd0] ;
WS = [inputs.WS] ;
E = inputs.E] ;
AR = [inputs.AR] ;
D = (0.5*rho0*((vmax*1.687810)^2)*S.*Cd0)+(((WS.*S).^2)./(0.5*rho0*((vmax*1.687810)^2)*E.*AR.*S*pi));

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Programming finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by