How to print text on the same line ?

104 次查看(过去 30 天)
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