having issues with display name value pairs of matlab issue

3 ビュー (過去 30 日間)
Mohamed elsidey
Mohamed elsidey 2021 年 5 月 16 日
コメント済み: Stephen23 2025 年 4 月 26 日
Hey Guys
I am trying to learn Matlab so I have went through introduction and successfully completed it and now I am now at mastering course so I have an issue of printing name value pairs problem I have ran the code it seems to work pretty well but the Terminal always pops up some issues of display the array of variables
here is my code:
function output_Array = name_value_pairs(varargin)
output_Array = {};
vec_length = nargin;
if vec_length <= 0
celldisp(output_Array);
error('you have to input even numbers or more than one input and make sure your name is type of char');
end
for ii = 1 : nargin
vec_length = nargin;
if mod(ii,2) ~= 0
fprintf('%d',ii);
kk = ii;
elseif mod(ii,2) == 0
fprintf('%d', ii);
end
if nargin == 1 || mod(vec_length,2) ~= 0 || ~ischar(varargin{kk})
celldisp(output_Array);
error('you have to input even numbers or more than one input and make sure your name is type of char');
else
output_Array = {varargin{kk}, varargin{ii}};
celldisp(output_Array);
end
end
end

回答 (1 件)

Raag
Raag 2025 年 4 月 26 日
Hi Mohamed,
It is my understanding that you want to print cell arrays of name-value pairs based on the variable argument inputs to your function. In the given code, error handling is repeated inside the loop, and the function only outputs the last pair instead of all pairs. Additionally, the code given does not robustly check that each "name" is a character array or string, and the frequent use of celldisp and error” within the loop interrupts normal execution. Below is a refined version of your function:
function output_Array = name_value_pairs(varargin)
output_Array = {};
% It checks for an even number of input arguments, ensuring complete name-value pairs
if nargin < 2 || mod(nargin, 2) ~= 0
error('You must input an even number of arguments, with at least one name-value pair.');
end
for ii = 1:2:nargin
% It validates that each name is a character array or string
if ~ischar(varargin{ii}) && ~isstring(varargin{ii})
error('The name must be a character array or string.');
end
% It appends pairs to the output array correctly, maintaining all pairs for display
output_Array = [output_Array; {varargin{ii}, varargin{ii+1}}];
end
celldisp(output_Array);
end
For a better understanding of the above solution, refer to the following MATLAB documentations:
  1 件のコメント
Stephen23
Stephen23 2025 年 4 月 26 日
Note that
[output_Array; {varargin{ii}, varargin{ii+1}}];
is simply
[output_Array; varargin(ii), varargin(ii+1)];
Note also that OUTPUT_ARRAY is an identical copy of VARARGIN, created in a slow way inside the loop. Simpler and much more efficient:
output_Array = varargin;
The usual recommended approach is to use argument validation or the input parser:

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

カテゴリ

Help Center および File ExchangeGet Started with MATLAB についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by