Is there a way to sort text file in matlab?
2 次查看(过去 30 天)
显示 更早的评论
I have a question if there is possibility to sort through text file in matlab. I'll post 3 pictures where it says all. I have a txt file and need to make nx3 matrix from it.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/191677/image.jpeg)
-
This is first part where I have to delete first 11 rows in txt file.
-
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/191678/image.jpeg)
-
Second part where I have 3 columns of numbers I need to write in matrix
-
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/191679/image.jpeg)
-
Third part where I need to delete everything down from information I need to sort.
2 个评论
Stephen23
2018-7-5
"Is there a way to sort text file in matlab?"
Yes.
Please upload a sample file. We cannot test code using screenshots of a text file.
采纳的回答
Stephen23
2018-7-5
>> str = fileread('kvadrat2.txt');
>> tkn = regexp(str,'^GRID\*\s+\d+\s+(\d+,\d+)\s+(\d+,\d+)\s+\d+\s+\*\s+\d+\s+(\d+,\d+)','lineanchors','tokens');
>> tkn = vertcat(tkn{:});
>> tkn = strrep(tkn,',','.');
>> mat = str2double(tkn)
mat =
0 50.0000 0
0 50.0000 50.0000
0 0 0
0 0 50.0000
0 5.0000 0
0 10.0000 0
0 15.0000 0
0 20.0000 0
0 25.0000 0
... lots of lines here
0 20.0000 30.0000
0 25.0000 30.0000
0 30.0000 30.0000
0 30.0000 25.0000
0 30.0000 20.0000
0 25.0000 20.0000
0 25.0000 25.0000
To save the matrix in a new file use |csvwrite:
csvwrite('newfile.csv',mat)
If you really need to use a comma as the decimal radix, search this forum for "comma decimal".
5 个评论
Stephen23
2018-7-5
@Matija Kosak: I hope that it helps. Remember to accept the answer that helped you most to resolve your question. Accepting answers is an easy way to show your appreciation of the volunteers who help you on this forum.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Text Files 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!