MATLAB Export Data

From EVOCD
Jump to: navigation, search

Contents

Abstract

The objective of this tutorial is to demonstrate how to save MATLAB internal data to a file of defined formatting. The input saved variable is assumed to be to be similar to ones which obtained from using script and data file from MATLAB Import Data tutorial. This article will show how to use MATLAB command fprintf and some others to save data to the file.

Author(s): Dmitry I. Zhuk, Mark A. Tschopp

Input

Assume that MATLAB have all the information and variables from MATLAB Import Data tutorial saved. Now the objective is to save this information using LAMMPS standard formatting for future use. This tutorial does not show now to calculate or obtain this data but only how to write existing information in a different format.

MATLAB Script

Here is one of the ways to save data to file.

fid1 = fopen(filename, 'w');
fprintf(fid1, '# Minimum Energy GB Structure for LAMMPS\n');
fprintf(fid1,'\n');
fprintf(fid1, '%d atoms\n', atoms);
fprintf(fid1, '1 atom types\n');
fprintf(fid1, '%f %f xlo xhi\n', xlo, xhi);
fprintf(fid1, '%f %f ylo yhi\n', ylo, yhi);
fprintf(fid1, '%f %f zlo zhi\n', zlo, zhi);
fprintf(fid1,'\n');
fprintf(fid1,'Atoms');
fprintf(fid1,'\n');
fprintf(fid1, '%d 1 %f %f %f\n', [double(atomID), Xpos, Ypos, Zpos]');
fclose(fid1);

Fopen command simply opens the file for editing with fileID "fid1". "w" here means to open file with permission to write. Fprintf command adds information to the opened file. Text to write goes in apostrophes. "\n" means to start with the next line. There are some other escape characters which are described in MATLAB help.

Variable can be also printed inside the text. Adding number format sign at the place where the actual number should go will do this. For example.

fprintf(fid, 'The wave traveled %d meters at the speed of %f meters per second', 100, speedofsound);

If the variable speedofsound is equal, for instance, to 340.29, then the result would be:

The wave traveled 100 meters at the speed of 340.29 meters per second

Note, that 100 here is not a variable but just a number.

Brackets in last fprintf function is needed to print only one of the variables on the line and then the other. Without them, the MATLAB would just print all the variables one after another, four on line. "double(atomID)" changes atomID type to double floating-point.

Resulting file

After running this script the result should be simular to this

# Minimum Energy GB Structure for LAMMPS

5828 atoms
1 atom types
0.000000 109.424979 xlo xhi
0.000000 218.533081 ylo zhi
0.000000 4.050000 ylo zhi

Atoms
1 1 53.069549 5.989693 2.024924
2 1 52.569822 2.273330 2.024912
3 1 1.577682 42.407913 4.050134
4 1 3.599188 42.331114 2.025135
5 1 1.432760 38.355783 4.050126
6 1 3.525962 40.304560 4.050129
7 1 1.504881 40.382048 2.025129
8 1 3.453345 38.277394 2.025125
9 1 1.291089 34.301725 4.050119
10 1 3.381448 36.249498 4.050122
11 1 1.361451 36.329039 2.025123
...


Next Tutorial

Go Back

Personal tools
Namespaces

Variants
Actions
home
Materials
Material Models
Design
Resources
Projects
Education
Toolbox