How to get property validation functions to show source file location on error?

1 Ansicht (letzte 30 Tage)
When using property validation functions for class propeties, if an error is thrown, MATLAB does not show the line number or any other source file information. This is in contrast to the behavior with function argument validation.
Here is an example class definition and function definition to demonstrate.
classdef ValidationClass
properties
PositiveProp {mustBePositive};
end
end
function validationFunction(PositiveArg)
arguments
PositiveArg {mustBePositive};
end
end
When I call the following code inside of a script, I get an error as expected.
validationFunction(-1);
% Prints the following error message:
% Error using error_test_script>validationFunction
% validationFunction(-1);
% ↑
% Invalid argument at position 1. Value must be positive.
%
% Error in error_test_script (line 9)
% validationFunction(-1);
This error message is useful, as it shows the exact line of code causing the error (with hyperlinks). However, when I do the same thing with class properties, I do not get a useful message.
test = ValidationClass;
test.PositiveProp = -1;
% Prints the following error message:
% Error using error_test_script
% Error setting property 'PositiveProp' of class 'ValidationClass'. Value must be positive.
This error message has much less info. It is very difficult to identify where the error occured. Is there a way to make the error message similar to that of the function argument version?

Akzeptierte Antwort

Matt J
Matt J am 22 Nov. 2022
It is very difficult to identify where the error occured.
Running in debug mode will take you to the point where the error occurred:
>> dbstop if error
>>test
  1 Kommentar
Matthew Dvorsky
Matthew Dvorsky am 8 Dez. 2022
Thanks, this is a very helpful suggestion. Hopefully MATLAB will be updated to give better error messages in these cases, but this is a good temporary solution.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Ayush
Ayush am 22 Nov. 2022
Verschoben: Matt J am 22 Nov. 2022
@Matthew Dvorsky This seems to be a requested feature which is not currently available.
Kindly contact us through MathWorks Support.

Kategorien

Mehr zu Argument Definitions finden Sie in Help Center und File Exchange

Produkte


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by