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

 채택된 답변

Rik
Rik 2023년 2월 27일

0 개 추천

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)
ans = logical
1
fcn({'1abc','1234'})
ans = logical
0

댓글 수: 3

Stephen23
Stephen23 2023년 2월 27일
편집: Stephen23 2023년 2월 27일
fcn = @(A) ...
iscellstr(A) && ...
size(vertcat(A{:}),2)==4 && ... <- fails if the text have different lengths
all(isstrprop([A{:}],'digit'));
fcn({'a','1234'})
Error using vertcat
Dimensions of arrays being concatenated are not consistent.

Error in solution (line 3)
size(vertcat(A{:}),2)==4 && ... <- will fail if the text have different lengths
Perhaps:
all(cellfun(@length,A)==4)
Rik
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)
Paul Mitchell
Paul Mitchell 2023년 2월 27일
편집: Paul Mitchell 2023년 2월 27일
Rik / Stephen - Thank You both very much for taking the time to look at solving my issue. It is much appreciated. Rik your origonal post solved my problem perfectly.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Programming에 대해 자세히 알아보기

제품

릴리스

R2020a

태그

질문:

2023년 2월 27일

편집:

2023년 2월 27일

Community Treasure Hunt

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

Start Hunting!

Translated by