Write sequence text files within a loop

  • Follow


when I using the following script to create sequence files within a loop;
for k=1:15
textfilename = ['Penumbradata' num2str(k) '.txt'];
  fid = fopen(textfilename, 'rt');
  textdata = fread(fid);
  fclose(fid);
end
this give me this errors;
??? Error using ==> fread
Invalid file identifier.  Use fopen to generate a valid file identifier.

Error in ==> profilesimulationhem at 628
 textdata = fread(fid);
Could someone tell me what's that mean?/Kha
0
Reply Kha 10/13/2010 2:02:07 PM

"Kha " <khawla_h1@hotmail.com> wrote in message <i94e4u$l6h$1@fred.mathworks.com>...
> when I using the following script to create sequence files within a loop;
> for k=1:15
> textfilename = ['Penumbradata' num2str(k) '.txt'];
>   fid = fopen(textfilename, 'rt');
>   textdata = fread(fid);
>   fclose(fid);
> end
> this give me this errors;
> ??? Error using ==> fread
> Invalid file identifier.  Use fopen to generate a valid file identifier.
> 
> Error in ==> profilesimulationhem at 628
>  textdata = fread(fid);
> Could someone tell me what's that mean?/Kha

doc fread

From the Description section:

A = fread(fid) reads data in BINARY format 
from the file specified by fid into matrix A.

Note  fread is intended primarily for binary data. When
reading text files, use the fgetl function.

doc fopen

or, perhaps try using the format:

[fid, message] = fopen(filename, permission)

and see what the message is.
0
Reply someone 10/13/2010 2:41:05 PM


Is it possible to use dlmwrite within a loop ? my code is bit long, but i am stuck here .... 
loop starts 
{ 
file taken as input 
some processing done over it
results saves in a varaible "d" 
now i want to save the results of d to new text file 
display the results on matlab 
loop goes to next file until last file 
}

for a single file without loop, this works fine

      dlmwrite('test.txt',d);

now what to do within loop to save the results every time with new file name, as every time new file is processing

like

dlmwrite('file1.txt',d);
dlmwrite('file2.txt',d);
.
.
.
.
.
.
dlmwrite('lastfile.txt',d);

is possible to do above thing by using loop ?

P.S my all results are binary 

my i.d is hassaan.tariqansar@gmail.com

--
0
Reply hassaan (2) 10/5/2012 5:53:42 PM

On 10/5/2012 12:53 PM, hassaan wrote:
> Is it possible to use dlmwrite within a loop ?,,,
....
> like
>
> dlmwrite('file1.txt',d);
> dlmwrite('file2.txt',d);

See FAQ wiki

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

--
0
Reply none1568 (6655) 10/5/2012 10:45:48 PM

3 Replies
501 Views

(page loaded in 0.071 seconds)

Similiar Articles:













7/23/2012 3:05:18 PM


Reply: