hhh
hhh

Reputation: 52850

Bash-style Programming-quote functionality in Matlab?

I would use programming-quote like this in Bash

$ export b=`ls /`
$ echo $b
Applications Library Network System Users Volumes tmp usr var

and now I want to find similar functionality in Matlab. Also, I want to find a command that outputs relative paths, not absolute paths like Matlab's ls -- I feel like reinventing the wheel if I am parsing this with a regex. I need to find this command to debug what is wrong with namespaces here. Familiar-Bash-style functionatilities would be so cool.

Upvotes: 0

Views: 95

Answers (2)

Andrew Janke
Andrew Janke

Reputation: 23898

The Matlab equivalent of bash backticks is calling the system() function and using the second output argument. It will run an external command and capture the output.

[status,b] = system('ls /');

If it's a string of Matlab code you want to run and capture the console output of, use evalc.

But to just get a listing of files, you want the Matlab dir function. Lots easier than parsing that output string, and you get more info. See Matlab dir documentation or doc dir for more details.

children = dir('/');
childNames = { children.name };
childIsDir = [ children.isdir ];

Upvotes: 2

Pursuit
Pursuit

Reputation: 12345

For your first question, I think you can get that behavior with anonymous functions:

b = @() ls('C:\');  %I'm on windows
b()

The expression b() now returns the contents of my C drive.

Upvotes: 2

Related Questions