issue in saving variable values while writing a file with if condition?

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 Comments

Can you post a more detailed working example? In this one, exp1, exp2 and such are not defined.
Dear Benjamin, I am using a toolbox so kept those expressions as general. I am attaching a working .m file here.
clc
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 ii=1:1
fid1 = fopen(sprintf('tt1%d.dat',ii),'w' );
fid2 = fopen(sprintf('tt2%d.dat',ii),'w' );
fid3= fopen(sprintf('tt3%d.dat',ii),'w' );
for l1=0.01:0.1:3
for l2=0.01:0.1:1.6
for g1=0.01:0.1:0.5
for g2=0.01:0.1:0.5
for g3=0.01:0.1:0.5
%some calculations epx1 exp2 exp3.....
exp1=l1^2+l2^2-g1*(g2+g3);
exp2=l1^2+l2^2-2*g2*(g1+g3);
exp3=2*l1^2+l2^2-g3*(g1+g2);
exp4=2*l1^2-l2^2-g3*(g1+g2);
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
x10=x1; x20=x2; x30=x3;
g10=x10(:,1);
g20=x10(:,2);
g30=x10(:,3);
exp1=x10(:,4);
exp2=x10(:,5);
exp3=x10(:,6);
exp4=x10(:,7);
g101=x20(:,1);
g201=x20(:,2);
g301=x20(:,3);
exp11=x20(:,4);
exp21=x20(:,5);
exp31=x20(:,6);
exp41=x20(:,7);
g100=x30(:,1);
g200=x30(:,2);
g300=x30(:,3);
exp12=x30(:,4);
exp22=x30(:,5);
exp32=x30(:,6);
exp42=x30(:,7);
if(exp1>0.01)
fprintf(fid1,'%f %f %f %f %f %f %f %f %f\r\n',l1,l2,g10,g20,g30,exp1,exp2,exp3,exp4);
end
if(exp2>0.01)
fprintf(fid2,'%f %f %f %f %f %f %f %f %f \r\n',l1,l2,g101,g201,g301,exp11,exp21,exp31,exp41);
end
if(exp3>0.01)
fprintf(fid3,'%f %f %f %f %f %f %f %f %f\r\n',l1,l2,g100,g200,g300,exp12,exp22,exp32,exp42);
end
end%l2
end%l1
fclose(fid1);
fclose(fid2);
fclose(fid3);
end
Dear Ankit, thanks but storing data this way gives me an error "Index in position 2 exceeds array bounds." However, defining x1 , x2 and x3 insdie the l1 and l2 loops show the results but with missing g1 values as an issue in the main post.
Hi @Anil Kumar, defining variables before and after shouldn't have any impact.
Index in position 2 exceeds array bounds - this error you will get if you not defined your variables properly and you trying to access it.
Your x1 is an array of for e.g. 11743x7, are you talking about g10 should have 1 column from x1 array, g20 2 Column etc etc?
What do you mean with missing g1 values? g1 varies from 0.01:0.1:0.5, would you like to store these values ? I have updated the above code...
Dear Ankit, I already tried the way you edit your code, but still getting same error 'Index in position 2 exceeds array bounds.' The only way it works for me is defining those array (x1, x2....) inside the l2 loop. I tried with empty matrix too but that took random data which I didnt understand.
"Your x1 is an array of for e.g. 11743x7, are you talking about g10 should have 1 column from x1 array, g20 2 Column etc etc?" : I think this the way it's working, if I understand you correctly.
Yes, I want to store these values corresponding to the if condition there. if you see the written data files, there the values in g1 column are 0 only, means it's not storing those values. I have atatched a screenshot here, see the 3dr col.
'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?
Dear Ankit, I am plotting those expressions w.r.t col. 1 and I want to see corresponding values of inner loop (l2, g1,g2 and g3) for those expressions. I guess it makes sense now why I need g1 values. I think I am doing some silly mistake while saving data.

Sign in to comment.

Answers (0)

Asked:

on 2 Feb 2022

Commented:

on 3 Feb 2022

Community Treasure Hunt

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

Start Hunting!