【发布时间】:2016-10-18 12:31:00
【问题描述】:
我正在从文件中读取输入并将其导入到我的数据中以在 Matlab 中运行:
parts = strread(tline,'%s','delimiter',';')
employee(i).name = parts(1);
employee(i).salary= str2double(parts(2));
然后我尝试打印出来:
for i = 1:3
fprintf('salary: %.2f\n',employee(i).salary);
fprintf('employee name: %s\n',employee(i).name);
end
工资打印没有问题。但是对于变量“name”,它给出了一个错误:
Error using fprintf
Function is not defined for 'cell' inputs.
fprintf('employee name: %s\n',employee(i).name);
我找了一些其他的例子:
How do I access structure fields dynamically?
Matlab Error: Function is not defined for 'cell' inputs
How do i define a structure in Matlab
但是没有什么可以解决这种情况,只有字符串不起作用。
我没有明确地将数据声明为结构,即在代码中没有包含“结构”一词的地方,但 Matlab 显然自动将其理解为“结构数组”。
这里可能缺少什么提示?
高度赞赏所有cmets!
【问题讨论】:
-
鉴于错误消息,我认为
employee(i).name是cell而不是char。class(employee(i).name)返回什么? -
我认为
employee(i).name是一个单元格。所以看看你能不能做str = employee(i).name{1}然后用str。 -
返回:ans = cell
-
所以...这意味着
parts可能是一个cell数组,当将数据拉入employee时,您需要access data in a cell array -
@ParagS.Chandakkar - 谢谢!有用。无论如何避免使用 str 的需要?
标签: matlab struct cell matlab-struct