Solved

Sprintf: Right Justify

Posted on 2003-12-06
3
892 Views
Last Modified: 2008-02-26
I am using spintf in Matlab to convert a matrix into a string to display it in an "edit text" box control.  I convert one row at a time, and then concatenate them into a matrix.  I use a horizontal tab '\t' at the end of each value converted in order that the columns stay aligned when the rows are concatenated into a matrix.  The problem is that the values are all centered, whereas I would like to see them right-justified.  I know that '-' can be used to left-justify the string, but I can't find any equivalent for right justification.  Note, I cannot add spaces to the front of a converted string (to push it to the right), as the number of characters must be identical from one row to the next in order that they can be concatenated to form a matrix.  Any thoughts?
0
Comment
Question by:samsoro
  • 2
3 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 9888474
printf("%20d\t%20d\t%20d\n",x0,x1,x2);
0
 
LVL 5

Expert Comment

by:arjanh
ID: 9888477
The normal behaviour for sprintf is to right-justify, only by using the '-' you can specify left justification. I am not sure what you mean by centered alignment.
You can also specify the column width:
sprintf( "%8s %8s", "a", "bb") will be printed as "       a       bb".

So I think what you want is something like
sprintf( "%8.2g %8.2g %8.2g", val1, val2, val3 );
This would be printed as
    1.23     12.00   123.45
  123.45    123.45     1.23
12345.67      1.23    10.00
0
 
LVL 5

Accepted Solution

by:
arjanh earned 125 total points
ID: 9888483
With a proportional font you would see that the matrix was aligned correctly....
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

A short article about a problem I had getting the GPS LocationListener working.
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now