필터 지우기
필터 지우기

Property value validation with values from file

조회 수: 1 (최근 30일)
Marcel-Dennis Boerzel
Marcel-Dennis Boerzel 2022년 4월 16일
댓글: Marcel-Dennis Boerzel 2022년 4월 24일
I have a matlab class called Seq with the property seqName. Now I want to verify, if a value assigned to that property is a member of strings which are stored in a .csv file.
I can use mustBeMember
PropName {mustBeMember(seqName,{'movieNameA','movieNameB','movieNameC'})} = 'defaultMovieName'
But how can I replace {'movieNameA','movieNameB','movieNameC'} with an imported .csv file which contains the allowed values?
Thanks in advance
Dennis
  댓글 수: 1
Marcel-Dennis Boerzel
Marcel-Dennis Boerzel 2022년 4월 16일
편집: Marcel-Dennis Boerzel 2022년 4월 16일
One additionally information. For me it is not important that it is a .csv-file. Any other importable filetype is ok for me as well.

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

채택된 답변

Steven Lord
Steven Lord 2022년 4월 16일
The census.mat file contains two variables, cdate and pop.
whos -file census.mat
Name Size Bytes Class Attributes cdate 21x1 168 double pop 21x1 168 double
Let's write a function that will retrieve the data from one of those variables specified by the user.
y = example1698110('cdate')
y = 21×1
1790 1800 1810 1820 1830 1840 1850 1860 1870 1880
y = example1698110('pop')
y = 21×1
3.9000 5.3000 7.2000 9.6000 12.9000 17.1000 23.1000 31.4000 38.6000 50.2000
y = example1698110('notInCensus')
Error using solution>example1698110
Invalid argument at position 1. Value must be a member of this set:
'cdate'
'pop'
function y = example1698110(name)
arguments
% Use a local validator that uses mustBeMember as part of its operation
name string {validateNameInput(name)}
end
y = load('census.mat', name).(name);
end
function validateNameInput(name)
% Use whatever means necessary to assemble the list of acceptable strings
varnames = {whos('-file','census.mat').name};
mustBeMember(name, varnames)
end

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Workspace Variables and MAT-Files에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by