How to split the cell data?
3 次查看(过去 30 天)
显示 更早的评论
NODES
1.0000000008.00000000.000000000
2.0000000007.50000000.000000000
32.0000000010.0000000.000000000
42.5000000010.0000000.000000000
52.000000008.00000000.000000000
62.500000007.50000000.000000000
7.000000000.000000000.000000000
810.0000000.000000000.000000000
910.000000010.0000000.000000000
101.000000008.00000000.000000000
I have a file which looks like this. I need to separate them into nodes, xcoord, ycoord and zcoord.I was wondering how to split it in matlab
0 个评论
回答(2 个)
Image Analyst
2016-3-22
You have a line of text with a bunch of numbers and 3 decimal places and no spaces or commas to tell where one number stops and the other starts. How would you do it? I guess what I would do is to read a line with fgetl() and then replace any string of seven zeros with a comma. Then use sscanf() or textscan() to turn that into 3 numbers. Here's one way that will work:
% For each line of the file.....
thisLine = fgetl(fid);
%thisLine = '42.5000000010.0000000.000000000'; % Test line.
fixedLine = strrep(thisLine, '.000000000', '.0')
fixedLine = strrep(fixedLine, '0000000', ', ')
xyz = sscanf(fixedLine, '%f, %f, %f')
% Scan and enter/append into our array.
x(lineCounter) = xyz(1);
y(lineCounter) = xyz(2);
z(lineCounter) = xyz(3);
See the help for fgetl() to see how to open a file and extract a line at a time from it. Of course your first line you'll just throw away since we don't need the word "NODES".
0 个评论
MHN
2016-3-23
Put your data in a text file, let say FileName.txt, then:
TXT = fileread('FileName.txt');
Split = strsplit(TXT, {'.' ' '});
Num = zeros(1,length(Split));
for i = 1:length(Split)
Num(1,i) = str2num(Split{1,i});
end
Num = reshape(Num, [4,length(Split)/4]);
Num = Num'; % your desire matrix is in Num
You can se the attachment as an example.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Large Files and Big Data 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!