Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: fprintf in a loop
Replies: 2   Last Post: May 5, 2013 5:59 AM

 Messages: [ Previous | Next ]
 dpb Posts: 9,850 Registered: 6/7/07
Re: fprintf in a loop
Posted: May 4, 2013 1:24 PM

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...

--

Date Subject Author
5/4/13 Q Zhang
5/4/13 dpb
5/5/13 Bruno Luong