How to call funcitons inside a loop ? To automate my testing

I am new to Matlab and working on an Antenna testing system with Keysight instruments. I want to automatize my task with a loop and for that I need 3 input arguements to intiate the process. this is what I have tried so far:
function Auto_test (times, degrees, seconds)
for steps = 1:times % steps = how many times I need to rotate the motor
Step_Size = steps * degrees; % degrees = for ex. I need 30" step size for every rotation
MyArcus.PositionTo(Step_Size); % it's a funciton from another .m file for a stepper motor
pause(1.5)
% both of these functions from other .m file to collect the current marker's value for every single step from an analyzer
obj.Get_Marker_Power(1); % '1' is a selected marker's number
obj.Get_Marker_Freq(1);
pause (seconds)
end
pause(2);
MyArcus.PositionTo(0); % at the end motor comes back to 0 position
end
now when I try to run this code "my motor works with 100% accuracy" but (obj.Get_Marker_Power(1) and the other) shows an error.
an error
% Unable to resolve the name obj.Get_Marker_Power.
%
% Error in Auto_test (line 21)
% obj.Get_Marker_Power(1);
also I need to store the values of "Steps, power and freq." in a table for a graph
As I have spent 2 week only to understand the loops, I have an idea that functions can not be called inside loop. But I do not have any idea how to imporve my code according to my requirement. Please help me here.

3 commentaires

The variable called "obj" in your function is not defined anywhere in your code. You either need to pass it into the function as an input argument or you need to create it somewhere inside the function.
Thank you your response.
in my main file
classdef MyArcus
properties(Constant) % Constant values set into the motor
H_vel = 1000; % HSPD High speed setting for stage (pps)
L_vel = 300; % LSPD Low speed setting for stage (pps)
Accn = 300; % Accelaration time (ms)
time_out = 10; % Time out value (ms)
end
methods (Static)
function out = PositionTo(pos)
out = RunCMD('CLR');
out2 = RunCMD('MM');
if strcmp(out2,'0')&&(pos < 361)
RunCMD('X',num2str((3200 * pos) / 360));
end
end
end
this is my main file where I have defined the object for the motor and it is working perfect. there is no issue with below funciton.
MyArcus.PositionTo(Step_Size);
the only problem I am haivng is with these two funcitons which is also called form another main file seperately made for the signal analyzer.
obj.Get_Marker_Power(1);
obj.Get_Marker_Freq(1);
I need to run these three funcitons in a loop to collect data for analysis
See my answer below. I think you need the first approach I've listed there.

Connectez-vous pour commenter.

 Réponse acceptée

Bora Eryilmaz
Bora Eryilmaz le 12 Déc 2022
Modifié(e) : Bora Eryilmaz le 13 Déc 2022
The variable called "obj" in your function is not defined anywhere in your code. You either need to pass it into the function as an input argument or you need to create it somewhere inside the function.
Looks like you need something like:
obj = MyArcus; % Instantiate the device once
...
obj.PositionTo(Step_Size); % Call methods using the object reference.
obj.Get_Marker_Power(1);
or
obj = MyArcus; % Instantiate the device once
MyArcus.PositionTo(Step_Size); % Since some methods are static, you can use class name, too.
...
MyArcus.Get_Marker_Power(1);

6 commentaires

Chirag
Chirag le 13 Déc 2022
Modifié(e) : Chirag le 14 Déc 2022
I have tried to assign it to the Object as per your suggestion. but it is showing an error. here I am giving you more details about it.
obj = I assigned this object for the Keysight Signal Analyzer. for the
classdef Analyzer_EXA_N9010A
...
methods
function obj = Analyzer_EXA_N9010A()
...
MyArcus = it's a seperate device (stepper Motor) . and this is what I tried with your answer
obj = MyArcus.PositionTo(Step_Size); % belongs to motor (to send a command to set the degree)
...
obj.Get_Marker_Power(1);% belongs to the Analyzer (to receive a string value) on every iteration.
obj.Get_Marker_Freq(1); % same
and this is an error
>> Auto_test(3, 45, 2)
Unrecognized method, property, or field 'Get_Marker_Power' for class 'string'.
Error in Auto_test (line 20)
obj.Get_Marker_Power(1);
All of the functions works perfectly nice when execute seperately. But the problem is only within the loop. Here I am calling functions from two diff. class & it's methods
You will have to instantiate your objects (devices) outside of the loops and only once, and then refer to them using the object references:
obj = Analyzer_EXA_N9010A(); % Instantiate once and outside of the loop
...
for i = 1:10
obj.PositionTo(Step_Size); % Call methods in the loop
...
end
Otherwise, I think you were instantiating multiple objects for the same device and they are conflicting with each other. That is probably why they were working fine before using loops.
Chirag
Chirag le 13 Déc 2022
Modifié(e) : Chirag le 13 Déc 2022
Many Thnks @Bora Eryilmaz. it just simply working. =))
could you help me to store these values in a table. I want to add the values for every iteration
Step_Size, obj.Get_Marker_Power(1), and obj.Get_Marker_Freq(1).
You could do something like this:
T = table([],[],[],'VariableNames', ["Size", "Power", "Freq"]); % Create outside of the loop
for k = 1:5
Size = 10;
Power = 5;
Freq = 1000;
T(end+1,:) = {Size, Power, Freq}; % Add new rows of data in the loop
end
T
T = 5×3 table
Size Power Freq ____ _____ ____ 10 5 1000 10 5 1000 10 5 1000 10 5 1000 10 5 1000
Thanks a lot @Bora Eryilmaz. This works perfectly within the loop.
But there is a warning after many different trials for preallocationg the memory.
error : The variable 'H' appears to change size on every loop iteration. Consider preallocaiting for speed.
what could be the possible solution for this?
You can ignore that warning.

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by