How to print status

2 次查看(过去 30 天)
Rafael Zanetti
Rafael Zanetti 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 中查找有关 Logical 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by