Solved

Sprintf: Right Justify

Posted on 2003-12-06
3
897 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to get all the API from website? 11 82
mapBully challenge 6 127
topping1 challenge 7 88
Tax Considerations for positive outcomes, outside the U.S. for IT Professionals 3 42
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
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 …

815 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

9 Experts available now in Live!

Get 1:1 Help Now