How can I run my code for each row of data in my excel spreadsheet?

1 view (last 30 days)
I have a code in MATLAB which works but I want it to run individually for all the rows and produce result for each row. Instead, it runs through the data and gives result for the final row only
  4 Comments
dpb
dpb on 5 Jul 2018
"We can't debug what we can't see..." Attach the code.
BUT, by your description you're using a loop; in Matlab you should read the data into an array and write your function to operate on the whole array; many times one avoids looping entirely and the result automagically then becomes the desired array.
BUT, "We can't debug what we can't see..." Attach the code.
Ezinne Adindu
Ezinne Adindu on 5 Jul 2018
H_2 = xlsread('main DGA.xlsx',1,'M2:M100');
CH_4 = xlsread('main DGA.xlsx',1,'H2:H100');
C_2H_2 = xlsread('main DGA.xlsx',1,'L2:L100');
C_2H_4 = xlsread('main DGA.xlsx',1,'J2:J100');
C_2H_6 = xlsread('main DGA.xlsx',1,'K2:K100');
R_1 = C_2H_2./C_2H_4;
R_2 = CH_4./H_2;
R_3 = C_2H_4./C_2H_6;
if R_1<0.1
if (R_2>=0.1 & R_2<3)
if R_3>1
Code=1;
elseif (R_3>=1 & R_3<=3)
Code=7;
else
Code=2;
end
elseif R_2<0.1
if R_3<1
Code=3;
elseif (R_3>=1 & R_3<=3)
Code=7;
else
Code=10;
end
elseif R_2>1
if R_3<1
Code=8;
elseif (R_3>=1 & R_3<=3)
Code=9;
else
Code=10;
end
end
elseif (R_1>=1 & R_1<=3)
if (R_2>=0.1 & R_2<1)
if R_3>1
Code=2;
elseif (R_3>=1 & R_3<=3)
Code=5;
else
Code=6;
end
elseif R_2<0.1
if R_3<1
Code=4;
elseif (R_3>=1 & R_3<=3)
Code=2;
else
Code=2;
end
elseif R_2>1
if R_3<1
Code=2;
elseif (R_3>=1 & R_3<=3)
Code=2;
else
Code=2;
end
end
else
Code=2;
end

Sign in to comment.

Accepted Answer

Guillaume
Guillaume on 5 Jul 2018
Your R_1, R_2 and R_3 are vectors. Therefore R_1 < 0.1 is a logical vectors, which you're then passing to if. It's almost always a mistake to pass a vector to if and it certainly is in your case. Despite your statement, your code, in fact, does not work. It can only work when R_1, R_2 and R_3 are scalars.
One way you could fix it is to write your if tests into a for loop that iterates over the elements of these 3 vectors simultaneously:
Code = zeros(size(R-1));
for idx = 1:numel(R_1)
r1elem = R_1(idx);
r2elem = R_2(idx);
r3elem = R_3(idx);
%now all your if, testing r1elem, r2elem, r3elem instead of R_1, R_2, R_3
%and assigning to Code(idx) instead of Code
end
I'll point out that you also have made some mistakes in your if testing with for example
if R_3 >1 %probably should have been R_3 < 1
...
elseif (R_3 >= 1 & ...)
Similarly, we find
if (R_2>=0.1 & R_2<3)
...
elseif R_2 > 1 %already taken care by the if for R_2 > 1 & R_2 < 3
Clearly, it is not easy to get such a spaghetti of if/else/elseif correct and thankfully there are much easier ways of obtaining the same result. E.g:
R1_thresholds = [-Inf, 0.1, 1, 3, Inf];
R2_thresholds = [-Inf, 0.1, 1, Inf];
R3_thresholds = [-Inf, 1, 3, Inf];
%create a 3D matrix that tells us which code corresponds to the intersection of each threshold
%R_1 along the rows, R_2 along the columns, R_3 along the pages
%I.E. row 1 of code_by_bin corresponds to R1<0.1, row 2 to R_1>=0.1 & R_1<1, row 3 to R_1>1 & R_1<=3, etc.
%column 1 corresponds to R2<0.1, column 2 to R2>0.1 & R2<1, etc.
%page 1 correspinds to R3<1, etc.
Code_by_bin = cat(3, ... first page for R3<1
[3, 1, 8; ... for R1<0.1 and R2<1, R2>=0.1 & R2<1, R2>=1 respectively
2, 2, 2; ... for R1>=0.1 & R1<1
4, 2, 2; ... for R1>= 1 & R1<3, and R2<1, R2>=0.1 & R2<1, R2>=1 respectively
2, 2, 2], ... for R1>=3
[7, 7, 8; ... 2nd page for R3>=1 & R3<3, this row for R1<0.1 and R2<1, R2>=0.1 & R2<1, R2>=1 respectively
2, 2, 2; ... for R1>=0.1 & R1<1
2, 5, 2; ... for R1>= 1 & R1<3, and R2<1, R2>=0.1 & R2<1, R2>=1 respectively
2, 2, 2], ... for R1>=3
[2, 2, 2; ... 3rd page for R3>=3 everything is 2 regardless of R1 and R2
2, 2, 2;
2, 2, 2;
2, 2, 2]);
%... your excel loading code
R_1 = C_2H_2./C_2H_4;
R_2 = CH_4./H_2;
R_3 = C_2H_4./C_2H_6;
row = discretize(R1, R1_thresholds);
col = discretize(R2, R2_thresholds);
page = discretize(R3, R3_thresholds);
code = Code_by_bin(sub2ind(size(Code_by_bin), row, col, page));
I may have made some errors in the actual code values against bin because of the many mistakes in your if branches. You can fix these easily.
  1 Comment
Ezinne Adindu
Ezinne Adindu on 5 Jul 2018
Wow!
Thank you so much. I implemented your first suggestion and it works
Would review the if else part of the code

Sign in to comment.

More Answers (0)

Categories

Find more on MATLAB Coder in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!