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 (add matrix to text file)

Subject: fprintf (add matrix to text file)

From: Ondrej

Date: 10 Mar, 2009 12:32:01

Message: 1 of 3

Hi Guys, can you please help me with the following problem:

i am using fprintf to export one cell array {1 column} with file names and one matrix with results (7 columns). i need \n to display the matrices correctly. the second displayed matrix now appears at the bottom of the first, something like this:

file name chi alp.A. alp.A.err alp.C.
118520_1.dat
118521_1.dat
118522_1.dat
1.0240000 3.2322910 0.0000790 5.1285510
1.2520000 3.2296840 0.0000890 5.1247410
1.2390000 3.2269580 0.0000890 5.1202070

is there any way to display it on the right?

my code is here:

% write to the TXT file
% open "results.txt" file
fid = fopen('results.txt','wt');
% write in column names in the frist row
fprintf(fid, '%s \t %s \t %s \t %s \t %s \t %s \t %s \t %s\n', 'file name', ...
    ' chi ', ' alp.A.', 'alp.A.err', ' alp.C.', 'alp.C.err', ...
    ' bet.A.', 'bet.A.err')
% write in the 1st cell array
fprintf(fid, '%s\n', file_name{:});
% write in the 2nd matrix
fprintf(fid, '%9.7f \t %9.7f \t %9.7f \t %9.7f \t %9.7f \t %9.7f \t %9.7f\n', a);
fclose(fid);

Subject: fprintf (add matrix to text file)

From: Rune Allnor

Date: 10 Mar, 2009 12:47:45

Message: 2 of 3

On 10 Mar, 13:32, "Ondrej " <ondrej.muran...@ansto.gov.au> wrote:
> Hi Guys, can you please help me with the following problem:
>
> i am using fprintf to export one cell array {1 column} with file names an=
d one matrix with results (7 columns). i need \n to display the matrices co=
rrectly. the second displayed matrix now appears at the bottom of the first=
, something like this:
>
> file name =A0 =A0 =A0 =A0 =A0 =A0 =A0chi =A0 =A0 =A0 =A0alp.A. =A0 =A0 =
=A0 =A0 alp.A.err =A0 =A0 =A0 =A0alp.C. =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0=
 =A0 =A0 =A0
> 118520_1.dat
> 118521_1.dat
> 118522_1.dat
> 1.0240000 =A0 =A0 =A0 =A03.2322910 =A0 =A0 =A0 0.0000790 =A0 =A0 =A0 5.12=
85510 =A0 =A0 =A0
> 1.2520000 =A0 =A0 =A0 =A03.2296840 =A0 =A0 =A0 0.0000890 =A0 =A0 =A0 5.12=
47410 =A0 =A0 =A0
> 1.2390000 =A0 =A0 =A0 =A03.2269580 =A0 =A0 =A0 0.0000890 =A0 =A0 =A0 5.12=
02070 =A0 =A0 =A0
>
> is there any way to display it on the right?
>
> my code is here:

> % write in the 1st cell array
> fprintf(fid, '%s\n', file_name{:});
> % write in the 2nd matrix
> fprintf(fid, '%9.7f \t %9.7f \t %9.7f \t %9.7f \t %9.7f \t %9.7f \t %9.7f=
\n', a);

You need to change these lines, and write the file name and the
data at the same time:

for n=3D1:N
   fprintf(fid, '%s', file_name{n});
   fprintf(fid, '%9.7f \t %9.7f \t %9.7f \t %9.7f \t %9.7f \t %9.7f \t
%9.7f\n', a(n,:));
end

Files are Sequential Access Memory (as opposed to
Random Access Memory), so you need to write every
element to file in the same order as they will
appear in the file.

Rune

Subject: fprintf (add matrix to text file)

From: Ondrej

Date: 10 Mar, 2009 13:17:01

Message: 3 of 3

Hi Rune, thank you very much for your suggestion, it works great! really many thanks I was puzzling with this last two evenings,... Ondrej

Tags for 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