too many open files even after adding fclose within my for loop

17 次查看(过去 30 天)
As the title says, I am receiving an error saying I have too many open files. I did look into it (via MatlabCentral) and attempted to fix it by adding 'fclose' after each 'fopen.' I am adding my for loop below. I included the first line of my code (readmatrix), but did not include the lines defining the variables because there are too many. What am I doing wrong here?
Thanks.
m=readmatrix('vertebrae_points_idno.xlsx');
%%did not include lines defining my variables%%
for i=1:length(m)
for j=1:length(m(:,1))
conversion=conversion_array(j);
%combines variables into matrices for saving and printing.
[c1,c2,c3,c4,c5,c6,c7]=matrix_combine(c1x(i,:),c1y(i,:),c2x(i,:),c2y(i,:),c3x(i,:),c3y(i,:),c4x(i,:),c4y(i,:),c5x(i,:),c5y(i,:),c6x(i,:),c6y(i,:),c7x(i,:),c7y(i,:));
%calculate centroids.
[c2_centroid,c3_centroid,c4_centroid,c5_centroid,c6_centroid,c7_centroid]=get_centroids(c2,c3,c4,c5,c6,c7);
%generate "ideal" arc
[line_x,line_y,x,y,C]=generate_arc(c2,c3,c4,c5,c6,c7);
%transpose for CircleFit
arc_xy=[line_x.' line_y.'];
%gather points of circle from vertebral points
Par=CircleFit(arc_xy);
%generate actual arc and plot
[x2,y2,true_lord_angle]=generate_second_arc(c2,c7,Par);
%calculate distance between two arcs
[converted_curve_dist,converted_max_curve_dist]=get_curve_distance(x,y,x2,y2,conversion);
%calculates extension angles
[c1_c7_angle,c2_c7_angle,c2c3_tangent,c3c4_tangent,c4c5_tangent,c5c6_tangent,c6c7_tangent]=rotation_angles(line_x,line_y,c1,c2,c3,c4,c5,c6,c7);
%closes image so the program can create another image with centroids.
close all
%calculates SVA, determines type of kyphosis, plots centroids and C2-C7 centroid line.
[kyphosis_type,sva_axis]=kyphosis_sva_new(c7,c2_centroid,c3_centroid,c4_centroid,c5_centroid,c6_centroid,c7_centroid, conversion);
spine_data=[converted_curve_dist,converted_max_curve_dist,sva_axis,true_lord_angle,c1_c7_angle,c2_c7_angle];
opening_name='spine_xray_data.txt';
opening_name2='translation_angles.txt';
fid=fopen(opening_name,'w');
fprintf(fid,'\n %10f\t %10.2f\t %10.2f\t %10.2f\t %10.2f\t %10.2f\t %10.2f\t %10s\t %10.4f',...
sub_id, spine_data,kyphosis_type,conversion);
fclose(fid);
fid2=fopen(opening_name2,'w');
fprintf(fid2,'\n %10.4f\t %10.4f\t %10.4f\t %10.4f\t %10.4f',...
c2c3_tangent,c3c4_tangent,c4c5_tangent,c5c6_tangent,c6c7_tangent);
fclose(fid2);
end
end

回答(2 个)

Voss
Voss 2024-8-22,17:37
It may be that too many files are still open, coming from previous runs of the code where you did not have the fclose commands yet.
Try running
fclose all
and then run your program again.
(Maybe your close all should be fclose all?)

Star Strider
Star Strider 2024-8-22,17:43
If you want to find out what files are open, see the documentation section on Get Information About Open Files.

Community Treasure Hunt

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

Start Hunting!

Translated by