MATLAB Answers

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

조회 수: 5(최근 30일)
reza Shahzadi
reza Shahzadi 2021년 9월 3일
댓글: Mathieu NOE 2021년 9월 6일
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
reza Shahzadi
reza Shahzadi 2021년 9월 4일
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
Mathieu NOE 2021년 9월 3일
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
Mathieu NOE
Mathieu NOE 2021년 9월 6일
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

댓글을 달려면 로그인하십시오.

추가 답변(0개)

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by