Use a script to pass parameters to and run another script file

View: New views
7 Messages — Rating Filter:   Alert me  

Use a script to pass parameters to and run another script file

by babelproofreader :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I have a script file that reads data from a text file, performs some operations on the data and writes the results out to another text file. This operation needs to be run many times, each time using a different data text file and writing the results out to separate "results" text files. Is there a way in which the above script file can be run from a second script file, this second script file passing the name of the required data text file to be read prior to operations, and indicating the text file name that the results should be saved as?

Re: Use a script to pass parameters to and run another script file

by Jordi GutiƩrrez Hermoso :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

2008/12/30 babelproofreader <babelproofreader@...>:

> Is there a way in which the above script file can be run from a
> second script file, this second script file passing the name of the
> required data text file to be read prior to operations, and
> indicating the text file name that the results should be saved as?

Sure, ever heard of a variable? ;-)

It's not the best programming practice, but as a quick fix, just have
script1 set some global variables that script2 is aware of, and then
have script1 call script2 many times.

Of course, the more robust practice is to turn script2 into a function
and localise the variables, and it's only marginally more work to do
so. Are you familiar with how functions work in Octave?

- Jordi G. H.
_______________________________________________
Help-octave mailing list
Help-octave@...
https://www-old.cae.wisc.edu/mailman/listinfo/help-octave

Re: Use a script to pass parameters to and run another script file

by Ben Abbott :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


On Dec 30, 2008, at 5:25 PM, babelproofreader wrote:

>
> I have a script file that reads data from a text file, performs some
> operations on the data and writes the results out to another text  
> file. This
> operation needs to be run many times, each time using a different  
> data text
> file and writing the results out to separate "results" text files.  
> Is there
> a way in which the above script file can be run from a second script  
> file,
> this second script file passing the name of the required data text  
> file to
> be read prior to operations, and indicating the text file name that  
> the
> results should be saved as?

Your first script file would be
-----------------------------
file_to_read = "data.txt"
file_to_write = "results.txt"
process_data (file_to_read, file_to_save)
-----------------------------

Your second function script would be
-----------------------------
function process_data (file_to_read, file_to_save)
.
.
.
endfunction
-----------------------------

Place your script between the function/endfunction lies and save the  
second function script with the name "process_data.m". You can use a  
different name, but be sure to change the function name (1st line) as  
well.

Is that what you'd like to do?

Ben

_______________________________________________
Help-octave mailing list
Help-octave@...
https://www-old.cae.wisc.edu/mailman/listinfo/help-octave

Re: Use a script to pass parameters to and run another script file

by babelproofreader :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Ben Abbott wrote:
Your first script file would be
-----------------------------
file_to_read = "data.txt"
file_to_write = "results.txt"
process_data (file_to_read, file_to_save)
-----------------------------

Your second function script would be
-----------------------------
function process_data (file_to_read, file_to_save)
.
.
.
endfunction
-----------------------------

Place your script between the function/endfunction lies and save the  
second function script with the name "process_data.m". You can use a  
different name, but be sure to change the function name (1st line) as  
well.

Is that what you'd like to do?

Ben

Exactly what I wanted - many thanks.
_______________________________________________
Help-octave mailing list
Help-octave@octave.org
https://www-old.cae.wisc.edu/mailman/listinfo/help-octave

Re: Use a script to pass parameters to and run another script file

by babelproofreader :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Ben, I have one small problem - I am using dlmwrite() to print out the results in my original script, but I cannot get dlmwrite() to accept a filename passed to it as a parameter when it is used within a function. Any suggestions?

Re: Use a script to pass parameters to and run another script file

by Ben Abbott :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


On Dec 31, 2008, at 11:55 AM, babelproofreader wrote:

>
> Ben, I have one small problem - I am using dlmwrite() to print out the
> results in my original script, but I cannot get dlmwrite() to accept a
> filename passed to it as a parameter when it is used within a  
> function. Any
> suggestions?

I've not used dlmwrite, but it sounds like you may be doing something  
wrong.

I suggest you create a simple example and attach it to your email.

Ben


_______________________________________________
Help-octave mailing list
Help-octave@...
https://www-old.cae.wisc.edu/mailman/listinfo/help-octave

Re: Use a script to pass parameters to and run another script file

by babelproofreader :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Ben, you were right; I was doing something wrong but I managed to figure it out and fix it. Again, thanks for your help.