Matlab iteration through excel rows
이전 댓글 표시
Hello,
I have a problem for which I'm not finding a solution for now, so I decided to ask you.
I have a given Excel File with several columns and Rows. I only need the Columns where the names are defined and their values are stored.
After knowing which columns are the right ones, I want to iterate through every row of these columns to extract the values. Can you may help me? Here is my Code for now.
filename = 'example.xls';
excelSheet = 'example';
columnFirst = {'A1','B1','C1','D1','E1','F1','G1','H1','I1','J1','K1','L1'};
columnName = 'Name';
columnNameLetter = '';
columnDefault = 'Default';
columnDefaultLetter = '';
n = 13;
for i=1:n
[~,text] = xlsread(filename,excelSheet,columnFirst{i});
if(strcmp(columnName,text))
columnNameLetter = columnFirst{i};
end
if(strcmp(columnDefault,text))
columnDefaultLetter = columnFirst{i};
end
end
I'm getting the result that the Names are in column G and their values in column L
Now how can I iterate through all the rows to extract the values?
댓글 수: 2
Guillaume
2019년 4월 10일
Bit of advice
columnFirst = {'A1','B1','C1','D1','E1','F1','G1','H1','I1','J1','K1','L1'};
n = 13;
for i = 1:n
So, whenever you change the list of column, you also have to change n to match the number of elements, AND you have to make sure that you've counted them correctly. Forget to change n, or put the wrong value and you've got a bug.
Why not let matlab determine the number of elements and avoid the risk altogether. It's also less work:
columnFirst = {'A1','B1','C1','D1','E1','F1','G1','H1','I1','J1','K1','L1'};
for i = 1:numel(columnFirst)
You should never hardcode the size of inputs, always let matlab find it out for you.
Florian Azemi
2019년 4월 10일
채택된 답변
추가 답변 (2개)
Alex Mcaulley
2019년 4월 10일
Try this, is that you want?
filename = 'example.xls';
excelSheet = 'example';
columnName = 'Name';
columnDefault = 'Default';
[~,~,raw] = xlsread(filename,excelSheet);
try
names = raw(2:end,contains(raw(1,:),columnName));
catch
names = [];
end
try
defaults = raw(2:end,contains(raw(1,:),columnDefault));
catch
defaults = [];
end
댓글 수: 4
Florian Azemi
2019년 4월 10일
Alex Mcaulley
2019년 4월 10일
Can you upload your excel? It is difficult to know what is happening. What is the result of running this?
contains(raw(1,:),columnName)
contains(raw(1,:),columnDefault)
I'm not seeing the points of the try...catch statements. The lines in the try can never error* anyway so the catch will never be invoked.
Also note that contains is not the same as strcmp. contains(x, 'Name') returns true if x is for example 'a rose by a any other Name', strcmp only returns true for 'Name'.
So, overall the code should be:
filename = 'example.xls';
excelSheet = 'example';
columnName = 'Name';
columnDefault = 'Default';
[~, ~, raw] = xlsread(filename,excelSheet);
name = raw(2:end, strcmp(raw(1, :), columnName));
defaults = raw(2:end, strcmp(raw(1, :), columnDefault));
*edit: well, they can error if the 1st row does not contain text, but in that case, you'd be better off knowing than just ignoring the problem.
Alex Mcaulley
2019년 4월 10일
편집: Alex Mcaulley
2019년 4월 10일
Florian Azemi
2019년 4월 10일
편집: Florian Azemi
2019년 4월 10일
카테고리
도움말 센터 및 File Exchange에서 Spreadsheets에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!