how to check if inputs for my function are strings or vectors

4 ビュー (過去 30 日間)
carly
carly 2022 年 12 月 4 日
編集済み: Jan 2022 年 12 月 4 日
like for my function, when the user inputs an argument that is not a string or vector, i want them to receive an error and for it to say "Input must be a string or vector."
this is what i have so far:
function [output] = function(input1, input2, input3)
if ~isstring(input1) || nargin==3 && ~isstring(input2) || nargin==3 && ~isstring(input3)
error('Input must be a string or vector.')
elseif ~isvector(input1) || nargin==3 && ~isvector(input2) || nargin==3 && ~isvector(input3)
error('Input must be a string or vector.')
end
  4 件のコメント
Matt J
Matt J 2022 年 12 月 4 日
Back up copy of original question:
like for my function, when the user inputs an argument that is not a string or vector, i want them to receive an error and for it to say "Input must be a string or vector."
this is what i have so far:
function [output] = function(input1, input2, input3)
if ~isstring(input1) || nargin==3 && ~isstring(input2) || nargin==3 && ~isstring(input3)
error('Input must be a string or vector.')
elseif ~isvector(input1) || nargin==3 && ~isvector(input2) || nargin==3 && ~isvector(input3)
error('Input must be a string or vector.')
Jan
Jan 2022 年 12 月 4 日
編集済み: Jan 2022 年 12 月 4 日
@carly: This public forum is based on sharing questions and answers. If you have found a solution by your own, post it as answer. Deleting the contents of the question is not helpful and shows a missing repect for the given answers. Therefore you question has been restored.

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

回答 (1 件)

Matt J
Matt J 2022 年 12 月 4 日
編集済み: Matt J 2022 年 12 月 4 日
function [output] = function(varargin)
varargin(4:end)=[];
valid=all( cellfun(@(z)isstring(z) | isvector(z),varargin) );
assert(valid,'Input must be string or vector');
end

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by