Arguments Validation function in class/for object does not use my input values

7 views (last 30 days)
Hello there,
I am trying to create an object. Therefore, I want to validate my inputs in the constructor function with arguments validation function.
Function in which the object is created:
% Inputs:
num_Testruns = 100;
% testrun index: cell array with strings "01", "02", ... , "100" in cells
testrun_idx = cellstr(num2str([1:num_Testruns]'));
testrun_idx = regexprep(testrun_idx(:),' ' ,'0');
% cell array with testrun names
filenames(1:num_Testruns, 1) = {['Testrun_']};
filenames = strcat( testrun_filenames,testrun_idx );
% Creating the object
testrun = TESTRUN( 'C:\example', filenames, num_Testruns );
Object class:
classdef TESTRUN
properties
Filepath (1,:) char {mustBeFolder} %= pwd % character array
Filenames cell {mustBeA(Filenames,"cell")} % cell array with one column and variant numbers of lines
Num_Testruns (1,1) uint32 {mustBeA(Num_Testruns,"uint32"), mustBePositive} % should be a positive integer
end
methods
% Constructor:
function obj = TESTRUN( filepath, filenames, num_Testruns )
% arguments
% filepath (1,:) char {mustBeFolder}
% filenames cell {mustBeA(filenames,"cell")}
% num_Testruns (1,1) uint32 {mustBeA(num_Testruns,"uint32"), mustBePositive}
% end
obj.Filepath = filepath;
obj.Filenames = filenames;
obj.Num_Testruns = num_Testruns;
end
.
.
.
end
end
My problem with the validation functions:
Short: The validation functions do not take most of my inputs.
Detailed:
The validation functions for the property "Filepath" does not take any input arguments (doesn't matter if I write it as {mustBeFolder} or {mustBeFolder(Filepath)}) but it takes the default value "pwd" if I uncomment it.
The validation function for "Filenames" gets the input "cell" but does gets an empty input for the Filename argument and therefore throws an error.
For "Num_Testruns" the validation function mustBeA(Num_Testruns,"uint32") sets Num_Testruns to the default value zero and mustBePositive throws an error because zero is not positive.
What am I missing here? What do I have to correct?
(The class TESTRUN is defined in the same directory as the calling function. I am using Matlab 2020b)
  2 Comments
Steven Lord
Steven Lord on 31 Aug 2023
What is the full and exact text of the message(s) you receive (all the text displayed in orange and/or red in the Command Window) when you run this code? Don't paraphrase, copy the text verbatim.
AAA
AAA on 1 Sep 2023
Edited: AAA on 1 Sep 2023
1) Error using implicit default value of property 'Filepath' of class 'TESTRUN'.
Value must be text with one or more characters.
If I comment the validation of Filepath I get:
2) Error using implicit default value of property 'Num_Testruns' of class
'TESTRUN'. Value must be positive.
But if I debug the code and step into the functions I can see that there are always empty inputs (if there is a defined default value in the validation function it is used such as a 0x0 cell for Filenames or 0 for Num_Testruns- that's why the mustBeA() function does not throw an error)

Sign in to comment.

Answers (1)

AAA
AAA on 1 Sep 2023
I think I got it. Please correct me if I am wrong.
When creating an instance/object of a class Matlab first evaluates the properties and checks all the validation functions for certain properties with default values (in case that there are no default values, Matlab assumes default values automatically, e.g. my Filepath is an empty char, Filenames is an empty cell, Num_Testruns is zero -> uint32(0). Some default values passed my validation functions but others did not and throw an error.)
Only in a second step (after that initial property check) the constructor function is called and the input values are considered.
So I suppose the way Matlab wants us to handle this is to set certain default values which will pass the validation functions in a way like:
classdef TESTRUN
properties
Filepath (1,:) char {mustBeFolder} = 'C:\'
Filenames cell {mustBeA(Filenames,"cell")} = {['Testrun_xxxx']}
Num_Testruns (1,1) uint32 {mustBeA(Num_Testruns,"uint32"), mustBePositive} = uint32(1)
end
methods
% Constructor:
function obj = TESTRUN( filepath, filenames, num_Testruns )
% arguments
% filepath (1,:) char {mustBeFolder}
% filenames cell {mustBeA(filenames,"cell")}
% num_Testruns (1,1) uint32 {mustBeA(num_Testruns,"uint32"), mustBePositive}
% end
obj.Filepath = filepath;
obj.Filenames = filenames;
obj.Num_Testruns = num_Testruns;
end
.
.
.
end
end
(I got this from https://de.mathworks.com/help/matlab/matlab_oop/initialize-property-values.html -> last chapter "Property Validation Before Construction" )
This lead to a subsequent question:
After the initial construction, at which points/times are the validation checks performed?
Is there a possibility to perform a validation check for Num_Testruns which depends on the property Filenames for example?

Categories

Find more on Programming Utilities in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!