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 in a loop

Subject: fprintf in a loop

From: Skirt Zhang

Date: 4 May, 2013 14:53:09

Message: 1 of 3

I want to have the output as below
1 & $3 $ & $ 4 $ & $ 5 $ & $ 6 $ & $7 $ \\

through fprintf using specification sth like below however, it doesn't work at all....
vector=[3 4 5 6 7 ]
for w=1:length(vector)
 fprintf(fid, '%s &$', vector(w), '$');
end

can anyone help me about this a bit? thanks
       

Subject: fprintf in a loop

From: dpb

Date: 4 May, 2013 17:24:34

Message: 2 of 3

On 5/4/2013 9:53 AM, Skirt Zhang wrote:
> I want to have the output as below
> 1 & $3 $ & $ 4 $ & $ 5 $ & $ 6 $ & $7 $ \\
> through fprintf using specification sth like below however, it doesn't
> work at all....
> vector=[3 4 5 6 7 ]
> for w=1:length(vector)
> fprintf(fid, '%s &$', vector(w), '$');
> end
>
> can anyone help me about this a bit? thanks

Well, it's not symmetric so you'll have to deal w/ the first and last
separately if that's not a mistake in posting. Assuming it is,

 >> v=[3:7];
 >> fmt=[repmat(['%d $ & $ '],1,length(v)) '\n'];
 >> fprintf(fmt,v)
3 $ & $ 4 $ & $ 5 $ & $ 6 $ & $ 7 $ & $
 >>

Salt to suit...

--

Subject: fprintf in a loop

From: Bruno Luong

Date: 5 May, 2013 09:59:09

Message: 3 of 3

Repmat on format is unnecessary:

vector=[3 4 5 6 7 ]
fprintf('%d & $ ', vector);
fprintf('\\\\\n');
% It does look lika a LaTex file

% Bruno

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