Error on line 28 of a 9 line code?

조회 수: 3 (최근 30일)
Robert
Robert 2015년 9월 7일
편집: John D'Errico 2015년 9월 7일
I have 365 text files of data, one for each day of the year. I have this code to create 12 new text files, monthly averages.
clear all
S = dir('*.txt');
N = {S.name};
for k = 1:numel(S)
A = load(N{k});
final = mean (N);
[~,name] = fileparts(N{k});
dlmwrite(sprintf('%s.txt',name),final)
end
I get the Error
Undefined function 'sum' for input arguments of type 'cell'.
Error in mean (line 28)
y = sum(x)/size(x,dim);
Error in Refining_to_Monthly (line 6)
final = mean (N);
I don't understand the error in general, but especially don't understand how there can be an error on line 28 of the code, when it is not even 28 lines long. Any help greatly appreciated.
Thank you in advance

채택된 답변

John D'Errico
John D'Errico 2015년 9월 7일
편집: John D'Errico 2015년 9월 7일
READ THE COMPLETE ERROR. Actually, start at the bottom, (the end of the error message.)
Undefined function 'sum' for input arguments of type 'cell'.
Error in mean (line 28)
y = sum(x)/size(x,dim);
Error in Refining_to_Monthly (line 6)
final = mean (N);
The error message says it found a problem in the Refining_to_Monthly function, on line 6.
There you call the function mean. In fact, a problem has not yet occurred, at least not one that causes MATLAB to get upset. It was not until line 28 of the mean function that it got upset, where the problem became one where MATLAB was confused. At that point MATLAB reports the stack, that will allow you to trace back what is happening.
So in fact, the error is NOT in line 28 of mean, but in how you called mean (in line 6 of your code.) Mean does not accept cell array arguments.

추가 답변 (1개)

Gurudatha Pai
Gurudatha Pai 2015년 9월 7일
The error is on line 28 of the function mean.
mean()
The function mean cannot work on cell arrays. You would want to call
mean(cell2mat(N))
or something like that.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by