For-loop, inserting loopcount into string output

조회 수: 44(최근 30일)
Emil Krogager 11 Jun 2018
댓글: Sandro Lecci 12 Jun 2018
Hello,
I've been tasked with a project which partly consists of having to load data from a file and displaying it in a (X,3) matrix. The challenging part is that there are requirements for the values for every coloumn of the matrix, such as; the numbers in coloumn 1 cannot be lower than 20, nor higher than 60.
If a value of given coloumn does not meet the criteria, the respective set of data is to be eliminated and removed from the end result matrix. I've managed to do just that without any major problems, although I am not stating that my code is as simple as possible. Here is the problem, however; for every time that a given value does not meet the respective requirement, the code is supposed to provide a string output describing exactly which value it is. Since I've tested my code where multiple values in different coloumns should display the error message, I simply do not know how to make the code display the exact position of the specific value.
In the end it simply displays the three error strings, one after the other. I was thinking that i could add the value "i" in the strings of my if-statements in my for-loop, causing the string to display which position the value has in the matrix, for example:
disp('Error; explanation, [value (i)]'), or something alike. I just dont know the syntax.
I'd love to get some help! Thank you in advance.
if true
% code
end
A1 = zeros(length(Q),1); A2 = zeros(length(Q),1); A3 = zeros(length(Q),1);
Temperature = Q(:,1);
GrowthRate = Q(:,2);
Bacteria = Q(:,3);
i = 1;
for i = 1 : length(Q)
if (Temperature(i)>=20) && (Temperature(i)<=60)
A1(i) = A1(i) + Temperature(i);
end
if (GrowthRate(i)>=0)
A2(i) = A2(i) + GrowthRate(i);
end
if (Bacteria(i)>=1) && (Bacteria(i)<=4)
A3(i) = A3(i) + Bacteria(i);
end
if (Temperature(i)<20)
A1(i) = 0; A2(i) = 0; A3(i) = 0;
disp('Error; explanation');
elseif (Temperature(i)>60)
A1(i) = 0; A2(i) = 0; A3(i) = 0;
disp('Error; explanation');
end
if (GrowthRate(i)<0)
A1(i) = 0; A2(i) = 0; A3(i) = 0;
disp('Error; explanation');
end
if (Bacteria(i)<1)
A1(i) = 0; A2(i) = 0; A3(i) = 0;
disp('Error; explanation');
elseif (Bacteria(i)>4)
A1(i) = 0; A2(i) = 0; A3(i) = 0;
disp('Error; explanation');
end
i = i + 1;
end
A1 = A1(A1~=0); A2 = A2(A2~=0); A3 = A3(A3~=0);
data = [A1, A2, A3];
댓글 수: 1표시숨기기 없음
Dennis 11 Jun 2018
You can use strcat to put several strings together and num2str to convert i or A(i) to a string.
errorstring=strcat('Error;',{' '},num2str(A(i)),' <20!');
disp(errorstring)

댓글을 달려면 로그인하십시오.

채택된 답변

Sandro Lecci 11 Jun 2018
Dear Emil,
I haven't check whether my proposition works, I let you try it.
A1 = zeros(length(Q),1); A2 = zeros(length(Q),1); A3 = zeros(length(Q),1);
Temperature = Q(:,1);
GrowthRate = Q(:,2);
Bacteria = Q(:,3);
for i = 1 : length(Q)
%check Temperature
if (Temperature(i)>=20) && (Temperature(i)<=60)
A1(i) = Temperature(i);
elseif Temperature(i)< 20
fprintf('ERROR on line %d : Temperature < 20\n', i)
elseif Temperature(i)> 60
fprintf('ERROR on line %d : Temperature > 60\n', i)
end
%check Growthrate
if (GrowthRate(i)>=0)
A2(i) = GrowthRate(i);
else
fprintf('ERROR on line %d : GrowthRate < 0\n', i)
end
%check Bacteria
if (Bacteria(i)>=1) && (Bacteria(i)<=4)
A3(i) = A3(i) + Bacteria(i);
elseif Bacteria(i)<1
fprintf('ERROR on line %d : Bacteria < 1\n', i)
elseif Bacteria(i)>4
fprintf('ERROR on line %d : Bacteria > 4\n', i)
end
end
A1 = A1(A1~=0); A2 = A2(A2~=0); A3 = A3(A3~=0);
data = [A1, A2, A3];
end
I just want you to pay attention to the last commands. You can concatenate them into the data matrix only if they have the same dimensions ( vector length). This assumes that you remove the same amount of data in each vector A (possibly the same lines..). If this is not the case you will encounter an error. Would you like to exclude the same line in every A vector, e.g. if any of the condition "Temperature - GrowthRate or Bacteria" is wrong for a given i ?
Best,
Sandro
P.S. There is no need in separating Q into A1-3, you can access the column of interest in Q directly
댓글 수: 4표시숨기기 이전 댓글 수: 3
Sandro Lecci 12 Jun 2018
Absolutely,
the "continue" command just jumps to the next iteration (i+1) and does not break the loop.
Imagine the condition where at i = 3 (and length(Q) = 10) Temperature(i) < 20. The if statement will leave Temperature(i) = 0 and linesToKeep(i) will also remain 0.
At this point you have the minimal requirement fulfilled to discard that line, independently of the outcome of Bacteria(i) and GrowthRate(i).
Without the "continue" statement, you will evaluate GrowthRate(i) (which we suppose now to be >0). Do you want to keep this number, knowing that Temperature(i) is < 20?
I hope this clarifies my code!
Best,

댓글을 달려면 로그인하십시오.

추가 답변(1개)

ES 11 Jun 2018
disp(['Error in row ', num2str(i)]);

댓글을 달려면 로그인하십시오.

Community Treasure Hunt

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

Start Hunting!

Translated by