Solved

Sprintf: Right Justify

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ejb stateless example 2 40
Cygwin - GNU GPL License 1 43
Generate Unique ID in VB.NET 21 100
Mutiple Folder view (5 Folder view) 3 48
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

740 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