How to print status

2 visualizaciones (últimos 30 días)
Rafael Zanetti
Rafael Zanetti el 17 de Jun. de 2020
Comentada: madhan ravi el 17 de Jun. de 2020
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.

Respuestas (2)

Steven Lord
Steven Lord el 17 de Jun. de 2020
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 comentario
madhan ravi
madhan ravi el 17 de Jun. de 2020
+1 for that SF line , been looking for it for days!!

Iniciar sesión para comentar.


madhan ravi
madhan ravi el 17 de Jun. de 2020
Editada: madhan ravi el 17 de Jun. de 2020
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 comentarios
Rafael Zanetti
Rafael Zanetti el 17 de Jun. de 2020
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 el 17 de Jun. de 2020
list = ["success", "failure"];
s = list((m<=0) + 1);

Iniciar sesión para comentar.

Categorías

Más información sobre Tables en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by