LeahNH
LeahNH

Reputation: 535

In Octave, how can I call ls with options?

I'm trying to get a list of filenames to iterate through in my Octave program. I currently call

x = ls

and that works fine. However, I want a list of only text files. I can call

ls *.txt

But I can't call

x = ls *.txt

Is there any way to do that? The workaround I've found is

x = eval("ls *.txt")

but I'm hoping to avoid that.

Upvotes: 0

Views: 867

Answers (2)

carandraug
carandraug

Reputation: 13091

On top of Andy's answer which explains why you are calling ls wrong, you are also wrong in calling ls to begin with. This function returns a char array with the list of files which is mostly useless for anything other than display at the Octave prompt.

Instead, consider use glob:

files = glob ("*.txt")

which will return a cell array of filenames.

Upvotes: 2

Andy
Andy

Reputation: 8091

You should read the manual: http://www.gnu.org/software/octave/doc/interpreter/Calling-Functions.html#Calling-Functions

x = ls ("*.txt")

If you call a function without (), all arguments are interpreted as strings but this only works if you don't want to store the result. So

foo bar baz

is equivalent to

foo ("bar", "baz")

Upvotes: 2

Related Questions