How can I open an xyz format file in MATLAB?
13 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Samaneh
el 11 de Jul. de 2014
Comentada: Image Analyst
el 5 de Mayo de 2021
Hello all, I have a xyz format file. I got it from my simulation program by LAMMPS. In this file I have some statements that are not useful for me but I can't delete them. My useful data in each timestep are the numbers that come after "ITEM: ATOMS id type z" , other statements aren't important. I have lots of data in each timestep in my program. My file contains lots of timesteps that come after each other continuously . For more detail I put 2 timesteps of my data here (timestep 0; timestep 10000):
ITEM: TIMESTEP
0
ITEM: NUMBER OF ATOMS
8553
ITEM: BOX BOUNDS pp pp pp
-21.97 21.963
-22.261 22.261
-121.7 68.7
ITEM: ATOMS id type z
1912 5 3.499
1913 4 4.234
8149 5 4.2
8150 4 3.56
8151 4 4.415
143 4 4.754
346 5 5.053
ITEM: TIMESTEP
10000
ITEM: NUMBER OF ATOMS
8553
ITEM: BOX BOUNDS pp pp pp
-21.97 21.963
-22.261 22.261
-121.7 68.7
ITEM: ATOMS id type z
2894 4 -107.919
2893 5 -107.733
2895 4 -108.452
1001 4 -112.385
1000 5 -111.436
1002 4 -110.96
3963 4 -101.516
I want to know how can I open and read this file by MATLAB? I want to read the numbers in each timestep and then apply some functions, also the same for other steps till end. How can I do this? Would you please guide me? Many thanks
2 comentarios
dpb
el 11 de Jul. de 2014
Do you know how many items are in the section of interest? Is it given by the value after the line
ITEM: NUMBER OF ATOMS
8553 here? If so, that'll make it simpler.
Also, I presume the extra linefeed is a fignewton of the posting and not included in the file?
Respuesta aceptada
Image Analyst
el 11 de Jul. de 2014
Ask the publisher of your simulation program if they have a MATLAB reader for their data files. They very well may have a reader, and if so, that is by far the easiest way to go.
6 comentarios
hamza ashar
el 5 de Mayo de 2021
Editada: hamza ashar
el 5 de Mayo de 2021
please will you help me to code this First part i have coded sucessfully but second part for repetiotion code i am not able to do it. Plz help me to do it i am in urgent of today.
Image transmission over Binary Channel.
We wish to study how a degraded image looks through binary channels. Starts with a binary image (black and white image) of your choice.
I. Assume that this image is transmitted through a BSC with p=0.01
Use the repetition code(n=3) for error correction and then show the improvement in the above case.
i have asked question here also but no help got
https://in.mathworks.com/matlabcentral/answers/819695-image-transmission-over-binary-channel?s_tid=srchtitle
x=imread("cameraman.tif");
disp(size(x))
x=im2double(x);
T=dctmtx(8);
dct= @(block_struct)T*block_struct.data*T';
B=blockproc(x,[8 8],dct);
mask= [1 1 1 1 0 0 0 0
1 1 1 0 0 0 0 0
1 1 0 0 0 0 0 0
1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0];
B2=blockproc(B,[8 8],@(block_struct)mask.*block_struct.data);
invdct= @(block_struct)T' *block_struct.data*T;
I2=blockproc(B2,[8 8],invdct);
figure(1)
imshow(I2)
% BW = im2bw(x,0.4);
BW = imbinarize(x);
figure(2)
imshow(BW);
ndata = bsc(BW,0.01);
figure(3)
imshow(ndata);
ndata=im2double(ndata);
BW=im2double(BW);
peaksnr = psnr(ndata,BW);
disp(peaksnr);
out=repmat(ndata,3,1);
figure (4)
imshow(out)
Image Analyst
el 5 de Mayo de 2021
Más respuestas (1)
Image Analyst
el 5 de Mayo de 2021
Try this. It took me about half an hour to write a reader for this type of file.
It works for the data you mentioned (attached, because you forgot to attach it).
% Demo by Image Analyst.
clc; % Clear the command window.
fprintf('Beginning to run %s.m ...\n', mfilename);
close all; % Close all figures (except those of imtool.)
clearvars;
workspace; % Make sure the workspace panel is showing.
format short g;
format compact;
fontSize = 16;
% One record will look like this:
% ITEM: TIMESTEP
% 0
% ITEM: NUMBER OF ATOMS
% 8553
% ITEM: BOX BOUNDS pp pp pp
% -21.97 21.963
% -22.261 22.261
% -121.7 68.7
% ITEM: ATOMS id type z
% 1912 5 3.499
% 1913 4 4.234
% 8149 5 4.2
% 8150 4 3.56
% 8151 4 4.415
% 143 4 4.754
% 346 5 5.053
records.TimeStep = 0;
records.NumberOfAtoms = 0;
records.BoxBounds = zeros(3, 2);
records.Atoms = zeros(7, 3);
fullFileName = fullfile(pwd, 'LAMMPS.XYZ');
% Open the file for reading in text mode.
fileID = fopen(fullFileName, 'rt');
recordNumber = 1;
textLine = 'Start';
while ischar(textLine)
% Get the time step.
% Read the next line, if we haven't already.
if ~contains(textLine, 'ITEM')
textLine = fgetl(fileID); % Should be "ITEM: TIMESTEP"
end
if textLine == -1
break; % End of file
end
% Get the next line which should be a number.
textLine = fgetl(fileID); % Should be a number
records(recordNumber).TimeStep = str2double(textLine);
% Get the number of atoms.
% Read the next line.
textLine = fgetl(fileID); % ITEM: NUMBER OF ATOMS
textLine = fgetl(fileID); % Should be a number
% Get the next line which should be a number.
records(recordNumber).NumberOfAtoms = str2double(textLine);
numberOfAtoms = records(recordNumber).NumberOfAtoms;
% Get the box bounds.
% Read the next line.
textLine = fgetl(fileID); % BOX BOUNDS pp pp pp
bb = zeros(3, 2);
for k = 1 : size(bb, 1) % Read 3 rows
textLine = fgetl(fileID); % Should be two numbers.
if textLine == -1
% Did not have all the lines. Started with the next record early for some reason.
bb = bb(1 : k-1, :); % Crop matrix so that it's shorter.
% Break if it's the end of the file.
break;
end
if contains(textLine, 'ITEM', 'IgnoreCase', true)
% Did not have all the lines. Started with the next record early for some reason.
bb = bb(1 : k-1, :); % Crop matrix so that it's shorter.
break;
I end
twoNumbers = sscanf(textLine, '%f %f')';
bb(k, :) = twoNumbers;
end
% Get the next line which should be a number.
records(recordNumber).BoxBounds = bb;
% Get the Atom numbers.
% Read the next line, if we haven't already.
if ~contains(textLine, 'ITEM')
textLine = fgetl(fileID); % ATOMS id type z
end
atomsMatrix = zeros(numberOfAtoms, 3);
for k = 1 : numberOfAtoms % Read all rows
textLine = fgetl(fileID); % Should be three numbers.
if textLine == -1
% Did not have all the lines. Started with the next record early for some reason.
atomsMatrix = atomsMatrix(1 : k-1, :); % Crop matrix so that it's shorter.
% Break if it's the end of the file.
break;
end
if contains(textLine, 'ITEM', 'IgnoreCase', true)
% Did not have all the lines. Started with the next record early for some reason.
atomsMatrix = atomsMatrix(1 : k-1, :); % Crop matrix so that it's shorter.
break;
end
threeNumbers = sscanf(textLine, '%f %f %f')';
atomsMatrix(k, :) = threeNumbers;
end
% Get the next line which should be a number.
records(recordNumber).Atoms = atomsMatrix;
recordNumber = recordNumber + 1;
end
% All done reading all lines, so close the file.
fclose(fileID);
% Display in command window.
records
fprintf('Done running %s.m\n', mfilename);
0 comentarios
Ver también
Categorías
Más información sobre Image Data 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!