issue in saving variable values while writing a file with if condition?
4 次查看(过去 30 天)
显示 更早的评论
If I use max and min function the actual values of variable in loops (each data set) are saved for that coresponding max or min through indices q
[p, q]=max(x1(:,4));
However, if I have a general condition (if exp<1) for an expresssion and while saving data for this, the values of first loop (g1 below) are always zero and it never shows actual values.
for ii=1:1
fid1 = fopen(sprintf('test_1%d.dat',ii),'w' );
fid2 = fopen(sprintf('test_2%d.dat',ii),'w' );
fid3= fopen(sprintf('test_3%d.dat',ii),'w' );
for l1=0.01:s:3
for l2=0.01:s:1.6
x10=zeros(1,7);
x20=zeros(1,7);
x30=zeros(1,7);
x1=zeros(1,7);
x2=zeros(1,7);
x3=zeros(1,7);
for g1=0.01:0.01:0.5
for g2=0.01:0.01:0.5
for g3=0.01:0.01:0.5
%some calculations epx1 exp2 exp3.....
if(exp1<1)
x1=[x1; g1 g2 g3 exp1 exp2 exp3 exp4];
end
if(exp2<1)
x2=[x2; g1 g2 g3 exp1 exp2 exp3 exp4];
end
if(exp3<1)
x3=[x3; g1 g2 g3 exp1 exp2 exp3 exp4];
end
end%g3
end%g2
end%g1
% [p, q]=max(x1(:,4)) actually saves values of gi through q
% Even if i put if condition here, the results remains the same
% if(exp1<1)
% x10=x1; x20=x2; x30=x3;
% end
x10=x1; x20=x2; x30=x3;
g10=x10(1);
g20=x10(2);
g30=x10(3);
.
.
g101=x20(1);
.
.
if(exp1>0.001)
fprintf(fid1,'%f ...\r\n',l1,l2,g10,g20,g30,exp1,exp2,exp3,exp4);
end
if(exp2>0.001)
fprintf(fid2,'%f ...\r\n',l1,l2,g101,g201,g301,exp1,exp2,exp3,exp4);
end
if(exp3>0.001)
fprintf(fid3,'%f ...\r\n',l1,l2,g100,g200,g300,exp1,exp2,exp3,exp4);
end
end%l2
end%l1
fclose(fid1);
fclose(fid2);
fclose(fid3);
end
How can I see actual values of the data set (e.g. gis for each value of l1 and l2) where the given condition is fullfilled? I have doubt that other calculated values of expressions may be wrong too due to the previous issue. Kindly help what am I doing wrong here while saving and retreiving data from x1, x2 and x3. Thanks!
8 个评论
Ankit
2022-2-3
'Index in position 2 exceeds array bounds' - you will get this error when you are trying to access the index which is not defined . As i mentioned it doesn't matter where you define..
This result due to the fact that the index number is more than the array elements.
what are you reading from these *.dat files?
回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Graphics Object Programming 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!