help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: newbie question: how to scrape a command line arg with argv()


From: Markus Appel
Subject: Re: newbie question: how to scrape a command line arg with argv()
Date: Sun, 26 Jan 2014 17:14:52 +0100
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.2.0

On 01/24/2014 12:20 PM, Damian Harty wrote:
>
>> octave myscript filename
>>
>> Dumb question of the day: how do I get a string out of a one element array, 
>> so that I can assign it to a variable in the script called filename, and 
>> pass it along to the next function?
> The array is actually a "cell array". You can convert it pretty easily to a 
> string:
>
>     arg_list = argv ()
>     filename = char(arg_list(1))
>
> ...then use the string as you please, eg
>
>     fid=fopen(filename)
>
> If you want to be tidier you can do it all in one, but I find this approach 
> quickly makes the code somewhat impenetrable, however clever it makes you 
> feel:
>
>     fid=fopen(  char( arg_list(1) )  )
>
>
Hi,

as the elements in the cell array returned by argv() are already
strings, you don't need to convert them again. You can simply extract
the element with curly braces:

fid=fopen(  argv(){1}  )

It might however be a good idea to check first if argv isn't empty and
throw an error:

if ( length(argv() < 1) )
  error("Not enough arguments")
endif

HTH,
Markus


reply via email to

[Prev in Thread] Current Thread [Next in Thread]