Undefined function or variable in MATLAB

7 ビュー (過去 30 日間)
Joshua
Joshua 2013 年 7 月 19 日
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 日
What is e=[Hydrogen Helium] intended to do?
  8 件のコメント
per isakson
per isakson 2013 年 7 月 19 日
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 日
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 日
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.
  1 件のコメント
Joshua
Joshua 2013 年 7 月 19 日
Thanks it worked.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeQuantum Mechanics についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by