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

5 vues (au cours des 30 derniers jours)
carly
carly le 4 Déc 2022
Modifié(e) : Jan le 4 Déc 2022
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 commentaires
Matt J
Matt J le 4 Déc 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 le 4 Déc 2022
Modifié(e) : Jan le 4 Déc 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.

Connectez-vous pour commenter.

Réponses (1)

Matt J
Matt J le 4 Déc 2022
Modifié(e) : Matt J le 4 Déc 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

Catégories

En savoir plus sur Characters and Strings dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by