Extracting N amount of rows from Excel
Show older comments
1. Read the Excel file & determine the dimensions of the data set 2. Compute and report the sum of each of the rows of the data, and the total sum of all values in the matrix. Do not use MATLAB built‐in function sum.
clear
clc
data = xlsread('HW_17.xlsx');
%this is what I want it to do for an N amount of rows
L = length(data);
%extract rows
R1 = data(1,:);
R2 = data(2,:);
R3 = data(3,:);
%this code is not working
for i=1:size(data,1)
R(i) = data(i,:)
end
Answers (1)
Geoff Hayes
on 11 Nov 2014
J - re-read the first question. It is asking you to determine the dimensions of the dataset. Consider doing this using the size which will give you the number of rows and columns in data.
For your middle block of code where you extract the first three rows from data. Why do you feel that this is necessary, especially as your code doesn't reference these variables once initialized?
As for your final block of code, you state that this code is not working. What do you mean by this? It seems that you are just extracting each row and copying it into a undeclared vector R, which probably generates an error like
In an assignment A(I) = B, the number of elements in B and I must be the same.
What are you trying to do here? The second part of the is asking you to report the sum of each row. So the iterating over each row is correct, but then what? If you were to do this by hand, using a calculator say, you would start at the first element of the first row and add to that the second element of the first row, then the third element, then the fourth, etc. until all elements of the first row have been added together. Which is equivalent to adding all the columns in the first row together. So to do this, you need two loops - the outer loop to iterate over each row (like which you have), and an inner loop to iterate over each column. Try coding this up and see what happens!
Categories
Find more on Data Import from MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!