input parser on a cell array
1 次查看(过去 30 天)
显示 更早的评论
I have an input cell array example A = {'1234','4567','8901'}
I wish to user the input parser addParameter to check each entry is a string, and also each string is only composed of a set of 4 numbers
is there anyway in which to do this ? I have been searching Help and Answers but cannot find anything
Thank You
0 个评论
采纳的回答
Rik
2023-2-27
You can implement a custom validationFcn to check your requirements, as you can read in the documentation.
A = {'1234','4567','8901'};
fcn = @(A) ...
iscellstr(A) && ...
size(vertcat(A{:}),2)==4 && ...
all(isstrprop([A{:}],'digit'));
fcn(A)
fcn({'1abc','1234'})
3 个评论
Rik
2023-2-27
编辑:Rik
2023-2-27
From what I can tell from the documentation, a fail is interpreted as invalid input, so my function should work. Although it is indeed more elegant to have this function not fail.
If any of those chars is a column vector this code will still return an error.
So perhaps:
all(cellfun('size',A,2)==4) && all(cellfun('size',A,1)==1)
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!