Function cannot read more than 3 objects as input ?

Hello, I have a function with four objects as inputs :
function test(obj.T1, obj.T2, obj.T3, obj.T4)
But when I run the fucntion the obj.T4 is not read at all and thus makes the function fail (I need some variables located in the T4 object).
I tried to change the order of the inputs like this :
function test(obj.T1, obj.T4, obj.T3, obj.T2)
And this time this is the T2 object which is not read !
Am I missing something ? This code used to run smoothly but I don't know if it was pure luck or if this structure should work in any cases...

6 comentarios

Net Fre
Net Fre el 16 de Nov. de 2021
Would be helpful if you attached the error message you get when trying to run the function
Jon
Jon el 16 de Nov. de 2021
This problem probably has to do with the details of how you implemented your function. Please provide the actual code for your function, and even better along with it a little example script that demonstrates the problem. If you cut and paste using the code button on the MATLAB Answers toolbar your code will be nicely formatted and can easily be copied to try to replicate the problem.
Hugo FOTIA
Hugo FOTIA el 16 de Nov. de 2021
Editada: Hugo FOTIA el 16 de Nov. de 2021
Here is the code :
function test(obj.T1, obj.T2, obj.T3, obj.T4)
figure(1);
clf(1);
Var1 = 10;
Var2 = 8;
Var3 = 3;
Var4 = T2.Pp.Pr*25;
Var5 = T2.Ca.Vp.Td;
Var6 = 5;
Var7 = 0;
GVSTOP(2);
T2.default.nbs=Var6;
T2.enable(false);
T2.setX(120);
tab = ones(1,T4.objA1.G0);
The error come from the "tab" (last line) where I want to create an array of ones of dimension 1 by G0, a variable coming from the T4 object. When I place a breakpoint on this last line, I can see in the Workspace window that the object T4 is not loaded (it is not displayed).
The error message says that the "tab" misses inputs, which is normal bcause the G0 value come from the T4 object which seems to be not loaded.
If I write
function test(obj.T1, obj.T4, obj.T3, obj.T2)
The code stops to the Var4 definition (line 9) because it can't find values coming from T2 object. But the T4 object is loaded (I can see it in Workspace window, but this time this is the T2 object which is missing).
Jon
Jon el 16 de Nov. de 2021
OK that's good. Now, could you also please provide a short script which calls your function and demonstrates the problem.
dpb
dpb el 16 de Nov. de 2021
Editada: dpb el 16 de Nov. de 2021
function test(obj.T1, obj.T2, obj.T3, obj.T4)
Don't use structure components as dummy arguments; either receive the object/struct itself and reference the fields it is by contract with the caller required to contain or use variables of the proper type and with meaningful local names internal to the function.
...
Var4 = T2.Pp.Pr*25;
Var5 = T2.Ca.Vp.Td;
...
tab = ones(1,T4.objA1.G0);
The structures T2 and T4 are undefined in the function before trying to use them.
That they're defined/extant in the caller workspace is irrelevant inside the function; every function has its own private workspace. If you need T2 and T4 inside your function, then pass them in the argument list (and use the struct root as the variable).
By the code posted, something like
function test(T2, T4)
...
would be one reasonable refactorization of the function.
Alternatively, (while I strongly doubt I would choose to do it that way, if you really are going to need all four of these structs at some point in the function and it's just not yet complete, if you were going to package them, then
function test(obj)
T2=obj.T2;
T4=obj.T4;
...
creates local copies that will go away automagically when the function exits.
Of course, one could use the more verbose form of obj.T2.xxx throughout and save the local memory/copy.
Hugo FOTIA
Hugo FOTIA el 17 de Nov. de 2021
Editada: Hugo FOTIA el 17 de Nov. de 2021
Jon wrote :
  • OK that's good. Now, could you also please provide a short script which calls your function and demonstrates the problem.
I'd like to but unfortunately this is not that simple, this code and this script is a small part of a huge program that runs before and after it, and I can't just run this as a standalone, I need the objects T1, T2, etc... to be created by other scripts before using this one.
But if you run it it gives you this error message because T4 is not read :
Not enough input arguments.
Error in test (line 21)
tab = ones(1,T4.objA1.G0);
dbp wrote :
  • Don't use structure components as dummy arguments; either receive the object/struct itself and reference the fields it is by contract with the caller required to contain or use variables of the proper type and with meaningful local names internal to the function.
I'm not sure I understand this sentence (sorry for my poor english), but you're basically saying that I should not use objects as inputs in a function ? Just to be clear these objects are already created with their methods, functions and properties by a program that runs before this code.
(And I actually have meaningful variables names but I'm not allowed to post it clearly in forums for confidentiality reasons, sorry about that).
  • The structures T2 and T4 are undefined in the function before trying to use them.
Yes that's right I actullay made a mistake while I copied the code here, the function actually look like this :
function test(T1, T2, T3, T4)
With T1, T2, etc.. being objects created beforehand.

Iniciar sesión para comentar.

 Respuesta aceptada

Hugo FOTIA
Hugo FOTIA el 17 de Nov. de 2021
Editada: Hugo FOTIA el 17 de Nov. de 2021
I found the solution, the problem came from the mlapp interface I was using, there was mistakes in the codes that led to not sending certain objetcs (as T4) in this function.
Thanks for your help and have a nice day !

Más respuestas (0)

Categorías

Más información sobre Programming en Centro de ayuda y File Exchange.

Productos

Versión

R2020b

Preguntada:

el 16 de Nov. de 2021

Editada:

el 17 de Nov. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by