How to print status

조회 수: 3 (최근 30일)
Rafael Zanetti
Rafael Zanetti 2020년 6월 17일
댓글: madhan ravi 2020년 6월 17일
Hi, I am writing a program that I need the output be print "Success" or "Failure" on a table of 4 columns, my program is that:
function m = margin_of_safety(F,sigma);
m = F - sigma;
table = [F;sigma;m];
fprintf(' F sigma m \n');
fprintf('%4d %10.2f %10.2f \n',table);
end
And I need the test be this way,but I want replace the checks for text as I had explained
I thank you.

답변 (2개)

Steven Lord
Steven Lord 2020년 6월 17일
I recommend not using table as a variable name as table is the name of a data type in MATLAB.
Let's use your sample data.
>> sampleData = [72 60 12; 72 80 -8; 82 60 22; 82 80 2];
Build a table array using your sample data.
>> T = array2table(sampleData, 'VariableNames', ["F", "sigma", "m"]);
Turn the m variable from your table T into a categorical array with value "failure" when T.m is not greater than 0 and "success" when T.m is greater than 0.
>> SF = categorical(T.m > 0, [false, true], ["failure", "success"]);
Now add this variable to the table. I'm using a table variable name that is not a valid MATLAB identifier, which is allowed in a table since release R2019b. Use a different name if you're using an earlier release.
>> T.("Success/Failure") = SF
T =
4×4 table
F sigma m Success/Failure
__ _____ __ _______________
72 60 12 success
72 80 -8 failure
82 60 22 success
82 80 2 success
Let's get some data from T.
T(T.("Success/Failure") == "failure", :)
  댓글 수: 1
madhan ravi
madhan ravi 2020년 6월 17일
+1 for that SF line , been looking for it for days!!

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


madhan ravi
madhan ravi 2020년 6월 17일
편집: madhan ravi 2020년 6월 17일
function m = margin_of_safety(F,sigma);
m = F - sigma;
s = ["success", "failure", "success", "success"];
TablE = [F;sigma;m;s];
fprintf(' F sigma m %s\n');
fprintf('%4d %10.2f %10.2f \n',TablE);
end
  댓글 수: 2
Rafael Zanetti
Rafael Zanetti 2020년 6월 17일
I thank for you support madhan, but I also would like to create a statement to generate the column, for example, if m<=0 print Failure, and m>0 print success.
madhan ravi
madhan ravi 2020년 6월 17일
list = ["success", "failure"];
s = list((m<=0) + 1);

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

카테고리

Help CenterFile Exchange에서 Tables에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by