How to turn a code into a function while one of the input is string
2 views (last 30 days)
Show older comments
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 Comments
Geoff Hayes
on 3 Sep 2021
reza - please see declare function name, inputs, etc. that describes how to define the inputs to this function. In this case, your fourth input is not a valid input parameter. What is the intent as I don't see how it is used in your code? Or are you trying to define what A should be? i.e. an array that is 1x(N0H+N0C) and so want to pass that into the function. Will the function then determine the different elements for A using the
A(1,i)=input ('Please enter orientation of the ith layer:');
Perhaps you need to show the body of the function.
Accepted Answer
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 Comments
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
More Answers (0)
See Also
Categories
Find more on Testing Frameworks in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!