Azzera filtri
Azzera filtri

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

2 visualizzazioni (ultimi 30 giorni)
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 Commenti
Matt J
Matt J il 4 Dic 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 il 4 Dic 2022
Modificato: Jan il 4 Dic 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.

Accedi per commentare.

Risposte (1)

Matt J
Matt J il 4 Dic 2022
Modificato: Matt J il 4 Dic 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

Categorie

Scopri di più su Characters and Strings in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by