ls syntax for output variable with flags
14 views (last 30 days)
Show older comments
I am trying to use the ls command to get a variable containing a single-column list of filenames (strings, char, cell, etc--doesn't really matter), replacing an older approach that leaves behind a text file every time I use it:
unix(horzcat('ls -1 ','*.mat',' > filelist'));
filenames = textread('filelist','%s');
It seems like it should be possible to do this, based on the following from the Matlab ls documentation:
- list = ls(___) returns the names of all the files and folders in the current folder that match the specified name. You can specify list with any of the arguments in the previous syntaxes.
and Tips (on the same documentation page):
- To further modify the results of the ls command on UNIX platforms, you can add any flags that the operating system supports. For example, ls -c displays files by timestamp and ls -d displays only directories. For more information, see the UNIX ls documentation.
But there are no examples of the correct syntax to use with flags to direct results into a variable. Anyone know the right syntax here? Thanks!
Here's what I've tried so far:
If I use a flag without parentheses or specifying an output variable, it sends exactly what I want (I think) to the command window:
>> ls -1 *.mat
20240229_PRC_Flume_run1.mat
20240229_PRC_Flume_run1b.mat
20240229_PRC_Flume_run1c.mat
20240229_PRC_Flume_run2.mat
20240229_PRC_Flume_run3.mat
20240229_PRC_Flume_run4.mat
20240229_PRC_Flume_seis-hammer-test.mat
20240229_PRC_Flume_tap-test.mat
20240301_PRC_Flume_hammer-test-2.mat
20240301_PRC_Flume_run10.mat
20240301_PRC_Flume_run11.mat
20240301_PRC_Flume_run5-20k.mat
20240301_PRC_Flume_run5-40k.mat
20240301_PRC_Flume_run6.mat
20240301_PRC_Flume_run7.mat
20240301_PRC_Flume_run8.mat
20240301_PRC_Flume_run9.mat
When I use parentheses and an output variable but no flags, it returns a single char array with all filenames:
>> list = ls('*.mat')
list =
'20240229_PRC_Flume_run1.mat 20240229_PRC_Flume_run4.mat 20240301_PRC_Flume_run11.mat 20240301_PRC_Flume_run8.mat
'20240229_PRC_Flume_run1b.mat 20240229_PRC_Flume_seis-hammer-test.mat 20240301_PRC_Flume_run5-20k.mat 20240301_PRC_Flume_run9.mat
'20240229_PRC_Flume_run1c.mat 20240229_PRC_Flume_tap-test.mat 20240301_PRC_Flume_run5-40k.mat
'20240229_PRC_Flume_run2.mat 20240301_PRC_Flume_hammer-test-2.mat 20240301_PRC_Flume_run6.mat
'20240229_PRC_Flume_run3.mat 20240301_PRC_Flume_run10.mat 20240301_PRC_Flume_run7.mat
'
>> whos list
Name Size Bytes Class Attributes
list 1x523 1046 char
>> list = ls -1 *.mat
'list = ls -1 *.mat
↑
'Invalid use of operator.
>> list = ls(-1 *.mat)
'list = ls(-1 *.mat)
↑
'Invalid use of operator.
>> list = ls([-1 *.mat])
'list = ls([-1 *.mat])
↑
'Invalid use of operator.
>> list = ls(-1 *mat)
'Unrecognized function or variable ''mat''.
>> list = ls('-1' '*mat')
'list = ls(''-1'' ''*mat'')
↑
'Invalid expression. Check for missing multiplication operator, missing or unbalanced delimiters, or other syntax error. To construct matrices, use brackets instead of parentheses.
>> list = ls('-1 *mat')
'Error using ls (line 47)
'ls: invalid option --
'usage: ls [-@ABCFGHILOPRSTUWabcdefghiklmnopqrstuvwxy1%,] [--color=when] [-D format] [file ...]
>> list = ls('-1 ','*.mat')
'Error using ls (line 47)
'ls: invalid option --
'usage: ls [-@ABCFGHILOPRSTUWabcdefghiklmnopqrstuvwxy1%,] [--color=when] [-D format] [file ...]
0 Comments
Accepted Answer
More Answers (0)
See Also
Categories
Find more on Environment and Settings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!