Undefined function or variable in MATLAB

I have this function saved as element.m Whenever I call on it I say element(Hydrogen) I am trying to get it to display the name, symbol, atomic number, category, ... But it keeps saying Undefined function or variable 'Hydrogen' I don't understand why its doing this.
function [Name, Symbol, Atomic_Number, Category, Proton, Neutron, Electron]=element(e)
%Inputs:
%e - 'Hydrogen' ;
%Outputs:
%Name
%Symbol
%Atomic_Number
%Category
%Proton
%Neutron
%Electron
e=[Hydrogen Helium]
if e=='Hydrogen'
Name='Hydrogen';
Symbol='H';
Atomic_Number='1';
Category='Nonmetal';
Proton='1';
Neutron='';
Electron='1';
elseif e==Helium
Name='Helium'
Symbol='H'
Atomic_Number='1'
Category='Noble Gas'
Proton='4'
Neutron='2'
Electron='4'
end
end

 채택된 답변

per isakson
per isakson 2013년 7월 19일

0 개 추천

What is e=[Hydrogen Helium] intended to do?

댓글 수: 8

Joshua
Joshua 2013년 7월 19일
I meant to delete e=[Hydrogen Helium]
per isakson
per isakson 2013년 7월 19일
???
At that point in the code neither Hydrogen nor Helium exists.
[A,B] concatenates the variables A and B
Joshua
Joshua 2013년 7월 19일
how would i define hydrogen and helium if i wanted to get the name, category, symbol ...
Remove the line
e=[Hydrogen Helium]
and replace
e=='Hydrogen'
by
strcmp( e, 'Hydrogen' )
or
all( e=='Hydrogen' )
Then try to run the function
per isakson
per isakson 2013년 7월 19일
Have a look at these links on debugging in Matlab
Joshua
Joshua 2013년 7월 19일
I did what you said with both strcmp and all, i tried both of them, when i wrote element('Hydrogen') in the command window it said ans=Hydrogen, but i dont understand why its not giving me the other information as well.
You have to make a call like
[Name,Symbol,Atomic_Number,Category,Proton,Neutron,Electron]=element('Hydrogen')
Joshua
Joshua 2013년 7월 19일
Thanks it worked.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Azzi Abdelmalek
Azzi Abdelmalek 2013년 7월 19일

0 개 추천

The variable Hydrogene is not defined in your code, how Matlab will recognize it?

댓글 수: 2

Joshua
Joshua 2013년 7월 19일
편집: Joshua 2013년 7월 19일
I am kind of new to MATLAB, how exactly would i define it if I wanted it to give me the Name, Category, Symbol ...
Joshua
Joshua 2013년 7월 19일
Thanks it worked.

댓글을 달려면 로그인하십시오.

Jason Schneider
Jason Schneider 2013년 7월 19일

0 개 추천

Remove
e = [Hydrogen Helium]
Use
strcmp as previously suggested.
Make sure when you call the function it is of the form:
[a b c d e f g] = element('Hydrogen')
You need to define where the rest of the output arguments will be assigned since they are internal variables to the function.
It seems this was also mentioned but the input argument needs to be a string or else Matlab will look for a variable that doesn't exist.

카테고리

도움말 센터File Exchange에서 Quantum Mechanics에 대해 자세히 알아보기

제품

질문:

2013년 7월 19일

Community Treasure Hunt

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

Start Hunting!

Translated by