Fill a line with tabs until specified length is reached

10 Ansichten (letzte 30 Tage)
Bananach
Bananach am 21 Jan. 2016
Kommentiert: Bananach am 21 Jan. 2016
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?

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 21 Jan. 2016
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 Kommentar
Bananach
Bananach am 21 Jan. 2016
Didn't think of taking text-string out of format-string. Wonderful.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

goerk
goerk am 21 Jan. 2016
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)

Kategorien

Mehr zu Signal Integrity Kits for Industry Standards finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by