Run TRIM in bath mode, but get Run-time error '62' \n Input past end of file
10 次查看(过去 30 天)
显示 更早的评论
Hello, everyone! I am writing a simple code to run TRIM in batch mode to calculate the average proton energy exiting from a target material. I developed the following script where I change the inlet proton energy in the file TRIM.IN, but once I run TRIM.exe it give me error '62', do you know how to solve this problem?
function [] = SRIM_automatic(energy_next,energy_prev)
% Enter units: energy [MeV], step [MeV]
energy_next = energy_next * 10^3; %[keV] energy to insert
energy_prev = energy_prev * 10^3; %[keV] energy to substitute
%system('start /D C:\Users\renald_e\Desktop\SRIM TRIM.exe');
fid = fopen('H:\folder...\TRIM.IN','r');
i = 1;
while ~feof(fid)
temp_line = fgetl(fid);
trim_file{i} = temp_line;
i = i + 1;
end
fclose(fid);
% Edit the initial proton energy: third element on third line
str_loc = strfind(trim_file{3},num2str(energy_prev));
str_length = length(num2str(energy_next));
% Substitute the proton energy
switch str_length
case 4
trim_file{3}(str_loc:str_loc+str_length-1) = sprintf('%c%s',' ',num2str(energy_next));
case 5
trim_file{3}(str_loc:str_loc+str_length-1) = sprintf('%s',num2str(energy_next));
end
fid = fopen('H:\folder...\TRIM.IN','w');
for i=1:numel(trim_file)
if i == numel(trim_file)
fprintf(fid,'%s',trim_file{i});
break
else
fprintf(fid,'%s\n',trim_file{i});
end
end
fclose(fid);
end
5 个评论
dpb
2021-11-26
编辑:dpb
2021-11-27
Glad to help...coaching/teaching is something I try to do as well as just answer a specific Q? You note I never did address your initial problem of why your code ran in to the EOF() issue but steered you in another direction entirely. :)
As for books, I'm sorry, no...I don't know of any that are either still in print or of a level for novice; I'm an old geezer and all my reference material other than the current documentation is at least 40 years old or, if more recent, very specialized stuff I bought while still in the active consulting gig -- but it's approaching 20 year now since I gave that up, too.
I know there is a link at the MathWorks site on MATLAB books, probably start there...
回答(2 个)
dpb
2021-11-25
编辑:dpb
2021-11-25
The above looks like could be much simpler if it is just doing a string substitution on a given line...try something like
function [] = SRIM_automatic(energy_next,energy_prev)
% Enter units: energy [MeV], step [MeV]
energy_next = energy_next * 10^3; %[keV] energy to insert
energy_prev = energy_prev * 10^3; %[keV] energy to substitute
%system('start /D C:\Users\renald_e\Desktop\SRIM TRIM.exe');
trim_file=textread('H:\folder...\TRIM.IN','%s','whitespace','','delimiter',newline);
% Edit the initial proton energy: third element on third line
trim_file(3)=strrep(num2str(energy_prev),num2str(energy_next));
% Write the new file
writecell('H:\folder...\TRIM.IN',trim_file,'FileType','text','QuoteStrings',0);
end
NB: Air code, not tested -- as does yours, this code overwrites the original file, be SURE to have backups before testing.
NB2: Above replaces the prev string with next string; it does not worry about the spacing. If it is critical, then
newstr=num2str(energy_next); % get the new conversion as char
lnew=length(newstr); % and it's length
newstr=strcat({blanks(4-lnew)}, newstr); % pad new string to four if needed
3 个评论
dpb
2021-11-25
Here's a convenient place for venerable textread.
You've not answered the Q? though, of whether the fixed width fields are significant or not -- if they are, your code isn't compensating for it as is...you test for the length of the new number, but not for the length of the text it is replacing.
I modified the above a little...can deal with the length Q? if know what is needed.
dpb
2021-11-25
编辑:dpb
2021-11-25
Illustration the above (modified) code logic works as intended -- I downloaded your file, changed the 13000 to 12980 and read the modified file back in to check...all in three lines of code...
txt=textread('trim.txt','%s','whitespace','','delimiter',newline); % read as cellstr array
txt(3)=strrep(txt(3),'13000','12980'); % make a substitution
writecell(txt,'trim.txt','FileType','text','QuoteStrings',0) % write the modified file
Let's see what we got...type the file back out at the command line...
>> type trim.txt
==> SRIM-2013.00 This file controls TRIM Calculations.
Ion: Z1 , M1, Energy (keV), Angle,Number,Bragg Corr,AutoSave Number.
1 1.008 12980 0 400 .94 400
Cascades(1=No;2=Full;3=Sputt;4-5=Ions;6-7=Neutrons), Random Number Seed, Reminders
1 0 0
Diskfiles (0=no,1=yes): Ranges, Backscatt, Transmit, Sputtered, Collisions(1=Ion;2=Ion+Recoils), Special EXYZ.txt file
0 0 1 0 0 0
Target material : Number of Elements & Layers
"H (72000) into Target " 2 1
PlotType (0-5); Plot Depths: Xmin, Xmax(Ang.) [=0 0 for Viewing Full Target]
5 0 3000000
Target Elements: Z Mass(amu)
Atom 1 = Er = 68 167.26
Atom 2 = O = 8 15.999
Layer Layer Name / Width Density Er(68) O(8)
Numb. Description (Ang) (g/cm3) Stoich Stoich
1 "Target" 3000000.11920929 4.72 .4 .6
0 Target layer phases (0=Solid, 1=Gas)
0
Target Compound Corrections (Bragg)
1
Individual target atom displacement energies (eV)
25 28
Individual target atom lattice binding energies (eV)
3 3
Individual target atom surface binding energies (eV)
3.05 2
Stopping Power Version (1=2011, 0=2011)
0
>>
Looks like the original excepting for line 3...the 12980 shows up as expected.
That was a 4-->4 on purpose, as noted, the Q? is whether must keep the alignment or not.
Joel Fischer
2025-1-27
I'm a bit late but it seems this question is still unresolved. So just in case anybody runs into this issue again:
I ran into this problem recently when trying to run my personal SRIM automation script on linux. The error seems to originate from VB: https://learn.microsoft.com/en-us/office/vba/Language/Reference/user-interface-help/input-past-end-of-file-error-62
In my case it turned out that the issue was that my input files had unix line endings (nl) instead of windows line endings (crnl). If any of the input files (TRIM.IN, TRIMAUTO) contain a solitary newline character ('\n'), trim crashes with error 62. If you don't have a text editor that can display '\r' and '\n', you can check the line endings using fgets in Matlab.
You're writing the lines using
fprintf(fid,'%s\n',trim_file{i});
so I'd guess that the newline character might also be the source of your problem. You can likely fix the issue by using
fprintf(fid,'%s\r\n',trim_file{i});
instead.
What in my case happened was that I had opened the input files in a text editor (Kate) which automaticaly converted the line endings. I could see how this could be a common problem for people running TRIM using Wine on linux.
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
