How to print text on the same line ?

185 ビュー (過去 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 Center および File ExchangeData Import and Export についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by