I created code in the command window but I could not write it as a function.
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Berfin Çetinkaya
am 15 Mär. 2022
Kommentiert: Voss
am 16 Mär. 2022
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 Kommentare
Jan
am 16 Mär. 2022
Again: What function header do you use? If you post the code, the readers can find the problem.
Akzeptierte Antwort
Voss
am 15 Mär. 2022
Bearbeitet: Voss
am 15 Mär. 2022
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 = my_function_with_input_and_output(105)
% 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 Kommentare
Voss
am 16 Mär. 2022
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
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Use COM Objects in MATLAB finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!