MATLAB: Read in Text File as string Array

text file

Hello, I have a text file that contains one column of words. I would like to read in the file as a string array of seperated strings so I can index the elements in the array.
Any advice is greatly appecraiated! Thanks!

Best Answer

fileName = '<your full file path here>'
% my sample text contains ---> apple, baseball, car, donut, & elephant in single column.
FID = fopen(fileName);
data = textscan(FID,'%s');
fclose(FID);
stringData = string(data{:});
Output:
stringData =
5×1 string array
"apple"
"baseball"
"car"
"donut"
"elephant"