Arguments Validation function in class/for object does not use my input values
10 次查看(过去 30 天)
显示 更早的评论
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 个评论
Steven Lord
2023-8-31
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.
回答(1 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Programming Utilities 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!