The document describes converting a MATLAB code into a function. It includes code to take in a row vector, calculate elements of a matrix based on the vector, and plot the matrix. The user is getting errors when trying to run the code and needs help converting it to a function.
How do I transfer this m-file to make it into a Function m-file in MAT.docx
1. How do I transfer this m-file to make it into a Function m-file in MATLAB? This is the only
thing i have left before i turn in my project, if you can help me clear this up i would appreciate it.
clear all;
a=input('Enter any desired row vector in []:');
n=length(a);
for ii=1:n
fprintf('ii = %d ', ii);
for jj = 1:n
fprintf(' jj = %d ', jj);
for kk=1:n
fprintf(' kk = %d ', kk);
A(ii,jj)= a(ii)*a(jj)*a(kk);
end
end
end
A
plot(A)
I keep getting these errors:
Line 1: Invalid syntax at 'Enter any desired row vector in []:'. Possibly, a ), } , or ] is missing.
Line 1: Parse error at ')' : usage might be invalid MATLAB syntax
! Line 1: The function return value 'a' might be unset.
Solution
ANSWER:
To convert a piece of code into a function to which parameters can be passed the folowing is the
syntax to be used.
function func_name(variable) where func_name is the name of the function we intend to keep
and variable is the variable to be passed to the function.
2. function assignment(a)
clear all;
n=length(a);
for ii=1:n
fprintf('ii = %d ', ii);
for jj = 1:n
fprintf(' jj = %d ', jj);
for kk=1:n
fprintf(' kk = %d ', kk);
A(ii,jj)= a(ii)*a(jj)*a(kk);
end
end
end
plot(A);
The above change has to be incorporated.