Info

This question is closed. Reopen it to edit or answer.

undefind variableserror in the command window

1 view (last 30 days)
asaf
asaf on 14 Jul 2014
Closed: MATLAB Answer Bot on 20 Aug 2021
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?

Answers (1)

Image Analyst
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
  2 Comments
asaf
asaf on 15 Jul 2014
no it didnt work out but i think i understood the problem. another thing i heard is that i can use a matfile to store all the vriables so that all the function will be able to take information from it because the code i am writing has a lot of vriables how can it be done? thank you

Tags

Community Treasure Hunt

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

Start Hunting!