Reputation: 531
I need to read a text file like this (columns can be 6,9,12, etc..):
R,R,S,S,T,T
R,R,S,T,S,T
R,R,S,T,T,S
R,R,T,S,S,T
R,R,T,S,T,S
R,R,T,T,S,S
R,S,R,S,T,T
R,S,R,T,S,T
R,S,R,T,T,S
R,S,S,R,T,T
R,S,S,T,R,T
and put data in a matrix of string (without commas).
I've tried with mat=dlmread(file_name)
, but I think that it only works with numbers.
I've also tried this:
mat=fileread(filename);
mat(mat==',') = ''
but the output is strange, mat
is 1 By 72
====EDIT====
example of output (matrix 11By6):
RRSSTT
RRSTST
RRSTTS
RRTSST
RRTSTS
RRTTSS
RSRSTT
RSRTST
RSRTTS
RSSRTT
RSSTRT
Upvotes: 0
Views: 2058
Reputation: 4510
I feel like a rep whore, but here is one of the many possible solutions.
Assuming your file is named test.csv -
fileID = fopen('test.csv');
C = textscan(fileID,'%s %s %s %s %s %s' ,'Delimiter',',');
====EDIT====
if you don't want to write %s 6 times - do:
formatSpec = '%s';
N = 6;
C = textscan(fileID, repmat('%s', [1 N]),'Delimiter',',');
Again, this is just one of the many * many ways to do it.
This will generate a cell structure of 1 x 6, where each of the 6 cells represent a column of your String. You can now index it using curly brackets:
>> C{1}
>>ans =
'R'
'R'
'R'
'R'
'R'
'R'
'R'
'R'
'R'
'R'
'R'
If you want a full cell structure where there is only 1 Char in each cell, you just have to do:
V = horzcat(C{:}); %// size of V is 11x6
V =
'R' 'R' 'S' 'S' 'T' 'T'
'R' 'R' 'S' 'T' 'S' 'T'
'R' 'R' 'S' 'T' 'T' 'S'
'R' 'R' 'T' 'S' 'S' 'T'
'R' 'R' 'T' 'S' 'T' 'S'
'R' 'R' 'T' 'T' 'S' 'S'
'R' 'S' 'R' 'S' 'T' 'T'
'R' 'S' 'R' 'T' 'S' 'T'
'R' 'S' 'R' 'T' 'T' 'S'
'R' 'S' 'S' 'R' 'T' 'T'
'R' 'S' 'S' 'T' 'R' 'T'
=====SECOND EDIT=====
To convert to Char Array: Use Char
T = reshape(char(V(:)),size(V)) %// size of T is now 11*6 Char
>> T =
RRSSTT
RRSTST
RRSTTS
RRTSST
.
.
Upvotes: 2