Attempted to access data(1); index out of bounds because numel(data)=0. Error in RunAirfoilCase (line 62) alpha = data(1) ;

1 vue (au cours des 30 derniers jours)
i dont know how to fix it, i would appreciate your help.
function [alpha,cl,cd,cdp,cm,xtr1,xtr2] = RunAirfoilCase( p , counter , camb , xcamb , thickness )
%
Reynolds = 1e6 ;
Mach = 0.05 ;
%
FileName = sprintf( 'NACA%04d.dat' , counter ) ;
fid = fopen( FileName , 'wt' ) ;
fprintf( fid , 'NACA %10.6f %10.6f %10.6f\n' , camb , xcamb , thickness);
for i = 1 : length(p)
fprintf( fid , ' %10.6f %10.6f\n' , p(i,1) , p(i,2) ) ;
end
fclose(fid);
Filename = 'xfoil.inp' ;
fid = fopen( Filename , 'wt' );
%fprintf( fid , 'Y\n' );
Line = sprintf( 'load NACA%04d.dat\n' , counter ) ;
fprintf( fid , Line ) ;
fprintf( fid ,'pane\n');
fprintf( fid ,'oper\n');
fprintf( fid ,'iter 200\n');
fprintf( fid ,'visc %20.6f\n', Reynolds);
fprintf( fid ,'mach %10.6f\n', Mach);
fprintf( fid ,'pacc\n');
%Line = sprintf( 'NACA%04d.pol\n' , ii ) ;
%fprintf( fid , Line );
fprintf( fid ,'\n');
fprintf( fid ,'\n');
fprintf( fid ,'alfa 0.0\n');
fprintf( fid ,'pacc\n');
fprintf( fid , 'pwrt\n' );
Line = sprintf( 'NACA%04d.pol\n' , counter ) ;
fprintf( fid , Line );
fprintf( fid ,'\n');
fprintf( fid ,'\n');
fprintf( fid ,'\n');
fprintf( fid ,'quit\n');
fclose(fid) ;
!xfoil.exe < xfoil.inp
%!./xfoil < xfoil.inp
%
%implementar la lectura de cl, cd y cm del archivo de resultados
%
PolarFile = sprintf( 'NACA%04d.pol' , counter ) ;
PolarFileID = fopen ( PolarFile , 'rt') ;
read = 1 ;
counter = 0 ;
alpha = 100.0 ;
cl = 10.0 ;
cd = 100.0 ;
cdp = 100.0 ;
cm = 100.0 ;
xtr1 = 1.0 ;
xtr2 = 1.0 ;
while ( read == 1)
Line = fgetl ( PolarFileID ) ;
counter = counter + 1 ;
if Line == -1
read = 0 ;
elseif counter == 13
data = sscanf ( Line , '%f %f %f %f %f %f %f') ;
alpha = data(1) ;
cl = data(2) ;
cd = data(3) ;
cdp = data(4) ;
cm = data(5) ;
xtr1 = data(6) ;
xtr2 = data(7) ;
end
end
fclose ( PolarFileID ) ;
  3 commentaires
Ameer Hamza
Ameer Hamza le 22 Juin 2018
fgetl() does return -1 when an end of file is reached, so the while loop will terminate eventually.
dpb
dpb le 22 Juin 2018
Bad eyes...missed it, that is correct.

Connectez-vous pour commenter.

Réponses (1)

Ameer Hamza
Ameer Hamza le 22 Juin 2018
As error points out the variable data is empty. The problem appears to be happening in the line
data = sscanf ( Line , '%f %f %f %f %f %f %f') ;
You are making the assumption that if fgetl return -1 then stop the loop otherwise sscanf() it will always 7 floating point numbers. But sprintf can fail this assumption. For example, here I am using 3 floating point number in each case.
sscanf('a 12 32', '%f %f %f') % a non-numeric character is present
ans =
[]
sscanf('10 20', '%f%f%f') % less then 3 numbers are present
ans =
10
20
Here are few solutions
  1. You either need to make sure that file strictly follows the format, i.e. only contain 7 numbers in each line.
  2. You need to change formatSpec to match the format of your file.
  3. Add extra checks in you code to see if the read line follow the required format.

Catégories

En savoir plus sur Low-Level File I/O dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by