How to split the cell data?

8 visualizaciones (últimos 30 días)
Tshering Lhamo
Tshering Lhamo el 22 de Mzo. de 2016
Respondida: MHN el 23 de Mzo. de 2016
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

Respuestas (2)

Image Analyst
Image Analyst el 22 de Mzo. de 2016
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 el 23 de Mzo. de 2016
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.

Categorías

Más información sobre Data Import and Export en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by