Filter löschen
Filter löschen

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

2 Ansichten (letzte 30 Tage)
carly
carly am 4 Dez. 2022
Bearbeitet: Jan am 4 Dez. 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 Kommentare
Matt J
Matt J am 4 Dez. 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 am 4 Dez. 2022
Bearbeitet: Jan am 4 Dez. 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.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Matt J
Matt J am 4 Dez. 2022
Bearbeitet: Matt J am 4 Dez. 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

Kategorien

Mehr zu Characters and Strings finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by