Can anyone point out the solution to the error shown below (in the end)?
Thank you in advance.
function output = name_value_pairs(varargin)
if nargin == 0 || rem(length(varargin),2) ~= 0
output = {};
return;
end
for i = 1:length(varargin)
if ~ischar(varargin{i})
if ~isdouble(varargin{i}) || isfloat(varargin{i})
output = {};
return;
end
end
end
output = cell(((length(varargin))/2),2);
n =1 ;
for j = 1: (length(varargin))/2
for k = 1:2
output{j,k} = varargin{n};
n = n+1;
end
end
end

 採用された回答

Ameer Hamza
Ameer Hamza 2020 年 10 月 2 日
編集済み: Ameer Hamza 2020 年 10 月 2 日

1 投票

You can use reshape
function C = name_value_pairs(varargin)
if nargin == 0 || rem(numel(varargin),2) ~= 0
C = {};
return;
end
C = reshape(varargin, 2, []).';
if ~all(cellfun(@ischar, C))
C = {}
end
end

6 件のコメント

Milind Amga
Milind Amga 2020 年 10 月 2 日
The only problem in this code is that 'output' should be replaced with 'C' :)
Milind Amga
Milind Amga 2020 年 10 月 2 日
@Ameer
Thank you for the guidance.
Ameer Hamza
Ameer Hamza 2020 年 10 月 2 日
Thanks for pointing that out. I am glad to be of help!
Yigit Utku Er
Yigit Utku Er 2020 年 10 月 19 日
Hi @Amer i used reshape also however it didnt workout in the way i want.And i also see that u used dot transpose to solve the issue i been facing.How does dot transpose work can you explain ?
Ameer Hamza
Ameer Hamza 2020 年 10 月 19 日
.' is just use to change the array shape from row to column. For cell array, both ' and .' are equivalent. For matrix ' means conjucate transpose, and .' means simple transpose.
Chem
Chem 2021 年 12 月 9 日
The code is great, it only has one small bug, when using the line ~all(cellfun(@ischar, C)) it requires all columns to be of type char, however, the problem stated says that only the first column must be of type char, so a convenient change is ~all(cellfun(@ischar, C(:,1))))

サインインしてコメントする。

その他の回答 (1 件)

Steven Lord
Steven Lord 2020 年 10 月 2 日

0 投票

if ~isdouble(varargin{i}) || isfloat(varargin{i})
Assuming there were an isdouble function doing what you would expect from the name (Arthur Roué is correct, there is no such function in MATLAB though a few objects in various other MathWorks products have methods by that name) what could cause this condition to not pass?
If varargin{i} is a double, the first part of the if condition is false so we need to evaluate the second part. The second part is true so the whole condition is true.
If varargin{i} is a single, the first part of the if condition is true so we don't need to evaluate the second part.
If varargin{i} is an int8, the first part of the if condition is true so we don't need to evaluate the second part.
About the only condition I can think of offhand where this condition could fail would be an object that overloads isdouble and isfloat to return true and false respectively. If someone's willing to go to that length to trick MATLAB, they're likely to encounter other unusual behavior.

カテゴリ

ヘルプ センター および File ExchangeOperators and Elementary Operations についてさらに検索

質問済み:

2020 年 10 月 2 日

コメント済み:

2021 年 12 月 9 日

Community Treasure Hunt

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

Start Hunting!

Translated by