Why does not string(f.name) work, f = dir(folder)?
Show older comments
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
Accepted Answer
More Answers (2)
folder = "~/Documents"
f = dir(folder) ; % f is a structure
for i = 1:length(f)
f(i).name
end
filename = fullfile(matlabroot, 'toolbox', 'matlab', 'elfun', '*.m');
D = dir(filename)
s = string({D.name});
Now you can operate on the elements of s.
s(1:4)
Categories
Find more on Call Python from MATLAB 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!