name value pairs with variable input arguments

조회 수: 23 (최근 30일)
M NAGA JAYANTH AVADHANI
M NAGA JAYANTH AVADHANI 2020년 8월 17일
write a function called name_value_pair that has a variable no of input arguments representing name value pairs.The function must be called with even number of input arguments.The functiion returns a single cell array that has exactly two columns. First with names and another with values.If the functiion is called with no input arguments or odd number of input arguments or if the name is not of char type then it should return an empty cell array
function db = name_value_pairs(varargin)
if rem(nargin,2) ~= 0 || nargin < 2
db = {};
return
end
if ~ischar(varargin(1:2:nargin))
db = {};
end
for i = 1:nargin/2
db(i,1) = varargin(2*i-1);
db(i,2) = varargin(2*i);
end
end
this is code that i have written and ii cant understand where the error is. please help me with this.
  댓글 수: 3
M NAGA JAYANTH AVADHANI
M NAGA JAYANTH AVADHANI 2020년 8월 17일
편집: M NAGA JAYANTH AVADHANI 2020년 8월 17일
i am not getting an empty cell array if the name is not of type char
hesham hany
hesham hany 2020년 8월 18일
+1
but I didn't get the answer below

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

채택된 답변

Walter Roberson
Walter Roberson 2020년 8월 17일
편집: Walter Roberson 2020년 8월 17일
if ~ischar(varargin(1:2:nargin))
db = {};
else %changed
However you need to change what you are testing in the if. varargin() with () indices gives a cell array and ischar is false for cell array. You need to test the content of each cell. I suggest cellfun(@ischar, stuff)
Then you need to deal with the fact that the ischar will be true for some entries and false for others, but if is considered true only if all the entries are true. You need to be testing if ANY entry is not character, and equivalent to that would be to test whether ALL ischar is false.
  댓글 수: 3
Atif Penkar
Atif Penkar 2020년 8월 19일
Still get the same error
Walter Roberson
Walter Roberson 2020년 8월 20일
Atif what is your code?

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

추가 답변 (5개)

Sovendo Talapatra
Sovendo Talapatra 2020년 8월 20일
편집: Sovendo Talapatra 2020년 8월 20일
for j = 1:2:nargin
if ~ischar(varargin{j})
db = {};
return
else
continue
end
By using for loop you can easily fix the code

Abdul Quadir Khan
Abdul Quadir Khan 2020년 11월 7일
function db = name_value_pairs(varargin)
if rem(nargin,2) ~= 0 || nargin < 2
db = {};
return
end
if ~ischar(varargin(1:2:nargin))
db = {};
else %changed
end
for i = 1:nargin/2
db(i,1) = varargin(2*i-1);
db(i,2) = varargin(2*i);
end
for j = 1:2:nargin
if ~ischar(varargin{j})
db = {};
return
else
continue
end
end

Selman Baysal
Selman Baysal 2022년 1월 11일
"varargin" is a cell array; this is why we cannot use something like varargin{1:2:nargin} and ~ischar(varargin(1:2:nargin)) gives 1 everytime. I think using two different for loops make this problem easier like:
function db = name_value_pairs(varargin)
l = nargin;
if rem(l,2) ~= 0 || l < 2
db = {};
return
end
for ii = 1:2:l
if ~ischar(varargin{ii})
db = {};
return
end
end
for ii = 1:l/2
db(ii,1) = varargin(2*ii-1);
db(ii,2) = varargin(2*ii);
end
end

Yifan He
Yifan He 2022년 8월 3일
function db = name_value_pairs(varargin)
if nargin == 0
db = {};
elseif nargin/2 ~= fix(nargin/2)
db = {};
elseif sum(cellfun(@ischar,varargin(1:2:end))) ~= nargin/2
db = {};
else
db(:,1) = varargin(1:2:end);
db(:,2) = varargin(2:2:end);
end

José Armando Velasco Villagomez
편집: José Armando Velasco Villagomez 2023년 1월 8일
Check this function it may solve your problem.
function db = name_value_pairs(varargin)
if nargin == 0 || rem(nargin,2) || sum(cellfun(@ischar,varargin(1:2:end))) ~= nargin/2
db = {};
else
db = [(varargin(1:2:end))',(varargin(2:2:end))'];
end
end

카테고리

Help CenterFile Exchange에서 Argument Definitions에 대해 자세히 알아보기

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by