How to display unique error message in function code?
    3 visualizaciones (últimos 30 días)
  
       Mostrar comentarios más antiguos
    
    Anne Nguyen
 el 4 de Oct. de 2019
  
    
    
    
    
    Respondida: Daniel M
      
 el 5 de Oct. de 2019
            Here is my code:
function returnVec = repvec(vec, s)
    returnVec = [];
    i = 1;
    for x = vec
        for y = 1:s
            returnVec(i) = x;
            i = i+1;
        end
    end
end
For example, in the command window when I put in repvec([1,2;3,4],2), I get an error message. This is what I wanted to happen and is supposed to occur. However, how do I create my own unique error message such as "The first input argument is invalid!" if any input arguments are incorrect? (This is so that others who use my code can know why they received an error.) I have to use the error function.
1 comentario
  dpb
      
      
 el 5 de Oct. de 2019
				What specific is the problem with following an example as shown in the documentation for the error() function?  It has examples for the exact purpose.
Respuesta aceptada
  Daniel M
      
 el 5 de Oct. de 2019
        You need to manually check the input arguments yourself. For example:
function returnVec = repvec(vec, s)
  if ~isvector(vec)
    error('vec must be a vector.')
  end
    returnVec = [];
    i = 1;
    for x = vec
        for y = 1:s
            returnVec(i) = x;
            i = i+1;
        end
    end
end
0 comentarios
Más respuestas (0)
Ver también
Categorías
				Más información sobre Special Functions en Help Center y File Exchange.
			
	Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!