For loop problem (Help)

11 Ansichten (letzte 30 Tage)
zhixuan hong
zhixuan hong am 21 Feb. 2018
Kommentiert: Guillaume am 22 Feb. 2018
Hi guys , anyone can help me to check my coding ...
Current result, something wrong ...
Desire result , How to get table like this? I'm trying to create an auto data collection software by using GUI.
  4 Kommentare
zhixuan hong
zhixuan hong am 21 Feb. 2018
m file
Guillaume
Guillaume am 21 Feb. 2018
Code as found in the m file:
subjects={'No', 'IMC(%)','Status'}
fid=fopen('Version_1','w');
fprintf(fid,'%s %s %s \r\n',subjects{:});
for k = 1:n
if x>= 75
No = num2str(n);
IMC = num2str(areaRatio);
Status = 'Pass';
dlmwrite('Version_1',[No IMC Status],'-append','delimiter','\t','precision','%.2f');
else
No = num2str(n);
IMC = num2str(areaRatio);
Status = 'Fail';
dlmwrite('Version_1',[No IMC Status],'-append','delimiter','\t','precision','%.2f');
end
end

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Guillaume
Guillaume am 21 Feb. 2018
Bearbeitet: Guillaume am 21 Feb. 2018
As far as I can tell the only difference between the two branches of the if is the value of Status, so a better way of writing that if ... else would have been:
if x>= 75
Status = 'Pass';
else
Staus = 'Fail';
end
No = num2str(n);
IMC = num2str(areaRatio);
dlmwrite('Version_1',[No IMC Status],'-append','delimiter','\t','precision','%.2f');
Avoid writing the same thing twice since you double the chance that you make a mistake in one of them.
Also I'm not sure why you're using dlmwrite to write text. As your example show each character ends up separated by a tab. Not very readable.
As for your problem, your loop reduces to:
for k = 1:n
dlmwrite('Version_1', Variables_That_Do_Not_Depend_ON_k, ...)
end
Nothing depends on the iteration variable k in your loop, so, yes you're going to be writing n times the same things. I suspect that No should be
No = num2str(k);
As for x and areaRatio, since they're both scalar I have no idea how they're supposed to change in the loop. Note that x and areaRatio are the same since earlier you have x = areaRatio. Why have two names for the thing? It just adds confusion.
  4 Kommentare
zhixuan hong
zhixuan hong am 22 Feb. 2018
yup , the IMC also a variable . After pressing the push button, areaRatio/x (IMC) will be calculated by using formula under push button function.
Guillaume
Guillaume am 22 Feb. 2018
How to vary areaRatio/x/IMC
I don't know! You tell us. Only you knows what you intended to do with that loop but didn't do.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by