[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: |
Damian Harty |
Subject: |
RE: newbie question: how to scrape a command line arg with argv() |
Date: |
Fri, 24 Jan 2014 11:20:38 +0000 |
> Apologies in advance for asking what is surely a shockingly basic question,
> expressed in highly inexpert terminology.
Never a need to apologise, speaking as someone who is highly inexpert.
> 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) ) )
It's also rife with opportunities for miscounted parentheses. I can't think of
anything other than an uneven number of sign errors that wastes more
programming time to track down. Your mileage may vary.
Note that in Octave, character arrays can and do exist, this can lead to quite
a lot of confusion as you find your way in. My experience is that the
documentation is very good at telling you how "construct" information - ie to
go from characters to a cell, from cells to a structure and so on, but not
necessarily good at how to "deconstruct" information - to make that journey in
reverse, as you have just discovered.
Damian
CONFIDENTIAL: The information contained in this email communication is
confidential information intended only for the use of the addressee.
Unauthorized use, disclosure or copying of this communication is strictly
prohibited and may be unlawful. If you have received this communication in
error, please notify us immediately by return email and destroy all copies of
this communication, including all attachments.