Info
This question is closed. Reopen it to edit or answer.
undefind variableserror in the command window
1 view (last 30 days)
Show older comments
I am new to matlab, and I am trying to write a simple function that changes between two numbers in a row of a matrix, but it doesn't work for some reason. This the function I wrote:
function [] = swibt( row1,num1,num2 )
%UNTITLED3 Summary of this function goes here
% Detailed explanation goes here
wibt1=x(row1,num1);
x(row1,num1)=x(row1,num2);
x(row1,num2)=wibt1;
end
when i type this in the command window it gives me an error alert and does not work:
>> instein(1)
x =
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
ans =
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
>> swibt(1,1,2)
Undefined function or variable "x".
Error in swibt (line 4)
wibt1=x(row1,num1);
Can anyone help me, because I can't figure it out?
0 Comments
Answers (1)
Image Analyst
on 14 Jul 2014
You need to pass x into the function when you call it:
function test()
x = rand(30,50);
newX = swibt(1,2,3,x);
function x = swibt( row1,num1,num2, x )
% Swap numbers
wibt1=x(row1,num1);
x(row1,num1)=x(row1,num2);
x(row1,num2)=wibt1;
end
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!