fprintf cell array syntax
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
Edit: Thanks, the answer I accepted shows the right syntax I needed to input the data into fprintf I am trying to use fprintf to create an input script for LAMMPS simulations and I ran into a problem when I was trying to input coordinates and orientations from matrices into the script.
for i = 1:length(crd)
fprintf(fid,{'region 1 sphere '},crd(i,:),{' '},r,{' units box\r\n'});
fprintf(fid,'delete_atoms region 1\r\n');
fprintf(fid,{'lattice fcc 3.52 orient x '},num2str(orn(i,1)),{' '},...
num2str(orn(i,2)),{' '},num2str(orn(i,3)),{' orient y '},...
num2str(orn(i,4)),{' '},num2str(orn(i,5)),{' '},num2str(orn(i,6)),...
{' orient z '},num2str(orn(i,7)),{' '},num2str(orn(i,8)),...
{' '},num2str(orn(i,9),'\r\n'));
end
This code gives an error of "Error using fprintf invalid format" The function FPRINTF is apparently called with a cell array.
The above code snippet is supposed to write in coordinates and orientations for many spheres, where crd(i,3) is a matrix of coordinates and orn(i,9) is the orientation in miller indices. Both matrices were double in structure but the crd was initialized as a character structure with num2str earlier in the script. The problem is with the way I call the cell array. I believe I am supposed to use %f and %d, I just don't know how to change my code to use them. An example of what I want printed out is this: region 1 sphere 10 10.5 20 20 units box delete_atoms region 1 lattice fcc 3.52 orient x 0 3 1 orient y 0 3 -1 orient z 1 0 0
The matrix of orientations are miller indices so they are all integers, while the matrix of coordinates will have decimals.
I hope my question was stated clearly enough. And my mistake if this has been already answered in previous questions.
Jonathan
0 Commenti
Risposta accettata
Kelly Kearney
il 20 Dic 2013
Not quite sure why you're attempting to use cell arrays here; the calling structure for fprintf expects the file id, a format string, and then all the variables to be substituted into the format string. The nice thing about fprintf is it does the string formatting for you; you don't need to convert all your data to strings like you're attempting to do.
Wasn't quite clear on your data format, but here's an example:
% Sample data
crd = rand(5,3);
r = 1;
orn = randi(10, [5 9]);
% Print
for ii = 1:length(crd)
fprintf(fid, 'region 1 sphere %f %d units box\r\n', crd(ii,1), r);
fprintf(fid, 'delete_atoms region 1\r\n');
fprintf(fid, 'lattice fcc 3.52 orient x %d %d %d %d %d %d %d %d %d\r\n', orn(ii,:));
end
0 Commenti
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Text Data Preparation in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!