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

Stephen23
Stephen23 2018년 10월 27일
편집: Stephen23 2018년 10월 28일
madhan ravi
madhan ravi 2018년 10월 27일
Upload your file
Stephen23
Stephen23 2018년 10월 28일
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 2018년 10월 28일
@Thiago Medeiros: use xlsread or readtable.
Thiago Medeiros
Thiago Medeiros 2018년 10월 29일
편집: Thiago Medeiros 2018년 10월 29일
My file (remembering that this is taken for testing machine) ref: [Link]

댓글을 달려면 로그인하십시오.

 채택된 답변

Stephen23
Stephen23 2018년 10월 29일
편집: Stephen23 2018년 10월 29일

1 개 추천

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

Thiago Medeiros
Thiago Medeiros 2018년 10월 30일
Thank you so much for helping me. I am eternally grateful.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Large Files and Big Data에 대해 자세히 알아보기

제품

질문:

2018년 10월 27일

댓글:

2018년 10월 30일

Community Treasure Hunt

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

Start Hunting!

Translated by