未定义的函数或变量matlab cell2mat?

时间:2015-10-12 23:46:12

标签: matlab variables

当我加载我的函数时,我创建了一个新文件,它无法识别我的变量。我正在尝试使用cell2mat将我的单元格数组转换为矩阵。那么我需要做些什么来识别变量呢?图片也在底部

file1
%Name function
function [name, hw, mt, fe]=enter_grades(number)
%inputs are scores outputs are there name and scores on hw mt fe
%asks for name and scores and stores them
ca = cell(number,4);
for i = 1:number
name = input('Enter names  of students! : ', 's');
hw = input('Enter Homework grade 0-100! : ', 's');
mt = input('Enter Midterm Exam grade 0-100! : ', 's');
fe = input('Enter Final Exam grade 0-100! : ', 's');
ca(i,:)={name hw mt fe};
end
save('enter_grades.mat', 'ca');
end
% this is used to call function
%[name, hw, mt, fe]=enter_grades(2)

file2

%Name function
function [avg, fg]=calc_stats(data)
%inputs is data from enter_grades outputs are there name and scores on hw mt fe
%creat an array that store all the grades and calculate the final grade
data=load('enter_grades.mat','ca')
a=cell2mat(ca)

end

file1 file2

我现在改变了a = cell2mat(data.ca)。

Error using cat
Dimensions of matrices being concatenated are not consistent.

Error in cell2mat (line 78)
        m = cat(1,m{:});

Error in calc_stats (line 6)
a=cell2mat(data.ca);

1 个答案:

答案 0 :(得分:1)

使用load with an output argument时,要加载的请求变量将作为字段存储在输出结构中。所以你的代码应该是:

data=load('enter_grades.mat','ca');
a=cell2mat(data.ca);

我还注意到这是使用Matlab load时的最佳做法。引用blogs for MathWorks

的人
  

不要" poof"变量到任何工作空间。翻译,不使用没有左侧的负载;避免使用eval,evalin和assignin。

增加的重点是我的。

所以赞不绝口。 ......荣誉!