Why did my code break after adding a class definition?

5 views (last 30 days)
I have some code that interfaces with external hardware via loadlibrary to a vendor-supplied DLL. After getting everything working the way I wanted, I decided to refactor to make future maintainability easier. As part of this process, I defined a class params.m that holds experimental parameters (e.g. image height, image width, number of frames,...). Previously, I had just made a params struct and added stuff to it as needed (without a class definition). I also added arguments to my function definitions, e.g.:
function configure(inParams, boardNumber)
arguments
inParams (1,1) params
boardNumber (1,1) double {mustBeInteger, mustBePositive}
end
% call hardware functions from DLL
end
In doing so, this broke my experiment. The Matlab code is shared between two systems, one that has a single board and one that has two boards so configure is called in a for loop:
for boardId = 1 : numBoards
configure(inParams,boardId);
end
% do stuff
The code refactor broke my experiment, but only on the system that has two boards. While debugging, I noticed that inserting an arbitrary pause made the two-board system work every other time the code was run (which is better than never, but still 50%). After that realization, I tried commenting out the arguments definition:
function configure(inParams, boardNumber)
% arguments
% inParams (1,1) params
% boardNumber (1,1) double {mustBeInteger, mustBePositive}
% end
% call hardware functions from DLL
end
Which removed the need for the arbitrary pause, but still only got the two-board system to work correctly on every other function call (always works on first try, and from then on any odd-numbered run will work). It seems as though there is some kind of under-the-hood optimization(?) that is ocurring after I added a class definition that breaks things.
Does anyone have other ideas what the issue might be and/or how I can disable optimizations for that one function call (which appears to be impossible based on my reading). I recognize this is an exceptionally odd error and I'm happy to provide more information if that would be useful. I have also reached out to the hardware manufacturer to see if they have any suggestions for what might be causing this.
---------- EDIT ------------
After getting in touch with the hardware vendor, they instructed me to enable some debug features on the board(s) to help diagnose it from that side. This apparently fixed the issue as now it works reliably (even with debugging turned off again). This is somewhat unsatisfying to me as the code would work reliably with the old (pre-class) version of the code and fail reliably with the new version, so I don't think it was exclusively a hardware problem.
  8 Comments
Walter Roberson
Walter Roberson on 12 Sep 2024
configure(inParams,boardId);
Your class is a value class. Any changes to the class that you make will get discarded, as you are not saving the resulting object back to inParams .
I can't say I am completely sure what happens if you just change handle objects within the value class.. I guess that would work?
Benjamin
Benjamin on 12 Sep 2024
Nothing should be changing inside that function (not even the handles classes). Parameters from inParams are passed to the board via the vendor DLL to configure the acquisition.

Sign in to comment.

Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!