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

KSSV
KSSV 2018년 2월 21일
Attach code or copy code here....not image snippets....
Guillaume
Guillaume 2018년 2월 21일
Pictures of code are not particularly useful. We can't copy/paste the code making it much harder to test/debug/modify/give you a corrected version. Plus, your picture is not particularly sharp, and on my screen I can barely read your code.
Copy/paste the code in your question (then use the {}Code button) or attach it as an m file.
Also gives us a better description of the problem.
zhixuan hong
zhixuan hong 2018년 2월 21일
m file
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

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

 채택된 답변

Guillaume
Guillaume 2018년 2월 21일
편집: Guillaume 2018년 2월 21일

0 개 추천

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

Thanks for your reply . really appreciate but the result like this , IMC and Status keep repeat...
for k = 1:n
if x>= 75
Status = 'Pass';
else
Status = 'Fail';
end
No = num2str(k);
IMC = num2str(x);
dlmwrite('Version_1',[No IMC Status],'-append','delimiter','\t','precision','%.2f');
end
Guillaume
Guillaume 2018년 2월 21일
As I wrote: As for x and areaRatio, since they're both scalar I have no idea how they're supposed to change in the loop
Since they're constant and IMC and Status only depend on them, then yes nothing is going to change.
I have no idea what you intended to do in that loop, but if you want the output to vary, you need to vary x and areaRatio in the loop.
zhixuan hong
zhixuan hong 2018년 2월 22일
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 2018년 2월 22일
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.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

질문:

2018년 2월 21일

댓글:

2018년 2월 22일

Community Treasure Hunt

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

Start Hunting!

Translated by