Conversion of mxLogical to bool in (C) MEX-files

4 views (last 30 days)
At the moment and for Linux 64-bit installs at least, mxLogical and bool types are one and the same. However I seem to understand from statements like this one that it is not guaranteed that this will remain true. Thus I wonder if it wouldn't be adequate to add a function for getting an array of bool values corresponding to an existing mxArray of type mxLogical. Something like:
bool *mxGetBools(const mxArray *array_ptr);
just like mxGetString provides a way to convert mxChar arrays to char arrays. This would truly only apply to arrays as there's already the function mxIsLogicalScalarTrue for scalars of mxLogical type. The use case for this would be when external libraries are requiring inputs as `bool *`.
I understand that this type of conversion could be done by the user via the mxIsLogicalScalarTrue function iterated on all array elements, but I think it would be nicer if it would be part of the API.
  1 Comment
James Tursa
James Tursa on 3 Sep 2022
Edited: James Tursa on 3 Sep 2022
You could always put in an enhancement request, I suppose. But probably the only way to guarantee you got the correct "bit pattern" for the bool in the background would be to allocate new memory for the array and run a loop to fill the elements based on the non-zeroness of the mxLogical values, then return the pointer to that memory. This would be different behavior from the other mxGetEtc numeric functions that only return pointers to existing data memory.

Sign in to comment.

Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!