Fill a line with tabs until specified length is reached
17 views (last 30 days)
Show older comments
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?
0 Comments
Accepted Answer
Walter Roberson
on 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.
More Answers (1)
goerk
on 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)
See Also
Categories
Find more on Language Support in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!