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:
Help me please.Write some data to *.txt

Subject: Help me please.Write some data to *.txt

From: As'adul

Date: 17 Mar, 2013 05:38:10

Message: 1 of 5

I have matrix, let's say 8*40 of size and then I want for each row I can write it in a *.txt file.
So because we have 40 rows then we have 40 *.txt files with each of it has name file of, say, file1.txt,file2.txt, and so on.

How can I make it?.Please anybody?

Subject: Help me please.Write some data to *.txt

From: Nasser M. Abbasi

Date: 17 Mar, 2013 07:07:38

Message: 2 of 5

On 3/16/2013 11:38 PM, As'adul wrote:

> I have matrix, let's say 8*40 of size and then I want for each row
>I can write it in a *.txt file.
> So because we have 40 rows

You mean 40 columns? when one write size=8 by 40, then the first number
means number of rows, ot columns.

Are you sure you want 40 rows 8 rows?

>then we have 40 *.txt files with each of it has name file of, say,
>file1.txt,file2.txt, and so on.
>
> How can I make it?.Please anybody?
>


So you want to write each row to separate file?

How about make a loop? have you tried Matlab's

http://www.mathworks.com/help/matlab/import_export/ways-to-write-to-text-files.html

Take it easy at first. Try to see if you can write ONE row.

If you can do that, then you can make a function of that code
and then put the function in a loop.

--Nasser

Subject: Help me please.Write some data to *.txt

From: As'adul

Date: 17 Mar, 2013 14:39:08

Message: 3 of 5

"Nasser M. Abbasi" wrote in message <ki3mje$5rt$1@speranza.aioe.org>...

> You mean 40 columns? when one write size=8 by 40, then the first number
> means number of rows, ot columns.
>
> Are you sure you want 40 rows 8 rows?

Oh ya sorry, I mean each column not each row.
 
for 40 columns, with fprintf and the others, I have to declare the name of the *.txt file for each column manually. That's why I rather get confused here. Is there any way to do it automatically?.Anyway thank you for your respond Nasser.

As'adul

Subject: Help me please.Write some data to *.txt

From: dpb

Date: 17 Mar, 2013 15:04:12

Message: 4 of 5

On 3/17/2013 9:39 AM, As'adul wrote:
> "Nasser M. Abbasi" wrote in message <ki3mje$5rt$1@speranza.aioe.org>...
>
>> You mean 40 columns? when one write size=8 by 40, then the first number
>> means number of rows, ot columns.
>>
>> Are you sure you want 40 rows 8 rows?
>
> Oh ya sorry, I mean each column not each row.
> for 40 columns, with fprintf and the others, I have to declare the name
> of the *.txt file for each column manually. That's why I rather get
> confused here. Is there any way to do it automatically?.Anyway thank you
> for your respond Nasser.

If you really want to make multiple files of each column (or row,
doesn't really matter on the how) see the Matlab FAQ

<http://matlab.wikia.com/wiki/FAQ#How_can_I_process_a_sequence_of_files.3F>

It shows reading a series of files; you simply change the reading to
writing in the loop solution shown--the only real "trick" is the
character manipulation to create the sequential file names.

I'd ask, however, why you are wanting to do this--if it is because you
want to process the data a column at a time, this is a very
overhead-intensive way to go about it--use the array and column
addressing directly. Only if there's some outside reason at work here
would it seem to be a good idea to make all these files separately.

Oh, and BTW, when you are doing this you'll need to build the
appropriate format string -- repmat is invaluable for this to avoid
writing a clumsy mess of repeating %5.3f or the like. Something like

fmt=[repmat('5.3f',1,size(x,1)) '\n'];

for ic=1:size(x,2)
   fid=fopen(...
   fprintf(fid,fmt,x(:,ic));

may come into play...altho again I would encourage you to think if this
is really the only solution to your problem.

--

Subject: Help me please.Write some data to *.txt

From: As'adul

Date: 17 Mar, 2013 23:33:06

Message: 5 of 5

dpb <none@non.net> wrote in message <ki4m0b$m5p$1@speranza.aioe.org>...
> > If you really want to make multiple files of each column (or row,
> doesn't really matter on the how) see the Matlab FAQ
>
> <http://matlab.wikia.com/wiki/FAQ#How_can_I_process_a_sequence_of_files.3F>
>
> It shows reading a series of files; you simply change the reading to
> writing in the loop solution shown--the only real "trick" is the
> character manipulation to create the sequential file names.
>
> I'd ask, however, why you are wanting to do this--if it is because you
> want to process the data a column at a time, this is a very
> overhead-intensive way to go about it--use the array and column
> addressing directly. Only if there's some outside reason at work here
> would it seem to be a good idea to make all these files separately.
>
> Oh, and BTW, when you are doing this you'll need to build the
> appropriate format string -- repmat is invaluable for this to avoid
> writing a clumsy mess of repeating %5.3f or the like. Something like
>
> fmt=[repmat('5.3f',1,size(x,1)) '\n'];
>
> for ic=1:size(x,2)
> fid=fopen(...
> fprintf(fid,fmt,x(:,ic));
>
> may come into play...altho again I would encourage you to think if this
> is really the only solution to your problem.

Awesome,this is really what I need. Thank you

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