Thread Subject: Writing a column vector to file

Subject: Writing a column vector to file

From: Felipe Sediles

Date: 27 Sep, 2007 19:22:45

Message: 1 of 2

Hi, I'm trying to take an N-element column vector and write
it in a particular format. I want to write it as a
comma-separated, 16-column, set, but N isn't necessarily
divisible by 16. How would I got about doing this using
fprintf?

Subject: Writing a column vector to file

From: Jos

Date: 27 Sep, 2007 19:40:42

Message: 2 of 2

"Felipe Sediles" <felipe.sediles.nospam@mathworks.com>
wrote in message <fdgvu5$pc4$1@fred.mathworks.com>...
> Hi, I'm trying to take an N-element column vector and
write
> it in a particular format. I want to write it as a
> comma-separated, 16-column, set, but N isn't necessarily
> divisible by 16. How would I got about doing this using
> fprintf?

One of the many solution:

% example data
A = 1:7 ;
n = 2 ; % number of required columns

% step 1: convert to single string
  fmt = repmat('%f,',1,n) ;
  S = sprintf([fmt(1:end-1) '\n'],A) ;
% step 1+ : remove a possible trailing comma
  if rem(numel(A),n), S = S(1:end-1) ; end

% step 2: write to a file as a single string
  fid = fopen('dummy.txt','wt') ;
  fprintf(fid,'%s',S) ;
  fclose(fid) ;

% show the result
  type dummy.txt

hth
Jos

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

Tag Activity for This Thread
Tag Applied By Date/Time
fprintf Felipe Sediles 27 Sep, 2007 15:25:03
rssFeed for this Thread

Public Submission Policy

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Disclaimer prior to use.

Contact us at files@mathworks.com