How to split the cell data?

2 次查看(过去 30 天)
Tshering Lhamo
Tshering Lhamo 2016-3-22
回答: MHN 2016-3-23
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

回答(2 个)

Image Analyst
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".

MHN
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.

类别

Help CenterFile Exchange 中查找有关 Data Import and Export 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by