use matlab please All Otri Homework 14 Review Homework Function Name: functiony
ID: 2291953 • Letter: U
Question
use matlab please
All Otri Homework 14 Review Homework Function Name: functiony Inputs 1. (char) A atring containing a function header Outputs: 1, (logical) A logical indicating whether or not the function header is valid Background: You're studying for finals and have finished all the practice tosts, but GT Wi-FI goos down before you can check your answeral To check to see whether all your function headers are valid, you decide to write a function in MATLAB. Aftorall, MATLAB will always be there for you when GT inevitably fails you again Function Description: a false indicating whether the header is valid Example: Write a function that takes in a atring containing a function header and outputs a true or > tout) functiony function [out] myfunction in)') out- true Hints: Using several conditionals will be easier than one long conditional. Think of a way to keep track of if one rule fails through a variable or a vector of variables . Create a helper function to check for valid variable names The functions strtrin) and 1skeyword() will be very useful.Explanation / Answer
function [out] = functiony(str)
str=strtrim(str);
out=true;
if(length(str)<10)
out=false;
elseif(~strcmp(str(1:8),'function'))
out=false;
elseif(~strcmp(str(9),' '))
out=false;
end
str=strtrim(str(9:end))
if(str(1)~='[')
out=false;
end
loceq=-1;
loc1=-1;
loc2=-1;
loc3=-1;
loc4=-1;
counteq=0;
count1=0;
count2=0;
count3=0;
count4=0;
for i=1:length(str)
if(str(i)=='[')
loc1=i;
count1=count1+1;
end
if(str(i)==']')
loc2=i;
count2=count2+1;
end
if(str(i)=='(')
loc3=i;
count3=count3+1;
end
if(str(i)==')')
loc4=i;
count4=count4+1;
end
if(str(i)=='=')
loceq=i;
counteq=counteq+1;
end
end
if(count1>1 || count2>1 || count3>1 || count4>1 || counteq>1)
out=false;
elseif(loceq==-1)
out=false;
elseif(loc1==-1)
out=false;
elseif(loc2==-1)
out=false;
elseif(loc3==-1)
out=false;
elseif(loc4==-1)
out=false;
end
if(out==false)
return;
end
if(loc1~=1)
out=false;
elseif(loc4~=length(str))
out=false;
elseif(loc2<loc1)
out=false;
elseif(loc4<loc3)
out=false;
elseif(loceq<loc2)
out=false;
elseif(loc3<loceq)
out=false;
end
opvars = str(loc1+1:loc2-1);
opvars=strsplit(opvars,',','CollapseDelimiters',false);
for i=1:size(opvars,2)
key=strtrim(char(opvars(i)));
if(strcmp(strtrim(opvars(i)),''))
out=false;
break;
end
if(~isstrprop(key(1),'alpha'))
out=false;
break;
end
p1=isstrprop(key,'wspace');
if(sum(p1)>0)
out=false;
break;
end
end
if(out==false)
disp('Improper Input variable definition');
return;
end
ipvars = str(loc3+1:loc4-1);
ipvars=strsplit(ipvars,',','CollapseDelimiters',false);
for i=1:size(ipvars,2)
key=strtrim(char(ipvars(i)));
if(strcmp(key,''))
out=false;
break;
end
if(~isstrprop(key(1),'alpha'))
out=false;
break;
end
if(~isstrprop(key(1),'alpha'))
out=false;
break;
end
p1=isstrprop(key,'wspace');
if(sum(p1)>0)
out=false;
break;
end
end
out=out;
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.