필터 지우기
필터 지우기

Why does not string(f.name) work, f = dir(folder)?

조회 수: 4 (최근 30일)
Simon
Simon 2022년 8월 17일
댓글: Simon 2022년 8월 19일
I want to do something to, like split, the filenames in a folder. I first use dir( ) to get the contents of the folder.
folder = "~/Documents"
f = dir(folder). % f is a structure
Though f.name looks like a cell array, however,
string(f.name)
% Error using string No constructor 'string'
% with matching signature found.'
I don't understand what that means. Your answer will help me learn to code with correct understanding.
cellfun(@string, f.name)
% Error using cellfun
% Input #2 expected to be a cell array, was char instead.
Does that mean f.name send its element to @string one by one, and cellfun expect to receive the whole cell array?
On the other hand, if I enclose f.name with { },
string({f.name}) % no error message

채택된 답변

Stephen23
Stephen23 2022년 8월 17일
편집: Stephen23 2022년 8월 17일
"Why does not string(f.name) work, f = dir(folder)?"
Dot indexing into the structure f
f.name
generates a comma-separated list:
In the general case, this code
string(f.name)
will not work because it generates this comma-separated list:
string(f(1).name, f(2).name, .., f(end).name)
and STRING() is not defined for multiple input arguments like that. So clearly that syntax will not work.
"Though f.name looks like a cell array..."
It isn't a cell array, it is a comma-separated list. In contrast, this code:
{f.name}
generates one cell array from the comma-separated list, i.e. is equivalent to this:
{f(1).name, f(2).name, .., f(end).name}
and STRING() has no problem converting that cell array to a string array.
  댓글 수: 2
Simon
Simon 2022년 8월 17일
Thanks for your wonderful explanation and the links of further documentation. I quote from the help page
"Such a list, by itself, is not very useful. But when used with large and more complex data structures like MATLAB structures and cell arrays, the comma-separated list can enable you to simplify your MATLAB code."
I just did some code expriment with comma separated list. It seems to resemble what generator in Python does. After an element is processed, it is not stored in memory. That's why vectorization doesn't work with it (as in the case, string(f.name))), and for-loop works.
Stephen23
Stephen23 2022년 8월 17일
편집: Stephen23 2022년 8월 17일
"It seems to resemble what generator in Python does.
The whole point of the Python generator is that the elements do not exist in memory prior to being generated, but with the comma-separated list the elements already existed in memory and are not generated on the fly. The Python equivalent would be the asterisk operators, e.g. unpacking tuples and input arguments.
"After an element is processed, it is not stored in memory."
That rather depends on you, not on the comma-separated list syntax. You are welcome to discard the elements or store them (as my example with the cell array shows), or supply them as input arguments to any other suitable function/operator of your choice.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

KSSV
KSSV 2022년 8월 17일
편집: KSSV 2022년 8월 17일
folder = "~/Documents"
f = dir(folder) ; % f is a structure
for i = 1:length(f)
f(i).name
end
  댓글 수: 1
Simon
Simon 2022년 8월 17일
Thanks for your answer. Now I learn that vectorization does not work with comma separated list, but for-loop can be used to iterate over its elements.

댓글을 달려면 로그인하십시오.


Steven Lord
Steven Lord 2022년 8월 17일
filename = fullfile(matlabroot, 'toolbox', 'matlab', 'elfun', '*.m');
D = dir(filename)
D = 73×1 struct array with fields:
name folder date bytes isdir datenum
s = string({D.name});
Now you can operate on the elements of s.
s(1:4)
ans = 1×4 string array
"Contents.m" "abs.m" "acos.m" "acosd.m"
  댓글 수: 1
Simon
Simon 2022년 8월 19일
Lord, that is a wonderful answer. I want to add thanks for another thing. When I was using Python, I use pathlib a lot. It is a powerful, convenient package for handling folder related tasks. I had not expected Matlab to have similar functions. Here, you bring 'fullfile' to me. This makes the experience of switching from Python to Matlab very pleasant.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Call Python from MATLAB에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by