Search code examples
arraysstringmatlabfile-iocell-array

Reading file data into cell array of character strings


I'm new to matlab. I have files conatins list of string like:

ABCCD
HGAQ
VBSER

I need to read it into cell array of character strings.

I tried this code:

fid2 = fopen('C:\matlab\data\myfile.txt');
tline = fgetl(fid2);

while ischar(tline)
    disp(tline)
    tline = fgetl(fid2);
end

fclose(fid2);

However, I didn't know how to convert the output to cell array of character strings


Solution

  • importdata does that for you:

    >> x = importdata('file.txt');
    x = 
        'ABCCD'
        'HGAQ'
        'VBSER'
    
    >> whos x
      Name      Size            Bytes  Class    Attributes
      x         3x1               364  cell