Not Enough Input Argument for loop in the outer shell of a nested function

1 view (last 30 days)
I am continuing to work on writing a csv file of track data from MotionBasedMultiObjectTrackingExample. I have got a small for loop that works within the displayTrackingResults function, but works in a way that slows processing to a crawl. The code and placement was as follows:
mask = insertObjectAnnotation ....
for write = 1:length(reliableTracks)
S=rmfield(reliableTracks, 'kalmanFilter');
writetable(struct2table(S), 'tracks.csv')
end
......
To address the processing issue, I felt I needed to move this to outer shell function with placement as follows:
function MotionBasedMultiObjectTrackingExample(reliableTracks)
...
while ...
...
end
for write = 1:length(reliableTracks)
S=rmfield(reliableTracks, 'kalmanFilter');
writetable(struct2table(S), 'tracks.csv')
end
Now I am receiving the error:
Not Enough Input Arguments
Error in MBMOTForRF_A400_Mine_1_W3_Rockspilt>MotionBasedMultiObjectTrackingExample (line 85)
for write = 1:length(reliableTracks)
Error in MBMOTForRF_A400_Mine_1_W3_Rockspilt (line 55)
MotionBasedMultiObjectTrackingExample()
I am lost as to what the problem here is. Any help would be greatly appreciated. Thank you for your time.
--Christine

Answers (1)

Dave B
Dave B on 29 Jul 2021
Christine: It looks the place where you're calling MotionBasedMultiObjectTrackingExample needs to pass in the argument. It appears that MBMOTForRF_A400_Mine_1_W3_Rockspilt line 55 is currently MotionBasedMultiObjectTrackingExample() but should be MotionBasedMultiObjectTrackingExample(reliableTracks)
But I don't think this is why it was slow in the first place:
for write = 1:length(reliableTracks)
S=rmfield(reliableTracks, 'kalmanFilter');
writetable(struct2table(S), 'tracks.csv')
end
you're looping over reliableTracks, which I think is a struct with multiple elements. But you're not using 'write' in your loop, so it looks like you're just writing the same table (to the same file) over and over again (?) In other words the two lines in your for loop are excuting length(reliableTracks) times, but each time they're doing the same thing.
Could it be that you inteded something more like...?
S = rmfield(reliableTracks, 'kalmanFilter');
for write = 1:length(reliableTracks)
writetable(struct2table(S(write)), [num2str(write) 'tracks.csv'])
end
  2 Comments
Christine Deer
Christine Deer on 30 Jul 2021
I've tried the recommended changes on line 55 with MotionBasedMultiObjectTrackingExample(reliableTracks), which generates the error:
Unrecognized function or variable 'reliableTracks'.
Error in MBMOTForRF_A400_Mine_1_W3_Rockspilt (line 55)
MotionBasedMultiObjectTrackingExample(reliableTracks)
Is there a different way of passing reliable tracks?
Thank you again for your time.
Dave B
Dave B on 31 Jul 2021
Christine - it's hard to say without knowing more about your code, it sounds like reliableTracks (the thing you're going to loop over) hasn't been defined in MBMOTForRF_A400_Mine_1_W3_Rockspilt. It doesn't matter what you call it, but the input to MotionBasedMultiObjectTrackingExample has to be defined.
If you attach your code I can try to navigate through it but I'm guessing it'll be difficult to figure out your intent (i.e. how the variable 'should be' defined). Below are some thoughts based on some guesswork, and then some very simplified examples of how functions (and nested functions) work.
The way I'd imagine your code looks (or should look if it worked):
% MBMOTForRF_A400_Mine_1_W3_Rockspilt.m
% (not sure if it's a function or not)
% ... m
goodTracks = ...; % This could be called reliableTracks or goodTracks, but it must be defined.
MotionBasedMultiObjectTrackingExample(goodTracks);
An alternate that I can picture:
% MBMOTForRF_A400_Mine_1_W3_Rockspilt.m
%...
allTracks = %...;
MotionBasedMultiObjectTrackingExample(allTracks);
% MotionBasedMultiObjectTrackingExample.m
function MotionBasedMultiObjectTrackingExample(tracks)
reliableTracks = tracks([tracks.reliability] > 1);
%...
end
Here are some examples that show a little of how subfunctions and nested functions work. This is a brief view, the MATLAB documentation is certainly more carefully written and thorough:
In general, with a function (whether it's a "local function" defined in the same file or a separate function defined in a different file) you work in a separate namespace. What that means is that the only variables you have access to in your function are those which are passed in. This is sometimes called 'pass by value', because it's the value that's passed to the function...changes to the variable in a function don't affect the variable that the function was called with. Importantly, the name of the variables in the function and in the function's caller can be completely different.
function mainfunc
x=3.14;
subfunc(x)
disp("x = " + x)
% param is not defined here
end
function subfunc(param)
% x is not defined here
param = cos(param);
disp("param = " + param)
end
Output:
param = -1
x = 3.14
One exception to this is nested functions. A nested function (a function inside a function) has access to the variables defined above it. This means it can do things to those values
function mainfunc
x = 3.14;
function nestfunc(param)
param = cos(param);
disp("param = " + param)
x = sin(x);
end
disp("x = " + x)
nestfunc(x)
disp("x = " + x)
end

Sign in to comment.

Categories

Find more on Scope Variables and Generate Names in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!