How to print text on the same line ?

193 views (last 30 days)
RuiQi
RuiQi on 28 Jun 2016
Edited: Cristi Savlovschi on 15 Jul 2021
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.

Accepted Answer

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

Sign in to comment.

More Answers (5)

Walter Roberson
Walter Roberson on 29 Jun 2016
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 on 15 Jul 2021
Edited: Cristi Savlovschi on 15 Jul 2021
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 on 27 Oct 2017
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 on 11 May 2021
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 on 11 May 2021
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 );

Categories

Find more on Data Import and Export in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!