issue in saving variable values while writing a file with if condition?
Show older comments
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
Benjamin Thompson
on 2 Feb 2022
Can you post a more detailed working example? In this one, exp1, exp2 and such are not defined.
Anil Kumar
on 3 Feb 2022
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
Anil Kumar
on 3 Feb 2022
Ankit
on 3 Feb 2022
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...
Anil Kumar
on 3 Feb 2022
Ankit
on 3 Feb 2022
'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?
Anil Kumar
on 3 Feb 2022
Answers (0)
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!