Fprintf not showing up in function output

15 visualizaciones (últimos 30 días)
Gurinder Punni
Gurinder Punni el 21 de Oct. de 2020
Respondida: Namit Joshi el 24 de Ag. de 2022
Hello, I made a function that calculates the area or volume of a matrix that is linear. And, I got the math stuff down but I am not sure how to display the text so it can output a complete sentence. Here is function code:
It shows the corresponding sentence for (A) but for (B) it shows just the value of D.
function(D) = areavol(A)
R = rank(A);
[rows,col] = size(A);
D = abs(det(A));
if col == 3
word = 'parallelepiped';
else
word = 'parallelogram';
if rows > R
D = 0;
fprintf('Error,%s is not linearly independent and the area or volume is %g',word,D);
else
if col == 3
fprintf('The volume of the parallelepiped is %g cubic units',D)
else
fprintf('The area of the parallegram is %g square units',D)
end
end
end
% (A)
A = randi(10,2)
%(B)
A = fix(10*rand(3))

Respuestas (1)

Namit Joshi
Namit Joshi el 24 de Ag. de 2022
Hi Gurinder,
When we assign "A = fix(10*rand(3))", the variable "A" stores a matrix of 3X3. In the code you have provided (I have attached the code with adjusted indentation below), when the number of columns is 3 you are only assigning to variable "word" and not printing anything. Therefore you do not see any print statement.
function D = areavol(A)
R = rank(A);
[rows,col] = size(A);
D = abs(det(A));
if col == 3
word = 'parallelepiped';
else
word = 'parallelogram';
if rows > R
D = 0;
fprintf('Error,%s is not linearly independent and the area or volume is %g',word,D);
else
if col == 3
fprintf('The volume of the parallelepiped is %g cubic units',D)
else
fprintf('The area of the parallegram is %g square units',D)
end
end
end
I assume that you missed adding an "end" in your code after the first else statement.
Hope this helps!

Categorías

Más información sobre MATLAB en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by