Fill a line with tabs until specified length is reached

10 次查看(过去 30 天)
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 个)

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)

类别

Help CenterFile Exchange 中查找有关 Signal 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