# funtion to convert value to string

3 views (last 30 days)
Slane Haltine on 15 Oct 2020
Edited: Adam Danz on 15 Oct 2020
I am very new to MATLAB but had the thought that I could achieve this with a if-else loop within a function.
I am trying to create a function that will output a string when given a p value.
When I give it an input (for example, a p value of 0.7 or 0.0003) I am trying to get it to output a sting based on the value such that
if p is smaller or equal to 0.01 then return a string with two asterisks '**'
if p is smaller or equal to 0.05 then return a string with one asterisk '*'
if p is smaller than or equal to 0.10 then it should return a string that
says 'trend towards significance'
if p is larger than 0.10 it should say 'not significant'
The function I have so far is
function [a] = pval(p)
%UNTITLED Summary of this function goes here
% Detailed explanation goes here
if p <= 0.01
a = '**'
else if p <= 0.05
a = '*'
else if p <= 0.10
a = 'trend toward significance'
else
a = 'not significant'
end

Mathieu NOE on 15 Oct 2020
hi
yiu have conditions that could be met simulteanously
to avoid overlap in condition domains , I suggest :
if p <= 0.01
a = '**'
elseif p > 0.01 && p <= 0.05
a = '*'
elseif p > 0.05 && p <= 0.10
a = 'trend toward significance'
else
a = 'not significant'
end
Slane Haltine on 15 Oct 2020
That was it! Thank you so much!

Adam Danz on 15 Oct 2020
Edited: Adam Danz on 15 Oct 2020
Alternative solution: use an anonymous function.
str = getStars(p) returns a string for the given p-value.
stars = {'**','*','trend toward significance','ns'};
getStars = @(p)stars{find(p <= [.01 .05 .1, inf],1,'first')};
Note: Traditionally, these are the astrices used to indicate significance level:
• p <= 0.05 n.s.
• p < 0.05 *
• p < 0.01 **
• p < 0.001 ***
The 'trend toward significance' isn't agreed upon but typically is very close to 0.05 so I'd lower that upper threshold of 0.1 to 0.065 or something like that (but all of this is completely arbitrary and silly in the first place).
When p is equal to one of the threshold values it receives the lower significance level. For example, when p=0.01 it gets one star.
Also, when p is outside of the expected bounds of 0:1 or equals NaN, it should return "N/A" or some other indicator that something's off (p never equals 1 in the real world).
IMO, this version is the one that should be used if you insist on using significance levels.
stars = {'N/A','***','**','*','trend toward significance','ns','N/A'};
getStars = @(p)stars{min([find(p < [0, 0.001 .01 .05 .1, 1, inf],1),numel(stars)])};
Thanks to Rik for the suggestion for handling NaN values.
Demo using the traditional definitions above
>> getStars(0)
ans =
'***'
>> getStars(0.0001)
ans =
'***'
>> getStars(0.001)
ans =
'**'
>> getStars(0.0025)
ans =
'**'
>> getStars(0.01)
ans =
'*'
>> getStars(0.05)
ans =
'trend toward significance'
>> getStars(0.1)
ans =
'ns'
>> getStars(0.9)
ans =
'ns'
>> getStars(1)
ans =
'N/A'
>> getStars(inf)
ans =
'N/A'
>> getStars(-.001)
ans =
'N/A'
>> getStars(nan)
ans =
'N/A'
Notes on the p-value
In most cases, p-values are not used correctly, are misinterpretted and they should therefore be avoided. Hundreds if not thousands of scientistis and staticticians support the movement to replace statistical significance (ie, the p-value) with alternatives such as nonparametric tests, bootstrapping, and visualizing error and letting the reader interpret results, etc. Categorizing significance based on p-values is problematic and is on its way out the door. Several high impact journals have either banned the use of p-values complete or have insisted that p-values are reported without rounding them to arbitrary significance levels.
Adam Danz on 15 Oct 2020
Good point, Rik! I love to over-engineer.
Here's a version that handles negative values (including -inf), inf, and nan, and values that exceed 1, all of which return 'N/A' since p values should be constrained to 0:1.
stars = {'N/A','***','**','*','trend toward significance','ns','N/A'};
getStars = @(p)stars{min([find(p < [0, 0.001 .01 .05 .1, 1, inf],1),numel(stars)])};
I'll update the answer to include this.

R2020a

### Community Treasure Hunt

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

Start Hunting!