ls syntax for output variable with flags

11 visualizaciones (últimos 30 días)
DLR
DLR el 13 de Abr. de 2024
Comentada: Voss el 13 de Abr. de 2024
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 ...]

Respuesta aceptada

Voss
Voss el 13 de Abr. de 2024
Editada: Voss el 13 de Abr. de 2024
% save a bunch of mat files
x = 1;
for i = 1:26
save([char(64+i) '.mat'],'x');
end
% use multiple arguments to ls
list = ls('-1','*.mat')
list =
'A.mat B.mat C.mat D.mat E.mat F.mat G.mat H.mat I.mat J.mat K.mat L.mat M.mat N.mat O.mat P.mat Q.mat R.mat S.mat T.mat U.mat V.mat W.mat X.mat Y.mat Z.mat '
  2 comentarios
DLR
DLR el 13 de Abr. de 2024
You're kidding me -- I was going to say I had already tried that (and just edited my post to include the error this produces), but just realized that I left a space in '-1 '. This works. Thank you so much!
Voss
Voss el 13 de Abr. de 2024
You're welcome!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Environment and Settings en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2024a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by