0

I have a file(res.txt) that looks like this:

a
na
na
a
a
a
na

I need to read this into a matrix and import into workspace. using textscan makes it a cell array. hence, a(2)=n not na . How do I import this file into a 1D matrix?

1 Answer 1

1

Try:

fid = fopen('file.txt','rt')
C = textscan(fid, '%s', 'Delimiter',''); C = C{1};
fclose(fid);

Now each element of the cell array C{i} contains one line.

If you want an actual character matrix (padded with spaces of course), convert the cell array using:

arr = char(C);

Now each line is: arr(i,:) (might want to use deblank on that)

Sign up to request clarification or add additional context in comments.

2 Comments

What is C? a cell matrix or a character matrix?
@MaxSteel: C is a cell array of strings of size 7-by-1. arr is a character matrix of size 7-by-2 (padded with spaces since MATLAB matrices cannot be jagged)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.