Why is my script not finishing when I run from command line?

Hello,
I wrote a script to process a large data file and write a subset of data out to csv file. I run the file from command line as:
>matlab -nodisplay < script.m
When I first started running it a few weeks ago it completed in a few minutes but now it hangs and does not show it has completed when in fact it has. Can anyone tell me why? I am using an older version of matlab - R2013a (8.1.0.604) 64-bit (glnxa64).
Andre

Antworten (1)

Enclose the code in your script in a try/catch block, and at the end of your script put an explicit "quit"
Historically MATLAB did not always exit when it reached end of file on standard input, especially when it encountered an error that crashed the normal flow of control.
You are a bit safer to use
matlab -nodisplay -r "try; run('YourScript.m'); end; quit"

2 Kommentare

Below is the code I was running. Your suggestions while they should work are not for whatever reason. I should add that I am working on a remote linux cluster and the data are mounted on a drive and I am writing to another mounted drive.
% Andre Pattantyus
% May 19, 2016
% Code to process WRF downscaling output to obtain climate data.
cd /share/Volume03/wrfout/2057/12
% Finding the number of files in the directory
WRFdir = dir('wrfout*'); % Finds the needed files
num_cycles = length(WRFdir); % Turns that information into a number
CFHT_timeseries_file=zeros(num_cycles*24,9); %length of month in hrs for 9 vars
CFHT_name = 'CFHT_sfcVariables_'; % Making a Text String
%
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
%
DperR=180/pi; % degrees per radians
L=2.453e6; % latent heat of vaporization J/kg
Rv=461; % gas constant for moist air J/kg
A=2.53e8*1000; % conversion constant in Pa for Td calculation
B=5.42e3; % conversion constant in K for Td calculation
AA=6.116441;
MM=7.591386;
Tn=240.7263;
m=0;
n = 1; % The counter index
while n <= length(WRFdir) % Start of the loop, will work while n<= the number of files
name = getfield(WRFdir(n),'name'); % Get?s the name of the file
yrmn= name(12:18);
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
% GET Variables
CFHT_filename_string = strcat(CFHT_name,yrmn); % stringing Name and Year
ncid=netcdf.open(name,'NOWRITE');
Tempdata=netcdf.inqVarID(ncid,'T2');
Udata=netcdf.inqVarID(ncid,'U10');
Vdata=netcdf.inqVarID(ncid,'V10');
MRdata=netcdf.inqVarID(ncid,'Q2'); % mixing ratio at 2 meters
PSFCdata=netcdf.inqVarID(ncid,'PSFC');
SNOWdata=netcdf.inqVarID(ncid,'SNOWNC'); % accumulated grid scale snow and ice
RAIN1data=netcdf.inqVarID(ncid,'RAINC'); % accumulated cumulus precipitation
RAIN2data=netcdf.inqVarID(ncid,'RAINNC'); % accumulated grid scale precipitation
Hour=netcdf.inqVarID(ncid,'Times');
Temp=netcdf.getVar(ncid,Tempdata); % K
datahour=netcdf.getVar(ncid,Hour);
U=netcdf.getVar(ncid,Udata); % m/s
V=netcdf.getVar(ncid,Vdata); % m/s
MR=netcdf.getVar(ncid,MRdata); % kg/kg
PSFC=netcdf.getVar(ncid,PSFCdata); % Pa
SNOW=netcdf.getVar(ncid,SNOWdata); % mm
RAIN1=netcdf.getVar(ncid,RAIN1data); % mm
RAIN2=netcdf.getVar(ncid,RAIN2data); % mm
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
% retreive Lat and Lon grids. DO THIS ONCE AND SAVE ARRAYS FOR REFERENCE
LAT_ind=netcdf.inqVarID(ncid,'XLAT');
LAT_full=netcdf.getVar(ncid,LAT_ind);
LAT=LAT_full(:,:,1);
LON_ind=netcdf.inqVarID(ncid,'XLONG');
LON_full=netcdf.getVar(ncid,LON_ind);
LON=LON_full(:,:,1);
%close netcdf file
netcdf.close(ncid);
%calculate wind speed and direction from components
ws=sqrt(U.^2+V.^2); % wind speed
dir=270-atan2(V,U)*DperR; % wind direction
%calculate RH from mixing ratio, T2 and PSFC
Es=610.94*exp(17.625*(Temp-273.15)./(243.04+(Temp-273.15)))/100; % from Lawrence 2005, BAMS
E=MR.*(PSFC/100)./(0.622+MR); % vapor pressure from mixing ratio with Pa to mb correction
RH=E./Es*100;
%calculate Td from Lawrence 2005, BAMS p. 226
Td=243.04*log((E*100)/610.94)./(17.625-log(E*100/610.94))+273.15;
%calculate total rain
PREC=RAIN1+RAIN2;
% get lat and lon for CFHT = 19.82525 -155.4689
[c ind]=min(abs(LAT(1,:)-19.82525));
CFHT_lat=ind;
[c ind]=min(abs(LON(:,1)--155.4689));
CFHT_lon=ind;
CFHT_sfcTemp=squeeze(Temp(CFHT_lon,CFHT_lat,:));
CFHT_sfcPres=squeeze(PSFC(CFHT_lon,CFHT_lat,:));
CFHT_sfcRH=squeeze(RH(CFHT_lon,CFHT_lat,:));
CFHT_sfcWS=squeeze(ws(CFHT_lon,CFHT_lat,:));
CFHT_sfcDIR=squeeze(dir(CFHT_lon,CFHT_lat,:));
CFHT_sfcTd=squeeze(Td(CFHT_lon,CFHT_lat,:));
CFHT_PREC=squeeze(PREC(CFHT_lon,CFHT_lat,:)); % total precipiation liquid
CFHT_SNOW=squeeze(SNOW(CFHT_lon,CFHT_lat,:));
CFHT_sfcQ=squeeze(MR(CFHT_lon,CFHT_lat,:)); %mixing ratio
p=size(Temp,3);
if n==1
for t=1:p
CFHT_timeseries_file(m+t,1)=CFHT_sfcTemp(t);
CFHT_timeseries_file(m+t,2)=CFHT_sfcPres(t);
CFHT_timeseries_file(m+t,3)=CFHT_sfcRH(t);
CFHT_timeseries_file(m+t,4)=CFHT_sfcQ(t);
CFHT_timeseries_file(m+t,5)=CFHT_sfcTd(t);
CFHT_timeseries_file(m+t,6)=CFHT_sfcWS(t);
CFHT_timeseries_file(m+t,7)=CFHT_sfcDIR(t);
CFHT_timeseries_file(m+t,8)=CFHT_PREC(t);
CFHT_timeseries_file(m+t,9)=CFHT_SNOW(t);
end
n=n+1;
m=m+24;
elseif n==num_cycles
for t=1:p-1
CFHT_timeseries_file(m+t,1)=CFHT_sfcTemp(t);
CFHT_timeseries_file(m+t,2)=CFHT_sfcPres(t);
CFHT_timeseries_file(m+t,3)=CFHT_sfcRH(t);
CFHT_timeseries_file(m+t,4)=CFHT_sfcQ(t);
CFHT_timeseries_file(m+t,5)=CFHT_sfcTd(t);
CFHT_timeseries_file(m+t,6)=CFHT_sfcWS(t);
CFHT_timeseries_file(m+t,7)=CFHT_sfcDIR(t);
CFHT_timeseries_file(m+t,8)=CFHT_PREC(t);
CFHT_timeseries_file(m+t,9)=CFHT_SNOW(t);
end
else
for t=2:p
CFHT_timeseries_file(m+t,1)=CFHT_sfcTemp(t);
CFHT_timeseries_file(m+t,2)=CFHT_sfcPres(t);
CFHT_timeseries_file(m+t,3)=CFHT_sfcRH(t);
CFHT_timeseries_file(m+t,4)=CFHT_sfcQ(t);
CFHT_timeseries_file(m+t,5)=CFHT_sfcTd(t);
CFHT_timeseries_file(m+t,6)=CFHT_sfcWS(t);
CFHT_timeseries_file(m+t,7)=CFHT_sfcDIR(t);
CFHT_timeseries_file(m+t,8)=CFHT_PREC(t);
CFHT_timeseries_file(m+t,9)=CFHT_SNOW(t);
end
n=n+1;
m=m+24;
end
dlmwrite(['/share/Volume01/wrfout/',CFHT_filename_string,'.csv'],CFHT_timeseries_file,'delimiter',',','precision',15);
end
I suggest writing name to a log file so you can trace how far it is getting. In most programming languages I would say "and remember to flush the buffer", but in MATLAB that is automatically done unless you give special permissions when you open the file (either 'A' or 'W' instead of 'a' or 'w')

Melden Sie sich an, um zu kommentieren.

Gefragt:

am 21 Jun. 2016

Kommentiert:

am 22 Jun. 2016

Community Treasure Hunt

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

Start Hunting!

Translated by