필터 지우기
필터 지우기

How to print text on the same line ?

조회 수: 102 (최근 30일)
RuiQi
RuiQi 2016년 6월 28일
편집: Cristi Savlovschi 2021년 7월 15일
I want to print a sentence on the same line like
for i=1:100
printf('At %d', i);
end
So matlab must print
At 1
Then on the SAME LINE SAME PLACE it must print At 2. Now all it does is print
At 1
At 2
At 3
At 4
Or
At 1 At 2 At 3 At 4 At 5
I want matlab to overwrite the number.

채택된 답변

KSSV
KSSV 2016년 6월 28일
for i=1:100
fprintf('At %d', i);
pause(0.1)
clc
end
  댓글 수: 2
RuiQi
RuiQi 2016년 6월 29일
That makes my program go a lot slower considering my loop iterates thousands of times
KSSV
KSSV 2016년 6월 29일
you can change the value of pause....pause(yourvalue)..without pause, result prints very fast....

댓글을 달려면 로그인하십시오.

추가 답변 (5개)

Walter Roberson
Walter Roberson 2016년 6월 29일
lastsize = 0;
for i=1:100
fprintf(repmat('\b', 1, lastsize));
lastsize = fprintf('%At %d', i);
end
fprintf('\n');
The output will come so quickly that you will not be able to see the intermediate values unless you put in a pause()

Cristi Savlovschi
Cristi Savlovschi 2021년 7월 15일
편집: Cristi Savlovschi 2021년 7월 15일
function dispProgressMsg(msg)
ASCII_BKSP_CHAR = 8;
persistent prevMsgLen;
if isempty(prevMsgLen)
prevMsgLen = 0;
end
disp([ char(repmat(ASCII_BKSP_CHAR,1,prevMsgLen)) msg]);
prevMsgLen = numel(msg)+1;

fedi sonnara
fedi sonnara 2017년 10월 27일
By the way, just to know the principle
fprintf('testing x...');pause(3);fprintf('done\n');
fprintf('testing y...');pause(3);fprintf('done\n');
will display
testing x...done
testing y...done

r r
r r 2021년 5월 11일
I have two files with similar data and I want to extract them into a file, and I also want to print the same line for the same data in the two files
Please help me
F1 = fopen('E.txt');
T1 = textscan(F1,'%s', 'delimiter', '\n');
fclose(F1);
F2 = fopen('G.txt');
tt2 = textscan(F2,'%s', 'delimiter', '\n');
fclose(F2);
T1s = char(T1{:});
T2s = char(T2{:});
[C,ix,ic] = intersect(T1s,T2s,'rows')
%%%%%%%%%%%%%%%%%%out Fiel :::
dlmwrite('R.txt',C,'%.6f');

r r
r r 2021년 5월 11일
I have two files in which there are numbers in the first column that are similar and I want to print the line that matches and differs in the number of the first column in the two files:
%%%%%%%%%%%%%%%%%%%%%%% Fiel.1
fid1 = fopen( 'E1.txt', 'rt' );
T1 = textscan(fid1,'%s', 'delimiter', '\n');
%codes1 = textscan( fid1, '%*s%*s%*s%*s%*s%*s%s', 'Delimiter','|' );
fclose( fid1 );
%%%%%%%%%%%%%%%%%%%%%%%%%%Fiel.2
fid2 = fopen( 'G1.txt', 'rt' );
T2 = textscan(fid2,'%s', 'delimiter', '\n');
%codes2 = textscan( fid2, '%*s%*s%*s%*s%*s%*s%s', 'Delimiter','|' );
fclose( fid2 );
%%%%%%%%%%%%%%%%%%%%%%%%%%%
T1s = char(T1{:});
T2s = char(T2{:});
%Similar data between two files::
%[C,ix,ic] = intersect(T1s,T2s,'rows')
%Differences data between two files::
[B,ib,ib] = visdiff(T1s,T2s,'rows')
%%%%%%%%%%%%%%%%%%%%print output:::
fid = fopen( 'Similar.txt', 'wt' );%Print all similar lines
fprintf('%s\n',C)
fclose( fid ) ;
fid = fopen( 'Different.txt', 'wt' );%Print all different lines
fprintf('%s\n',B)
fclose( fid );

카테고리

Help CenterFile Exchange에서 Data Import and Export에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by