MATLAB error: “previously appeared to be used as a

2020-05-06 12:55发布

问题:

I want to create a function called E7stats, which performs a simple statistical analysis on the scores of the first midterm, contained in a csv file. The function takes one string input, filename, which is the name of the csv file, and returns one output, a 1⇥2 structure array S , both of whose two entries contain four fields mean, std d ev, max, and min, which are the mean, standard deviation, maximum value and minimum value of the electronic and paper based midterm scores. The function also creates two histograms of the two midterm 1 scores with 30 equally-sized bins. The Scores of electronic and paper based midterm 1 are stored in the first and second columns in the CSV

My problem is that I get the error:

"mean" previously appeared to be used as a function or command, conflicting with its use here as the name of a variable. A possible cause of this error is that you forgot to initialize the variable, or you have initialized it implicitly using load or eval.

and I know WHY i get the error but I don't know how to fix it because as stated above my variables need to be named mean,min, max. any suggestions welcome. thanks!

function S= E7stats(filename)
filename='grades_E7MT1.csv';
S=csvread(filename,1,0);
stddev = std(S)
mean= mean(S)
min= min(S)
max= max(S)

I asked my instructor:

we have to display contents in the structure array or should it just return the structure array along with '1x2 struct array with fields: mean stddev max and he replied: the structure is the output of the function. you don't need to display it in the command window. Only make sure it has the correct fields and values.

so now i'm really confused on what my function should output? sorry for all the confusion!!

回答1:

Don't use the function name mean() as the name of your variable. Call it myMean or something like that.

Or alternatively, you could create a structure that has field names such as mean, min, and max:

d.mean = mean();
d.min  = min();