I’m trying to save all the files in a directory as an array of strings, like this:
files = {'hello.gdf'; 'hello2.gdf'...; ... 'etc.gdf'}
Since I have many directories, I want to do this automatically. This is my code:
gdffiles = dir(fullfile('D:', 'subject', '01', '*.gdf'))
for i=1:size(gdffiles)
files(i) = gdffiles(i).name;
end
I want to assign to files the name of the gdf files found, but I get this message:
??? Subscripted assignment dimension mismatch.
Error in ==> getFiles at 3
files(i) = gdffiles(i).name;
What am I doing wrong? Thanks!
The reason for error:
You try to assign
filesin thei-th place a string (chararray)gdffiles(i).name. However, you are using an array-element assignment (round parenthesis()). Therefore, you get an error: You can only assign a singlecharusingfiles(i).Possible solutions:
You should assign to
filesusing curly braces – sincefilesis acellarray:You can achieve the same result without the loop by: