I have txt file with rows and columns but are separated by ";" I need to read the file and play in memory with the columns separated by ";" and lines by enter same, but I have numbers and characters can someone help me?

5 commentaires

Stephen23
Stephen23 le 27 Oct 2018
Modifié(e) : Stephen23 le 28 Oct 2018
madhan ravi
madhan ravi le 27 Oct 2018
Upload your file
Stephen23
Stephen23 le 28 Oct 2018
Thiago Medeiros's "Answer" moved here:
but this command I always have to pass the type of data I'm reading, but I never know what kind. And my intuition is to read a kind of random content table. and then I will use them one by one, this command groups some values which is not good and turns them into a table, I want a file that is already in a matrix and put in an array with rows and columns.
Stephen23
Stephen23 le 28 Oct 2018
@Thiago Medeiros: use xlsread or readtable.
Thiago Medeiros
Thiago Medeiros le 29 Oct 2018
Modifié(e) : Thiago Medeiros le 29 Oct 2018
My file (remembering that this is taken for testing machine) ref: [Link]

Connectez-vous pour commenter.

 Réponse acceptée

Stephen23
Stephen23 le 29 Oct 2018
Modifié(e) : Stephen23 le 29 Oct 2018

1 vote

Importing that file (attached) is made more complicated by the fact that is uses a decimal comma, whereas MATLAB only parses a decimal point. You can search this forum to find several approach to deal with a decimal comma. Here is one solution using strrep and textscan:
[fid,msg] = fopen('abalone.txt','rt');
assert(fid>=3,msg)
hdr = fgetl(fid);
str = fread(fid,[1,Inf],'uint8=>char');
fclose(fid);
str = strrep(str,',','.');
fmt = ['%s',repmat('%f',1,8)];
opt = {'CollectOutput',true, 'Delimiter',';'};
C = textscan(str,fmt,opt{:});
Here are the first ten rows of data, as imported into MATLAB:
>> C{1}(1:10,:)
ans =
'M'
'M'
'F'
'M'
'I'
'I'
'F'
'F'
'M'
'F'
>> C{2}(1:10,:)
ans =
0.455000 0.365000 0.095000 0.514000 0.224500 0.101000 0.150000 15.000000
0.350000 0.265000 0.090000 0.225500 0.099500 0.048500 0.070000 7.000000
0.530000 0.420000 0.135000 0.677000 0.256500 0.141500 0.210000 9.000000
0.440000 0.365000 0.125000 0.516000 0.215500 0.114000 0.155000 10.000000
0.330000 0.255000 0.080000 0.205000 0.089500 0.039500 0.055000 7.000000
0.425000 0.300000 0.095000 0.351500 0.141000 0.077500 0.120000 8.000000
0.530000 0.415000 0.150000 0.777500 0.237000 0.141500 0.330000 20.000000
0.545000 0.425000 0.125000 0.768000 0.294000 0.149500 0.260000 16.000000
0.475000 0.370000 0.125000 0.509500 0.216500 0.112500 0.165000 9.000000
0.550000 0.440000 0.150000 0.894500 0.314500 0.151000 0.320000
You can easily split the header too:
>> H = regexp(hdr,';','split');
>> H{:}
ans = Sex
ans = Length
ans = Diameter
ans = Height
ans = WholeWeight
ans = ShuckedWeight
ans = VisceraWeight
ans = ShellWeight
ans = Rings

1 commentaire

Thiago Medeiros
Thiago Medeiros le 30 Oct 2018
Thank you so much for helping me. I am eternally grateful.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

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

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by