Clear Filters
Clear Filters

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

2 views (last 30 days)
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 Comments
Matt J
Matt J on 4 Dec 2022
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 on 4 Dec 2022
Edited: Jan on 4 Dec 2022
@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.

Sign in to comment.

Answers (1)

Matt J
Matt J on 4 Dec 2022
Edited: Matt J on 4 Dec 2022
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

Categories

Find more on Characters and Strings in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!