add string to matrix/ array in loop

I wrote this function to get the full paths of all files in a specific folder:
function [ paths ] = getPaths(folder)
%GETPATHS Get full path of files containing a given folder
filelist = dir(folder);
filenames = {filelist.name};
paths = zeros(numel(filenames),1);
for k=3:numel(filenames)
[pathstr, name, ext] = fileparts(filenames{k});
current_path = [folder pathstr name ext];
paths(k) = current_path;
end
end
I get the error message:
??? In an assignment A(I) = B, the number of elements in B and
I must be the same.
Error in ==> getPaths at 10
paths(k) = current_path;
How can I add string to matrix/ array in loop?

 채택된 답변

Oleg Komarov
Oleg Komarov 2011년 4월 15일

1 개 추천

fldr = 'C:\Users\Oleg\Desktop\';
s = dir(fldr);
strcat(fldr, {s(~[s.isdir]).name})

댓글 수: 1

D.
D. 2011년 4월 18일
this works with a '\' between path and file:
fldr = 'C:\Users\Oleg\Desktop\';
s = dir(fldr);
strcat(fldr, '\', {s(~[s.isdir]).name})
thank you!

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

추가 답변 (1개)

Andrei Bobrov
Andrei Bobrov 2011년 4월 15일

1 개 추천

can so
...
paths = cell(numel(filenames),1);
for k=3:numel(filenames)
[pathstr, name, ext] = fileparts(filenames{k});
paths(k) = {folder pathstr name ext};
end
...

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

질문:

D.
2011년 4월 15일

Community Treasure Hunt

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

Start Hunting!

Translated by