2013-11-15 110 views
1

我有一个函数,它在一个人无效的字段名称

function person = prompt_person() 
name = input (' Whats your name ? ' , 's') ; 
day = input (' What day of the month were you born ? ') ; 
month = input (' What month were you born ? ') ; 
year = input (' What year were you born? ') ; 
phone = input (' Whats your telephone number ? ') ; 
date_of_birth = struct ('day', day, 'month', month, 'year', year) ; 
person = struct ('name' , name, 'date_of_birth' , date_of_birth , 'phone' , phone) ; 
end 

的personalia但我不断收到错误信息“无效的字段名‘名’”,“错误消息prompt_person(线8)我不知道什么是错,因为我试图创建一个简单的小测试功能:

function [out] = tes() 
word=input('Insert word here ','s'); 
num=input('Insert number here '); 
out= struct('Number1', word, 'Number2', num); 
end 

,它工作得很好,即使它似乎是完全相同的代码,让我在toruble在第一个功能。任何想法发生了什么?

+0

尝试使用'personName'作为变量名称。 – Vuwox

+0

无法在这里重现此...您使用什么MATLAB版本? –

+0

我正在使用Matlab r2013a – user100620

回答

0

我试着将变量名更改为personName建议,我意外地发现最新错误: 结果我需要使用一个变量名,在''和文本之间没有空格。

E.g. 'PersonName'而不是'PersonName'。

0

您可能会考虑使用strtrim从字符串中去除前导和尾随白字。例如:

>> name = ' John Doe '; 
>> name = strtrim(name) 
name = 
John Doe 

如果您需要删除所有空格,请尝试strrep(name,' ','')

>> name = strrep(name,' ','') 
name = 
JohnDoe