2015-05-12 81 views
0
M = round(csvread('noob.csv')) 

save projectDAT.dat M -ascii 
load projectDAT.dat 


mat = (projectDAT) 
sum_of_rows(mat) 
plotthegraph 

这是我的主要脚本。我有一个excel文件,它在matlab中打开了一个20 x 20的矩阵。现在我不得不在这个主题中调用一个函数,它将为我找到一行中的元素的总和并将它们放入列向量中。这里是我的功能:通过调用函数绘制图形

function sumRow = sum_of_rows(mat) 
[m n] = size(mat); 

sumRow = zeros(m,1); 
for i = 1:m; 
    for j = 1:n; 
     sumRow(i) = sumRow(i) + mat(i,j); 
    end 
end 
vec = sumRow; 
end 

我需要绘制使用此列向量的线图。我应该从主脚本中调用一个函数。该函数应该能够接受来自sum_of_rows函数的输入。我试图这样做:

function plotthegraph(~) 

% Application for plotting the height of students 
choice = menu('Choose the type of graph', 'Plot the data using a line plot',   'Plot the data using a bar plot'); 
if choice == 1 
    plot_line(sum_of_rows) 
y = sum_of_rows 
x = 1:length(y) 
plot(x,y) 
title('Bar graph') 
xlabel('Number of characters') 
ylabel('Number of grades') 

elseif choice == 2 
    plot_bar(sum_of_columns) 
end 

虽然它不工作。有人可以帮助我,我真的很感激它。谢谢。

回答

0

你可以做以下摆脱sum_of_rows功能:

sumRow = sum(mat,2); 

第二个参数告知总和添加在矩阵横行所有列,让你每一行的总和

要绘制这个向量,你需要将它作为输入传递给你的函数。此外,如果您不指定变量,matlab会为您处理x值,正如您在定义x时所做的那样。你的函数应该是这样的:

function plotthegraph(sum_of_rows) 

% Application for plotting the height of students 
prompt='Type 1 for line plot, 2 for bar'; 
choice=input(prompt) 
if choice == 1 
    plot(sum_of_rows) 
    title('Line graph') 
    xlabel('Number of characters') 
    ylabel('Number of grades') 
elseif choice == 2 
    bar(sum_of_rows) 
end 
end 

所以你必须调用这个函数通过行的总和:

plotthegraph(sumRow) 
+1

谢谢你这么多 – Logan

+0

嘿。你的代码工作得很好。但是当我关闭matlab并再次打开它以再次尝试代码时,它会一直说: ???未定义的函数或变量'sumRow'。 – Logan

+0

加载project.dat再次发出sumRow = sum(mat,2);.当matlab关闭时,环境被清除 – brodoll