I have an extra 'ans' I don't want to be shown, how do I remove it?

Hello everybody, Here is the code in question:
function [Final_Matrix,Eigenvalues, Eigenvectors] = Ass_3_Question_3b (varargin);
%%Creates a diagonal matrix with inputs N(square dimesions), d(diagonal constant), and k(off-diagonal constant)
p = inputParser; %%Stars the Parser function
Default_Dimension = 3; %%Sets the default value for the dimension of the square matrix to 3
Valid_Dimension_Input = @(x) isnumeric(x) && (x <= 10) && (x >= 2); %%Checks if the inputed value 'N' fits (2 <= N <= 10)
Default_Diagonal = 2; %%Sets the default value for the diagonal constant of the square matrix to 2
Default_Offdiagonal = 1; %%Sets the default value for the off-diagonal constant of the square matrix to 1
addParameter(p,'N', Default_Dimension, Valid_Dimension_Input); %%Adds the dimesion 'N' parameter to the parser function adn checks if N satifies the range given
addParameter(p,'d', Default_Diagonal, @isnumeric); %%Adds the dimesion 'd' parameter to the parser function
addParameter(p,'k', Default_Offdiagonal, @isnumeric); %%Adds the dimesion 'k' parameter to the parser function
parse(p,varargin{:}); %%Closes the Parser function
if ~isempty(p.UsingDefaults) %%If statement that checks if input values are not given and displays a message of which default Parser values are being used
disp('Input values are missing, Using default values for: ')
disp(p.UsingDefaults)
end
N_m = ones(p.Results.N,p.Results.N); %%A matrix of ones for the inputed dimension through Parser
base = zeros(p.Results.N,p.Results.N); %%A matrix of zeros for the inputed dimension through Parser
d_1 = (p.Results.d)*N_m; %%Functions to create the diagonal line with Parser value
d_2 = tril(d_1,0);
d_m = triu(d_2,0);
k_i = (p.Results.k)*N_m; %%Functions to create both off-diagonal lines with Parser value
k_1 = tril(k_i,-1);
k_lm = triu(k_1,-1);
k_2 = tril(k_i,1);
k_um = triu(k_2,1);
Final_Matrix = (((base + d_m) - k_lm) - k_um) %%The resulting matrix for the input values
[Eigenvalues, Eigenvectors] = eig(Final_Matrix) %%Outputs the eigenvalues and eigenvectors of the square matrix
end
When I run my code, I get, final matrix the two eigen and an 'ans'.
The question is: How do I make it not show the 'ans' but only the other three?
Thanks allot!

 Réponse acceptée

Put a semicolon after your function call,
>> Ass_3_Question_3b;

3 commentaires

Ah yes that works! But is there something I can change in the code so that when I click the 'Run' button it would do the equivalent?
Matt J
Matt J le 25 Fév 2018
Modifié(e) : Matt J le 25 Fév 2018
You could rewrite the function as below,
function varargout = Ass_3_Question_3b (varargin),
....
Final_Matrix = (((base + d_m) - k_lm) - k_um)
[Eigenvalues, Eigenvectors] = eig(Final_Matrix)
varargout={};
if nargout
varargout={Final_Matrix,Eigenvalues, Eigenvectors};
end
end
You could also customize the 'Run' button using the drop-down arrow beneath it so that it will add a semicolon to the function call.
Worked! Thanks again!

Connectez-vous pour commenter.

Plus de réponses (1)

The ans is the result of your function call without output arguments and no-semicolon
>> Ass_3_Question_3b
The fact that you see three variables is because there are no semi-colons ending the last lines of the function.
Try this, and see if you can figure out what's happening :D
[A B C] = Ass_3_Question_3b

Catégories

Community Treasure Hunt

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

Start Hunting!

Translated by