How can I use xlsread with changing indices?

Hello, I would like to read just some cells from my Excel document doing something like this:
for i=1:7:22
j=mod(i,7);
x(j,:)=xlsread('filename','Ai:Ai')
end
In order to have inside the vector x the following elements: x=[A1 A8 A15 A22]. How can I do this? Thank you for the help and don't esitate to ask if I have been not so clear.

 Accepted Answer

dpb
dpb on 6 Jul 2018
Edited: dpb on 6 Jul 2018
x(j,:)=xlsread('filename',sprintf('A%d:A%d',i,i));
but I'd recommend against using it that way; xlsread opens and closes the file for every access plus with the COM overhead it is noticeably slow so to do this will be quite sluggish. Instead read the data and keep only what is needed, will be quite a bit quicker, especially if your list grows to more elements than just four or five..
ix=1:7:22; % the desired list
x=xlsread('filename',sprintf('A%d:A%d',ix(1),ix(end))); % first thru last
x=x(ix); % keep the desired subset

More Answers (0)

Community Treasure Hunt

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

Start Hunting!