我目前正在从事这项工作
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返回<1x(length)char>。
如果要使用store函数创建一个名为“文件名”的.txt文件,并将serialize_person(persons(i))放在该文本文件的第i行上,我想要什么。
但是,即使serialize_person本身运行正常,当我尝试运行存储功能时,也会收到错误消息
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)));
关于什么可能有问题的任何有根据的猜测?
最佳答案
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中。因此,解决问题的方法只是在for循环中将(i)更改为{i}。
关于matlab - 将文本插入文件的功能。 Matlab的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20136050/