comp.lang.idl-pvwave archive
Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado

Home » Public Forums » archive » Writing to Multiple txt file with different names
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Switch to threaded view of this topic Create a new topic Submit Reply
Writing to Multiple txt file with different names [message #94590] Tue, 18 July 2017 06:12 Go to next message
thtran296 is currently offline  thtran296
Messages: 8
Registered: June 2017
Junior Member
Hello everybody,

I need to write my data to a bunch of different files, ideally with 'consecutive' names, such as File 1, File 2, File 3, .. so on (up to thousands of text files)

Here's my attempt:

get_lun, unit
for i = 1,1000 do begin
openw, unit, ‘File_’+ i + ‘.dat’, /get_lun
printf, unit, 'my data will go in here'
endfor

IDL keeps saying filename argument must be a scalar string.
Any help would be appreciated.

Thank you.
Re: Writing to Multiple txt file with different names [message #94591 is a reply to message #94590] Tue, 18 July 2017 06:40 Go to previous messageGo to next message
Jim  Pendleton is currently offline  Jim Pendleton
Messages: 165
Registered: November 2011
Senior Member
On Tuesday, July 18, 2017 at 7:12:18 AM UTC-6, thtr...@gmail.com wrote:
> Hello everybody,
>
> I need to write my data to a bunch of different files, ideally with 'consecutive' names, such as File 1, File 2, File 3, .. so on (up to thousands of text files)
>
> Here's my attempt:
>
> get_lun, unit
> for i = 1,1000 do begin
> openw, unit, ‘File_’+ i + ‘.dat’, /get_lun
> printf, unit, 'my data will go in here'
> endfor
>
> IDL keeps saying filename argument must be a scalar string.
> Any help would be appreciated.
>
> Thank you.

Try
openw, unit, ‘File_’+ i.tostring() + ‘.dat’, /get_lun

or if you're using an older version of IDL,
openw, unit, ‘File_’+ strtrim(i, 2) + ‘.dat’, /get_lun

The reason is that when the interpreter sees the "+" and a number, it will up-cast the other arguments in the expression to the highest precedence, rather than converting integers to strings.

Compare and contrast:
IDL> i = 10 & help, 'file_' + i + '.dat'
% Type conversion error: Unable to convert given STRING to Integer.
% Detected at: $MAIN$
% Type conversion error: Unable to convert given STRING to Integer.
% Detected at: $MAIN$
<Expression> INT = 10

IDL> i = 10 & help, '1' + i + '3'
<Expression> INT = 14

IDL> i = 10 & help, '1' + i + 30.0
<Expression> FLOAT = 41.0000

Jim P
Re: Writing to Multiple txt file with different names [message #94592 is a reply to message #94590] Tue, 18 July 2017 06:42 Go to previous message
Craig Markwardt is currently offline  Craig Markwardt
Messages: 1869
Registered: November 1996
Senior Member
On Tuesday, July 18, 2017 at 9:12:18 AM UTC-4, thtr...@gmail.com wrote:
> Hello everybody,
>
> I need to write my data to a bunch of different files, ideally with 'consecutive' names, such as File 1, File 2, File 3, .. so on (up to thousands of text files)
>
> Here's my attempt:
>
> get_lun, unit
> for i = 1,1000 do begin
> openw, unit, ‘File_’+ i + ‘.dat’, /get_lun
> printf, unit, 'my data will go in here'
> endfor
>
> IDL keeps saying filename argument must be a scalar string.
> Any help would be appreciated.
>

Problem 1. You need to use "straight" quotes. You are using something like Microsoft word or some notepads that use curly quotes. IDL uses straight quotes.

Problem 2. The string 'File_'+i+'.dat' won't work because you are combining an integer and string. You could use 'File_'+strtrim(i,2)+'.dat' or use STRING() with a FORMAT= argument.

Problem 3. You are using GET_LUN once at the top of your loop and again for each loop. You never close any files. Use FREE_LUN at the end of your loop.

CM
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Plot scatter points with transparency
Next Topic: Auto-path finding when writing file

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ] [ PDF ]

Current Time: Wed Oct 08 07:14:43 PDT 2025

Total time taken to generate the page: 0.00738 seconds