How to print text on the same line ?

195 visualizzazioni (ultimi 30 giorni)
RuiQi il 28 Giu 2016
I want to print a sentence on the same line like
for i=1:100
printf('At %d', i);
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
At 1 At 2 At 3 At 4 At 5
I want matlab to overwrite the number.

Risposta accettata

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

Accedi per commentare.

Più risposte (5)

Walter Roberson
Walter Roberson il 29 Giu 2016
lastsize = 0;
for i=1:100
fprintf(repmat('\b', 1, lastsize));
lastsize = fprintf('%At %d', i);
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 il 15 Lug 2021
Modificato: Cristi Savlovschi il 15 Lug 2021
function dispProgressMsg(msg)
persistent prevMsgLen;
if isempty(prevMsgLen)
prevMsgLen = 0;
disp([ char(repmat(ASCII_BKSP_CHAR,1,prevMsgLen)) msg]);
prevMsgLen = numel(msg)+1;

fedi sonnara
fedi sonnara il 27 Ott 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 il 11 Mag 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');
F2 = fopen('G.txt');
tt2 = textscan(F2,'%s', 'delimiter', '\n');
T1s = char(T1{:});
T2s = char(T2{:});
[C,ix,ic] = intersect(T1s,T2s,'rows')
%%%%%%%%%%%%%%%%%%out Fiel :::

r r
r r il 11 Mag 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 );
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
fclose( fid ) ;
fid = fopen( 'Different.txt', 'wt' );%Print all different lines
fclose( fid );


Scopri di più su Data Import and Export in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by