# How to turn a code into a function while one of the input is string

1 view (last 30 days)
R@SH on 3 Sep 2021
Commented: Mathieu NOE on 6 Sep 2021
I wrote a code and it works without ant error or problem. I want to turn it in to a function. this function has 4 input which 3 of them are numeral and the last one is a matrix with strings as its entries. Here is the summary of the code I wrote:
NOH=input ('Please enter number of hoop layers:');
NOC=input('Please enter number of cross layers:');
tetta=input ('Please enter orientation of cross layers:');
for i=1:NOH+NOC
A(1,i)=input ('Please enter orientation of the ith layer:');
if strcmp(A(1,i),'h')
t_h(i)=(Ns*T/B)/(V_f*ro_f);
t_c(i)=0;
tetta=pi/2;
end
elseif strcmp(A(1,i),'c')
t_c(i)=(2*Ns*T/B*sin(tetta))/(V_f*ro_f);
t_h(i)=0;
end
As it can be observed NOH,NOC, tetta are 3 numeral variable and A[1,NOH+NOC] is a matrix which entries of that could be {'h'} or {'c'}.
I wrote this function as below but its not working. I really appreciate your kind instructions in this matter.
function[PS]=stiffness1(NOH,NOC,tetta,[A(1,NOH+NOC)])
.....
end
##### 2 CommentsShowHide 1 older comment
R@SH on 4 Sep 2021
HI dear Geoff
You re right. this is the way I want to show the body of the function. A is a 1*NOH+NOC matrix and the entries of this matrix are string. they are either "h" or "c" and it the body of this function they are defined. Now I just want to be able to enter those inputs as a matrix in my code.

Mathieu NOE on 3 Sep 2021
hello Reza
see my suggestion below
A is now a cell array
I added a structure param to easily pass the various coefficients to the subfuntion
you only need now to add the computation of PS
clc
clearvars
%% main code
NOH=input ('Please enter number of hoop layers:');
NOC=input('Please enter number of cross layers:');
tetta=input ('Please enter orientation of cross layers:');
for ci=1:NOH+NOC
A{ci} = input (['Please enter orientation of the ' num2str(ci) ' layer (type ''h'' or ''c'') :']); % cell array of char / string
end
% parameters (structure) to pass Ns,T,B,V_f,ro_f to the subfunction
% dummy values
param.Ns = 1;
param.T = 1;
param.B = 1;
param.V_f = 1;
param.ro_f = 1;
[PS] = stiffness1(NOH,NOC,tetta,A,param);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% subfunction
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [PS] = stiffness1(NOH,NOC,tetta,A,param)
for ci=1:NOH+NOC
if strcmp(A{ci},'h')
% t_h(ci)=(Ns*T/B)/(V_f*ro_f);
t_h(ci)=(param.Ns*param.T/param.B)/(param.V_f*param.ro_f);
t_c(ci)=0;
tetta=pi/2;
elseif strcmp(A{ci},'c')
% t_c(ci)=(2*Ns*T/B*sin(tetta))/(V_f*ro_f);
t_c(ci)=(2*param.Ns*param.T/param.B*sin(tetta))/(param.V_f*param.ro_f);
t_h(ci)=0;
end
end
% your code for PS here
PS = 0;
end
##### 2 CommentsShowHide 1 older comment
Mathieu NOE on 6 Sep 2021
hello Reza
yes , I did test your data and it works . You can double check by using the debug mode , put some breakpoints in the subfunction section and test the code
of course , as the PS computation is not defined , this remains to be done and checked
clc
clearvars
%% main code
% NOH=input ('Please enter number of hoop layers:');
% NOC=input('Please enter number of cross layers:');
% tetta=input ('Please enter orientation of cross layers:');
%
% for ci=1:NOH+NOC
% A{ci} = input (['Please enter orientation of the ' num2str(ci) ' layer (type ''h'' or ''c'') :']); % cell array of char / string
% end
%% test data
% % test inputs #1 % OK
% tetta= pi/2; %
% NOH=2
% NOC=3
% % A=[{'h'} {'c'} {'h'} {'h'} {'h'}] % OK
% A=[{'h'} {'c'} {'c'} {'c'} {'h'}] % OK
% % test inputs #2 % OK
% tetta= pi/2; %
% NOH=1
% NOC=2
% A=[{'h'} {'h'} {'h'}] % OK
% % A=[{'c'} {'c'} {'h'}] % OK
% test inputs #3 % OK
tetta= pi/2; %
NOH=4
NOC=2
% A=[ {'c'} {'h'} {'h'} {'h'} {'h'} {'c'}] % OK
A=[{'h'} {'h'} {'h'} {'c'} {'c'} {'c '}] % OK
% parameters (structure) to pass Ns,T,B,V_f,ro_f to the subfunction
% dummy values
param.Ns = 1;
param.T = 1;
param.B = 1;
param.V_f = 1;
param.ro_f = 1;
[PS] = stiffness1(NOH,NOC,tetta,A,param);
%% subfunction
function [PS] = stiffness1(NOH,NOC,tetta,A,param)
for ci=1:NOH+NOC
if strcmp(A{ci},'h')
% t_h(ci)=(Ns*T/B)/(V_f*ro_f);
t_h(ci)=(param.Ns*param.T/param.B)/(param.V_f*param.ro_f);
t_c(ci)=0;
tetta=pi/2;
elseif strcmp(A{ci},'c')
% t_c(ci)=(2*Ns*T/B*sin(tetta))/(V_f*ro_f);
t_c(ci)=(2*param.Ns*param.T/param.B*sin(tetta))/(param.V_f*param.ro_f);
t_h(ci)=0;
end
end
% your code for PS here
PS = 0;
end