Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

Question about making a function file with vectors and variables

조회 수: 1 (최근 30일)
Portgas Ace
Portgas Ace 2012년 9월 20일
마감: MATLAB Answer Bot 2021년 8월 20일
Question deleted by James. Partially restored by Matt Fig:
a part of my program has syntax that looks like this,
for n=1:1:A1 if (Z(n)<3 | Z(n)>9 | Z(n)~=Z(n)) disp('One or more of the element/s is/are not included.'); return;
elseif ~isnumeric(Z(n)) disp('There is error') return; end
so if i enter:
function_name ([3 4 A], [4 5 6])
matlab says: Undefined function or variable 'A'.
  댓글 수: 2
Jan
Jan 2012년 9월 20일
Why do you delete your question? Then the work of the once, who wanted to help you, is lost. Not friendly.
Matt Fig
Matt Fig 2012년 9월 21일
OP:
a part of my program has syntax that looks like this,
for n=1:1:A1 if (Z(n)<3 | Z(n)>9 | Z(n)~=Z(n)) disp('One or more of the element/s is/are not included.'); return;
elseif ~isnumeric(Z(n)) disp('There is error') return; end
so if i enter:
function_name ([3 4 A], [4 5 6])
matlab says: Undefined function or variable 'A'.

답변 (4개)

Wayne King
Wayne King 2012년 9월 20일
편집: Wayne King 2012년 9월 20일
MATLAB will do that on its own.
>> clear A % in case A is in the workspace
>> a=[1 2 A 4]
The MATLAB error message will be:
Undefined function or variable 'A'.
  댓글 수: 1
Portgas Ace
Portgas Ace 2012년 9월 20일
but im using a function file. im only required to input a vector with only numbers as value and if i entered a non-number, i should show an error. so ex, a = [1 2 A 4]; there should be an error saying ive entered a wrong input. something like that. :(

Wayne King
Wayne King 2012년 9월 20일
If A is not a variable in the workspace, then inputting it into a function will cause an error
function sumout = testfunction(input1 )
sumout = sum(input1);
end
If you call that function with
>>sumout = testfunction([1 2 3 A 4]);
throws an error. You can always use isnumeric() if you want
if ~isnumeric(input)
error( )
end
  댓글 수: 1
Portgas Ace
Portgas Ace 2012년 9월 20일
편집: Portgas Ace 2012년 9월 20일
hope this solves my problem. thank you! :D

Jan
Jan 2012년 9월 20일
After A = 1; a = [1,2,A,4]; there is no method to detect, if a has been created using numbers, functions or other variables. When A is e.g. a character array, it is converted to double implicitly:
A = 'Z';
a = [1, 2, A, 4] % ==> [1, 2, 90, 4]
Here A is converted to the type DOUBLE and the ASCII value 90 is stored. Afterwards a is a double vector as all its elements have the same type.
I think, you have a different problem. Perhaps you want to test the class of A before inserting into the vector.

Portgas Ace
Portgas Ace 2012년 9월 20일
a part of my program has syntax that looks like this,
for n=1:1:A1 if (Z(n)<3 | Z(n)>9 | Z(n)~=Z(n)) disp('One or more of the element/s is/are not included.'); return;
elseif ~isnumeric(Z(n)) disp('There is error') return; end
so if i enter:
function_name ([3 4 A], [4 5 6])
matlab says: Undefined function or variable 'A'.

이 질문은 마감되었습니다.

Community Treasure Hunt

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

Start Hunting!

Translated by