Search code examples
octavels

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.


Solution

  • 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.