Solved

Sprintf: Right Justify

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

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 …
This is about my first experience with programming Arduino.
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…

861 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