user836026
user836026

Reputation: 11340

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

Upvotes: 0

Views: 40

Answers (1)

Luis Mendo
Luis Mendo

Reputation: 112659

importdata does that for you:

>> x = importdata('file.txt');
x = 
    'ABCCD'
    'HGAQ'
    'VBSER'

>> whos x
  Name      Size            Bytes  Class    Attributes
  x         3x1               364  cell    

Upvotes: 2

Related Questions