How to print in the same line

5 Ansichten (letzte 30 Tage)
satendra kumar
satendra kumar am 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

Antworten (1)

Grzegorz Knor
Grzegorz Knor am 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

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by