I created code in the command window but I could not write it as a function.

조회 수: 1 (최근 30일)
N = 138
j=1;
for i = 1:2:N
A(1,i) = int32(j);
A(2,i) = randi([1 10],1);
A(1,i+1) = int32(j);
A(2,i+1) = randi([11 18],1);
j = j+1;
end
hello, I created such a code on the command window, but I could not write it as a function, it gave an error. Can you turn this into a function?
  댓글 수: 4
Jan
Jan 2022년 3월 16일
Again: What function header do you use? If you post the code, the readers can find the problem.
Berfin Çetinkaya
Berfin Çetinkaya 2022년 3월 16일
function [outputArg1,outputArg2] = son(inputArg1,inputArg2)
Is the function title "son" here?
i updated the code like this. I still haven't been able to convert it to a function.
for b = 2:9
N = 138;
j=1;
for i = 1:2:N
A(1,i) = int32(j);
A(b,i) = randi([1 10],1);
A(1,i+1) = int32(j);
A(b,i+1) = randi([11 18],1);
j = j+1;
end
end
c= birincifaz(:,:)
x=find(c==1)
ja=randi(613,1)
c(x(ja))= randi(10,1)

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

채택된 답변

Voss
Voss 2022년 3월 15일
편집: Voss 2022년 3월 15일
If you are going to write a function, you have to decide what its inputs and outputs are.
Here I've put your code into four separate functions, each of which takes zero or one inputs and returns zero or one outputs, so you can see how you might define your function based how you need to use it (i.e., what its its inputs and outputs should be).
(All function definitions should be in m-file(s). Functions cannot be defined on the command-line.)
% calling the functions defined below:
my_function()
my_function_with_input(100)
my_A = my_function_with_output()
my_A = 2×138
1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10 11 11 12 12 13 13 14 14 15 15 5 14 7 15 9 12 9 12 8 17 3 17 9 18 9 11 3 16 1 12 4 12 6 18 2 11 8 16 1 18
my_A = my_function_with_input_and_output(105)
my_A = 2×106
1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10 11 11 12 12 13 13 14 14 15 15 1 16 3 14 5 14 5 17 4 11 5 12 5 17 8 11 8 13 6 18 1 17 4 15 2 14 9 15 9 13
% function definitions follow:
% my_function is a function that takes no inputs and returns no outputs
function my_function()
N = 138;
j=1;
for i = 1:2:N
A(1,i) = int32(j);
A(2,i) = randi([1 10],1);
A(1,i+1) = int32(j);
A(2,i+1) = randi([11 18],1);
j = j+1;
end
end
% my_function_with_input is a function that takes one input (N) and returns
% no outputs
function my_function_with_input(N)
j=1;
for i = 1:2:N
A(1,i) = int32(j);
A(2,i) = randi([1 10],1);
A(1,i+1) = int32(j);
A(2,i+1) = randi([11 18],1);
j = j+1;
end
end
% my_function_with_output is a function that takes no inputs and returns
% one output (A)
function A = my_function_with_output()
N = 138;
j=1;
for i = 1:2:N
A(1,i) = int32(j);
A(2,i) = randi([1 10],1);
A(1,i+1) = int32(j);
A(2,i+1) = randi([11 18],1);
j = j+1;
end
end
% my_function_with_input_and_output is a function that takes one input (N)
% and returns one output (A)
function A = my_function_with_input_and_output(N)
j=1;
for i = 1:2:N
A(1,i) = int32(j);
A(2,i) = randi([1 10],1);
A(1,i+1) = int32(j);
A(2,i+1) = randi([11 18],1);
j = j+1;
end
end
  댓글 수: 2
Berfin Çetinkaya
Berfin Çetinkaya 2022년 3월 16일
I tried the code below. but it gave a warning. It writes:
>> my_function_with_input_and_output
Not enough input arguments.
Error in my_function_with_input_and_output (line 3)
for i = 1:2:N
function A = my_function_with_input_and_output(N)
j=1;
for i = 1:2:N
A(1,i) = int32(j);
A(2,i) = randi([1 10],1);
A(1,i+1) = int32(j);
A(2,i+1) = randi([11 18],1);
j = j+1;
end
end
Voss
Voss 2022년 3월 16일
The functions with inputs have to be called with inputs:
% calling the functions defined below:
my_function()
my_function_with_input(100)
my_A = my_function_with_output()
my_A = my_function_with_input_and_output(105)
Notice how I send a number to the ones that are "with_input".
So you would call
my_function_with_input_and_output(105)
on the command-line, where 105 there is the input to the function my_function_with_input_and_output

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Use COM Objects in MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by