How to display a string and matrix in MATLAB?
Show older comments
Hi all,
This question can look very basic to some experts of this forum, however the question bugs me as I am not an expert.
Say I initialize A= 3x3 unit matrix.
I want to display:
The result is: followed by matrix A.
I tried fprintf, but I guess it doesn't support matrix and I got errors.
disp() doesn't support displaying text+matrix.
Any idea how to do this?
1 Comment
Muhammad Abdullah
on 16 Mar 2025
A = eye(3); % Create a 3x3 identity matrix
fprintf('The result is:\n'); % Print a message before displaying the matrix
for i = 1:size(A,1) % Loop through each row of the matrix
fprintf('%.2f %.2f %.2f\n', A(i,:)); % Print each row with 2 decimal places
end
Accepted Answer
More Answers (3)
KAREN MEZA FLORES
on 21 Oct 2021
7 votes
bro how do you even code - thanks
JIAN CUI
on 26 Oct 2016
1 vote
Same question, sometimes I wish to print sentence like 'The sum of matrix 1...n is N', which involves a matrix [1 .. n] and variable N, as well as strings. So how am I suppose to print this one out? Thank you!
Muhammad Abdullah
on 16 Mar 2025
0 votes
A = eye(3); % Create a 3x3 identity matrix
fprintf('The result is:\n'); % Print a message before displaying the matrix
for i = 1:size(A,1) % Loop through each row of the matrix
fprintf('%.2f %.2f %.2f\n', A(i,:)); % Print each row with 2 decimal places
end
Categories
Find more on Logical in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!