How to print in the same line

조회 수: 6 (최근 30일)
satendra kumar
satendra kumar 2011년 9월 28일
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

답변 (1개)

Grzegorz Knor
Grzegorz Knor 2011년 9월 28일
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

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by