Solved

Sprintf: Right Justify

Posted on 2003-12-06
3
907 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

This article will show, step by step, how to integrate R code into a R Sweave document
In this post we will learn different types of Android Layout and some basics of an Android App.
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…

739 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