函数将文本插入到文件中。Matlab
我目前正在处理这个函数函数将文本插入到文件中。Matlab,matlab,text,compiler-errors,Matlab,Text,Compiler Errors,我目前正在处理这个函数 function[check] = store( filename, persons ) fid = fopen(filename,'w'); if exist('fid') check = true; else check = false; end for i=1:length(persons) sprintf(fid, '%s\n',serialize_person(persons(i))); end end Serialize_p
function[check] = store( filename, persons )
fid = fopen(filename,'w');
if exist('fid')
check = true;
else
check = false;
end
for i=1:length(persons)
sprintf(fid, '%s\n',serialize_person(persons(i)));
end
end
Serialize_person返回一个。
如果store函数创建一个名为'filename'的.txt文件,并将serialize_person(persons(I))放在该文本文件的第I行,那么我想要的是什么
然而,即使serialize_person本身运行良好,但当我尝试运行store函数时,会收到错误消息
Attempt to reference field of non-structure array.
Error in serialize_person (line 3)
out=sprintf ( '<%s>#%s#<%i>\n' , person.name, serialize_date( person.date_of_birth),
person.phone );
Error in store (line 14)
sprintf(fid, '%s\n',serialize_person(persons(i)));
尝试引用非结构数组的字段。
序列化_person时出错(第3行)
out=sprintf('#%s#\n',person.name,序列化日期(person.date,出生日期),
个人电话);
存储错误(第14行)
sprintf(fid,'%s\n',序列化_person(persons(i));
对什么可能是错的有任何有根据的猜测吗
function[check] = store( filename, persons )
fid = fopen(filename,'w');
if exist('fid')
check = true;
else
check = false;
end
for i=1:length(persons)
sprintf(fid, '%s\n',serialize_person(persons{i}));
end
end
这是那些被忽视的简单错误之一。person是一个结构单元,每个结构单元都可以放入序列化的person中。因此,问题的解决方案就是在for循环中将(i)改为{i}。您确定在
serialize\u person
中引用的person
变量是实际的结构吗?这个错误表明它不是。我想我现在已经修复了它,应该是序列化person(persons{I})而不是(I),因为我接受了一个结构单元。