MATLAB: Ho to separate multiple files into individual columns

columnsconcatdata importimportknntxt

I have a folder containing 160 .txt files and need to pull them into a matlab matrix and then into vectors. Each file contains a review string which has already been converted into single words per row(e.g. Is this a sentence? becomes
'Is' 'this' 'a' 'sentence' 'QUESTION_MARK')
What I am trying to do is pull the documents into Matlab and then each word for the file will shown in rows and then each document carried over to the next column. The code I have does pull in the documents and every line of text for all 160 files from the selected folder but it lists al the data down one column where I need to have one file to a column. I know there is a way to do this but I cannot seem to find a solution that works with my code.
if true
fpath = ('C:\Users\Willem\Documents\MATLAB\fold1');
files = dir(fullfile(fpath,'*.oneline'));
nfiles = length(files);
data = [];
for k = 1:nfiles
data = [data; importdata(fullfile(fpath,files(k).name))];
end
end
Any and all help will be very much appreciated.

Best Answer

MATLAB arrays cannot have varying number of rows per column. The closest you can get would be to use empty elements as placeholders.
data = {};
for k = 1 : nfiles
thisdata = importdata(fullfile(fpath,files(k).name);
nrow = length(thisdata);
if size(data,1) < nrow
data(nrow,1) = {}; %extend number of rows if needed
end
data(1:nrow,end+1) = thisdata(:); %add on the new info
end
Related Question