How to print in the same line

5 views (last 30 days)
satendra kumar
satendra kumar on 28 Sep 2011
I want to print a pattern on the command window as below
********
********
********
********
********
********
********
********
But Its not working as the line get changed on every execution of inner for loop.
So the question is that how to print the result on the same line in matlab command window.
Thanks
clear all
clc
m=2;
for i=1:8
if(m==2)
for i=1:8
disp('*');
end
m=1;
end
if(m==1)
disp(' ')
for i=1:8
disp('*');
end
m=1;
end
end

Answers (1)

Grzegorz Knor
Grzegorz Knor on 28 Sep 2011
Use fprintf instead of disp.
clear all
clc
m=2;
for i=1:8
if(m==2)
for i=1:8
fprintf('*');
end
fprintf('\n')
m=1;
end
if(m==1)
fprintf(' ')
for i=1:8
fprintf('*');
end
fprintf('\n')
m=2;
end
end

Categories

Find more on Loops and Conditional Statements 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!