How to read alphanumer​ic(alphabe​tic+numeri​c) columns to MatLab?

I want to read data from a 'txt' file to MatLab. Please note that 3 columns of this 'txt' file consist of numerical values and the remaining column is an alphanumeric column (eg: 01f500000309, 01f50000030a ). I want to extract all those 4 columns to MatLab with their headers. I tried tblread,tblwrite but none of them worked. Any suggestions?

1 commentaire

Your comments to Per Isakson's answer indicate that it really would be simpler if you uploaded the data file, or a shortened version of it, so that we can try it for ourselves. Please make a comment here (to your question), with an upload of the data file.

Connectez-vous pour commenter.

 Réponse acceptée

per isakson
per isakson le 12 Jan 2015
Modifié(e) : per isakson le 14 Jan 2015
Try textscan
fid = fopen( 'cssm.txt' );
cac = textscan( fid, '%f%f%s' ...
, 'CollectOutput', true ...
, 'Delimiter' , ',' );
[~] = fclose( fid );
where cssm.txt contains
1, 2, 01f500000309
3, 4, 01f50000030a
&nbsp
>> cac{:}
ans =
1 2
3 4
ans =
'01f500000309'
'01f50000030a '
&nbsp
&nbsp
Addendum
fid = fopen( 'cssm.txt' );
hdr = textscan( fid, '%s%s%s%s', 1 ...
, 'CollectOutput', true ...
, 'Delimiter' , ',' );
cac = textscan( fid, '%f%f%f%s' ...
, 'CollectOutput', true ...
, 'Delimiter' , ',' );
[~] = fclose( fid );
hdr{:}
cac{:}
outputs
ans =
'Num1' 'Num2' 'Num3' 'Char1 '
ans =
17 1 2
18 3 4
ans =
'01f500000309'
'01f50000030a'
where cssm.txt contains
Num1,Num2,Num3,Char1
17, 1, 2, 01f500000309
18, 3, 4, 01f50000030a
&nbsp
Second addendum
>> importdata cssm.txt
ans =
'Num1 Num2 Char1 ABCDEFGHIJK ABCDEFGH ABCDEF ABCDEFGH '
'1 2 xxxxxxxxxxxx 192.168.20.2 UDP 60 01f600000234'
'3 4 yyyyyyyyyyyy 192.168.20.2 UDP 60 01f600000234'
The script below outputs
ans =
'ABCDEFGHIJK' 'ABCDEFGH' 'ABCDEF' 'ABCDEFGH'
ans =
'192.168.20.2' 'UDP'
'192.168.20.2' 'UDP'
ans =
60
60
ans =
'01f600000234'
'01f600000234'
>>
fid = fopen( 'cssm.txt' );
hdr = textscan( fid,'%*s%*s%*s%s%s%s%s', 1 ...
, 'CollectOutput', true );
cac = textscan( fid,'%*s%*s%*s%s%s%f%s', inf ...
, 'CollectOutput', true );
[~] = fclose( fid );
hdr{:}
cac{:}
where cssm.txt contains
Num1 Num2 Char1 ABCDEFGHIJK ABCDEFGH ABCDEF ABCDEFGH
1 2 xxxxxxxxxxxx 192.168.20.2 UDP 60 01f600000234
3 4 yyyyyyyyyyyy 192.168.20.2 UDP 60 01f600000234

6 commentaires

Thanks alot for your response. text scan worked !!! But the heading has been misaligned (meaning further to the right,3 of the column headers have gone to the very last column) Do you know how to format the header?
per isakson
per isakson le 13 Jan 2015
Modifié(e) : per isakson le 13 Jan 2015
I'm not sure I understand. However,
  • "the heading has been misaligned" How?
  • my example contains two numerical columns, not three as in your question.
  • I added another example of using textscan to my answer. Hopefully, this one is closer to your file. Header and data block must be read in two steps because they require different format strings
@ per isakson- thank you so much for your rapid response.Really appreciate your effort. I am sorry if i confuse you though.
Let me be more specific with my question. I actually want to extract 4 columns from the text file.(i just realized that i haven't tell you that i have 7 columns in total and want to extract only 4 columns from that- really sorry about that).
Actually your first code worked very well( i was able to extract all 7 columns with their headers. But headers are misaligned).
Pls see below for the output i obtained by using your 1st code: (kindly note that i manually extracted 4th,5th,6th, and 7th column which were misaligned; i just want to give you the real picture here)
'ABCDEFGHIJK ABCDEFGH ABCDEF ABCDEFGH'
'192.168.20.2 UDP 60 01f600000234'
My qu's are: (1). How to align the headers properly?
(2). suppose i want to extract 4th,5th,6th and 7th columns. Can i just say?
A=fopen('test.txt')
A(4:end)
Read the documentation for fopen. What do you think you would get from your suggested code
A=fopen('test.txt')
A(4:end)
?
According to the documentation, this function " returns an integer file identifier equal to or greater than 3... If fopen cannot open the file, then fileID is -1 ". The value A does not contain the data of the file, only a scalar value to identify which file has been opened.
per isakson
per isakson le 14 Jan 2015
Modifié(e) : per isakson le 14 Jan 2015
@Chathu, Still confused about what you have and what you want, I have added another example based on your recent comment. That should be enough for you to solve your problem with the help of the documentation on textscan. If you have the Stat Toolbox you might want to look at dataset
@Stephen- got your point. Thanks for your reply.
@per isakson- thank you so much for your responses. Really admire your continuous support. Actually, my data set is huge. I would have emailed you the data set rather than elaborating it. I really apologize for confusing you.
"textscan" (as mentioned in the 2nd addendum ) worked quite closer to my interest. Thanks a million for your guidance.

Connectez-vous pour commenter.

Plus de réponses (1)

Try readtable() if you have R2013b or later:
t = readtable(filename);

1 commentaire

Thanks for replying. Unfortunately my MatLab is little older than above said version.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Large Files and Big Data dans Centre d'aide et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by