필터 지우기
필터 지우기

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