function and indexing errors

2 vues (au cours des 30 derniers jours)
Hichem Younsi
Hichem Younsi le 21 Déc 2018
hello a newest askig help
i have a function to calculate in a loop a variable
function Ls= WeLatente(WeSolFin,Lc,Ds)
Ls=0;
k=6;
for k=1:k
Ls(k)=Ls(k)+WeSolFin(k)'*Lc*Ds;
end
and i get 2 errors messages
Not enough input arguments.
and
Error in WeLatente (line 6)
Ls(k)=Ls(k)+WeSolFin(k)'*Lc*Ds;
any help i'll appreciate

Réponse acceptée

madhan ravi
madhan ravi le 21 Déc 2018
Sir Wlater is right about calling function other than that there is indeed an error inside the loop:
function Ls= WeLatente(WeSolFin,Lc,Ds)
k=6;
Ls=zeros(1,k); % preallocate
for k=2:k
% ^----- here
Ls(k)=Ls(k-1)+WeSolFin(k)'*Lc*Ds;
% ^^^----- here
end
  2 commentaires
Hichem Younsi
Hichem Younsi le 21 Déc 2018
i'm confused, function other than that there, i'm sorry
thank you i forget to initilize Ls ever i did it on the general cod, but i don't find the errors it stil said enough argument and errors in the equation even with that code
madhan ravi
madhan ravi le 21 Déc 2018

Connectez-vous pour commenter.

Plus de réponses (2)

Walter Roberson
Walter Roberson le 21 Déc 2018
you are trying to run the code by just clicking the green run button . When you click that button the function is run without passing in any parameters . you need to go down to the command line and invoke the function passing in appropriate parameters .
When you click on the run button , MATLAB will not look in the base workspace to try to find variables of the same name as the parameters .
  8 commentaires
Walter Roberson
Walter Roberson le 24 Déc 2018
is the error ont the command where i run it.
Yes. As we have told you before, when you click on the green Run button, MATLAB will not look in your workspace to find variables named in the function. Every function has its own set of variable names that are only meaningful within the function and do not refer to any other function's variables (exception: nested functions with shared variables.)
You need to go to the command line and command
Ls = WeLatente(WeSolFin, Lc, Ds);
That will allow it to take the variables WeSolFin and Lc and Ds from the base workspace and pass their values in to the function, which will attempt to use them.
You will then run into the problem that your code does
Ls(k)=Ls(k)+WeSolFin(k)'*Lc*Ds;
but your WeSolFin is a table() object, and indexing a table object at k using () indexing is not going to work, and if it worked it would not return a numeric value that could be added. What you probably need is to invoke,
Ls = WeLatente(WeSolFin{:,:}, Lc, Ds);
Note that this is the same thing that madhan told you to do in https://www.mathworks.com/matlabcentral/answers/436868-function-and-indexing-erros?s_tid=prof_contriblnk#answer_353555 but you missed seeing that because you ASKED THE SAME QUESTION TWICE and the discussion got split.
madhan ravi
madhan ravi le 24 Déc 2018
Modifié(e) : madhan ravi le 24 Déc 2018
+1 This explanation will sort out the error beautifully...

Connectez-vous pour commenter.


Hichem Younsi
Hichem Younsi le 24 Déc 2018
thank for every one who helped me, my code right run like this:
Ls= WeLatente(WeSolFin{:,:},Lc,Ds)
function Ls= WeLatente(WeSolFin,Lc,Ds) % function definition
Ls=zeros(1,6);
k=6;
for k=1:k
Ls(k)=Ls(k)+WeSolFin(k)*Lc*Ds;
end
end
so i realise i don't have to load the data on the work space the error was in the command line like said Walter Roberson,i didn't this befor thank you sir and the use of the bruket {}, however the resultes doesn't match the one i calculate befor withe excel with a difference of 600

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Tags

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by