Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
fprintf tabular format

Subject: fprintf tabular format

From: Jac

Date: 23 Aug, 2012 06:14:08

Message: 1 of 5

I have been set the task of calculating a range of persons electricity bill and then display these findings in a tabular format using fprintf.

I have been able to calculate the cost for each person easily enough.
I have a column vector listing persons I.D numbers
A column vector listing the amount of electricity each person used
and one with the calculated cost.

I need to display something like this:
Customer_ID Usage(KWhr) Cost
123456 980 $500
145673 620 $315.75
133321 1000 $526.35

I have tried several this but my output is either way out of alignment or has random information everywhere

Any help would be greatly appreciated thanks!!!

Subject: fprintf tabular format

From: Jac

Date: 23 Aug, 2012 09:05:05

Message: 2 of 5

Correction**

Should be presented spaced out like this:
Customer_I.D______Usage_________Cost
123456___________980___________$700
134425___________700___________$589.60
154498___________523___________$426.75

>




>





>

>

Subject: fprintf tabular format

From: Justin Ashmall

Date: 23 Aug, 2012 13:27:07

Message: 3 of 5

"Jac " <jac_domney@hotmail.com> wrote in message <k14hnf$afr$1@newscl01ah.mathworks.com>...
> I have been set the task of calculating a range of persons electricity bill and then display these findings in a tabular format using fprintf.
>
> I have been able to calculate the cost for each person easily enough.
> I have a column vector listing persons I.D numbers
> A column vector listing the amount of electricity each person used
> and one with the calculated cost.
>
> I need to display something like this:
> Customer_ID Usage(KWhr) Cost
> 123456 980 $500
> 145673 620 $315.75
> 133321 1000 $526.35
>
> I have tried several this but my output is either way out of alignment or has random information everywhere


If you don't like the Matlab documentation, try looking up an sprintf tutorial on the web. There are tonnes of them, sprintf is used by lots of languages and is the same format as fprintf.

You'll have to print the header rows (strings) and numbers separately. It'll be easiest to fprintf each row separately. Remember to put a "\n" on the end of each row to print a newline.
e.g.
>> fprintf('%10d %10d %10.2f\n', 1234, 987, 456.78)

Subject: fprintf tabular format

From: thomas.ibbotson@mathworks.co.uk

Date: 23 Aug, 2012 13:28:57

Message: 4 of 5

Jac <jac_domney@hotmail.com> wrote:
> I have been set the task of calculating a range of persons electricity bill and then display these findings in a tabular format using fprintf.
>
> I have been able to calculate the cost for each person easily enough.
> I have a column vector listing persons I.D numbers
> A column vector listing the amount of electricity each person used
> and one with the calculated cost.
>
> I need to display something like this:
> Customer_ID Usage(KWhr) Cost
> 123456 980 $500
> 145673 620 $315.75
> 133321 1000 $526.35
>
> I have tried several this but my output is either way out of alignment or has random information everywhere
>
> Any help would be greatly appreciated thanks!!!

What format string are you using with fprintf? Have you tried spacing
things out using '\t'?

Subject: fprintf tabular format

From: dpb

Date: 23 Aug, 2012 14:03:07

Message: 5 of 5

On 8/23/2012 8:27 AM, Justin Ashmall wrote:
...

>> I have been able to calculate the cost for each person easily enough.
>> I have a column vector listing persons I.D numbers
>> A column vector listing the amount of electricity each person used
>> and one with the calculated cost.
...

>> I have tried several this but my output is either way out of alignment
>> or has random information everywhere
>
>
...

> You'll have to print the header rows (strings) and numbers separately.
> It'll be easiest to fprintf each row separately. Remember to put a "\n"
> on the end of each row to print a newline.
> e.g.
>>> fprintf('%10d %10d %10.2f\n', 1234, 987, 456.78)

And, of course, remember that Matlab is column major order and recycles
the format string thru the array...so you'll want something like for the
data

fprintf('%10d %20d %$20.2f\n', [id kw cost]');

Use a small subset of the data and play at the command line to see what
(and why) wrote the output array as did...

--

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us