Reputation: 317
I have a multi line text file that in each line has 3 words separated by some spaces. I want to write the first word of each line into a nX1 cell array, so that:
cell{1}{1}=line1_1stword
cell{1}{2}=line2_1stword
.
.
.
How can I do that? I know that the following command reads each line into a line of the cell, but I want just the first word.
fid = fopen(`myFile.ext`)
allData = textscan(fid,'%s','Delimiter','\n');
Upvotes: 1
Views: 440
Reputation: 5133
You can use [strsplit](http://www.mathworks.in/matlabcentral/fileexchange/21710-string-toolkits/content/strings/strsplit.m"Download the file from MatlabCentral here") function.
cell=strsplit(text,' ')
Upvotes: 1
Reputation: 221504
Try this -
fid = fopen('myFile.ext')
allData = textscan(fid,'%s','Delimiter','\n')
%%// Read in the first word from each row of data
outcellarray = regexp(allData{:},'^([\w\-]+)','match')
%%// Store all the first words into a single cell array of strings
outcellarray = vertcat(outcellarray{:})
Inspired by this code.
Upvotes: 2