Fill a line with tabs until specified length is reached

10 ビュー (過去 30 日間)
Bananach
Bananach 2016 年 1 月 21 日
コメント済み: Bananach 2016 年 1 月 21 日
In order to format my output of multiple lines, I currently use something like
fprintf('Foo Method: \t\t\t\t\t %f',a)
fprintf('Foo et al Method: \t\t\t\t %f',b)
fprintf('Foo Method with adaptively chosen parameter:\t %f',c)
to align results.
Is there something like
fprintf('Foo Method: \t[50] %f',a)
fprintf('Foo et al Method: \t[50] %f',b)
fprintf('Foo Method with adaptively chosen parameter:\t[50] %f',c)
where \t[50] jumps to the first tab stopp that occurs at or after the 50th character of the line?
Similarly/alternatively, can I fill up a line until the 50-th character without counting myself how many characters have already been written to the line?

採用された回答

Walter Roberson
Walter Roberson 2016 年 1 月 21 日
There is no standard about where tab stops go. I have seen typewriters from the 1940s and tab stops were user-configurable back then, and probably decades earlier as well.
The closest to a convention is that there was typically a tab at column 8 and another at column 72, but aside from that... some early CRTs used tabs every 8 columns.
Tabs are a nuisance, and are often converted to spaces instead. Consider using the standard UNIX expand utility.
If you need to align text then you cannot do it by column number unless you are using a fixed width font.
No, there is no mechanism in MATLAB for "jumps to the first tab stop that occurs at or after" a particular location.
I recommend that you use %s formats with a negative width count. For example,
fprintf('%-50s%f', 'Foo Method:', a);
The negative count indicates that left justification is to be used. The width indicates the minimum number of character positions will be output, blank padded.
  1 件のコメント
Bananach
Bananach 2016 年 1 月 21 日
Didn't think of taking text-string out of format-string. Wonderful.

サインインしてコメントする。

その他の回答 (1 件)

goerk
goerk 2016 年 1 月 21 日
I think an easy way is to write your own function for this.
function str = myStr(InputStr,len)
str=InputStr;
lenDiff = len - length(str);
if lenDiff < 0
warning('String too long');
else
str = [str blanks(lenDiff)];
end
end
now you can use it:
fprintf([myStr('Foo Method:',50) '%f'],a)
  1 件のコメント
Bananach
Bananach 2016 年 1 月 21 日
Just as good as the other answer, only a bit later :)

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeSignal Integrity Kits for Industry Standards についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by