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

2 visualizaciones (últimos 30 días)
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 comentarios
Geoff Hayes
Geoff Hayes el 3 de Sept. de 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.
R@SH
R@SH el 4 de Sept. de 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.

Iniciar sesión para comentar.

Respuesta aceptada

Mathieu NOE
Mathieu NOE el 3 de Sept. de 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 comentarios
R@SH
R@SH el 4 de Sept. de 2021
Dear Mathieu,
I really apprciate your solution. But I didn't get it. A is a matrix with order of (1*NOH+NOC) and its entries should be chosen from {'h' or 'c'} h and c are defined in the body of the function. To elaborate on this, please check below examples:
NOH=2, NOC=3 A=[{'h'} {'c'} {'h'} {'h'} {'h'}] or A=[{'h'} {'c'} {'c'} {'c'} {'h'}]
NOH=1, NOC=2 A=[{'h'} {'h'} {'h'}] or A=[{'c'} {'c'} {'h'}]
NOH=4, NOC=2 A=[ {'c'} {'h'} {'h'} {'h'} {'h'} {'c'}] or A=[{'h'} {'h'} {'h'} {'c'} {'c'} {'c'}]
Would you please clarify whether your suggestion can cover this case?
Mathieu NOE
Mathieu NOE el 6 de Sept. de 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

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

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

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by