-1

I have the following in MATLAB:

data1 = xlsread('C:\Desktop\example.xlsx','PKA1');
data2 = xlsread('C:\Desktop\example.xlsx','PKA2');

All the way up to 24.

How can I create a loop to do this for me?

This would create 24 variables: data1, data2, data3, etc. all corresponding to PKA1, PKA2, PKA3, etc.

How can I create a loop that reads in data1, data2, without writing them all out?

Jack
  • 307
  • 2
  • 13

2 Answers2

3

Don't create dynamic variables. Read here why. Use an array if the content of each sheet is same. Otherwise use a cell array. Use num2str to convert loop variable to string and concatenate it with the common string PKA.

k = cell(1,24);    %Pre-allocation
for k=1:24
   data{k} = xlsread('C:\Desktop\example.xlsx',['PKA', num2str(k)]);
end
Sardar Usama
  • 19,536
  • 9
  • 36
  • 58
  • If I do it like this, how can I read data in certain variables? Previously I had: t = data1(1,:); Is there a quick way to create say, t1, t2 which corresponds to the first column in data for all 24 cases? – Jack Jul 17 '17 at 17:05
  • What if I want to read in the first column of each array to t1, t2 etc? can that be done with a loop as well? – Jack Jul 17 '17 at 17:07
  • I think you meant data{1}(:,1), but how do I read that as a loop as well? If I want to create data1 = data{1}(:,1), but all the way up to 24 – Jack Jul 17 '17 at 17:09
  • The reason I ask all this, is because I want to take the 1st and 4th column from each set, and plot it on the same figure. Is there an easy way to do that? – Jack Jul 17 '17 at 17:11
  • As already pointed out to you, please don't create dynamic variables like data1, data2 ,.... it is a very bad programming practice – Sardar Usama Jul 17 '17 at 17:11
  • How can I plot (x,y) from each one on same plot where x is first column and y is 4th column from each matrix? – Jack Jul 17 '17 at 17:11
  • `plot(data{k}(:,1) , data{k}(:,4))` with [`hold on`](https://www.mathworks.com/help/matlab/ref/hold.html) – Sardar Usama Jul 17 '17 at 17:12
  • But I want to plot all 24 of them. This just plots one of them – Jack Jul 17 '17 at 17:14
  • You know loops, right? Please don't keep extending your question! If you have a new question, ask it by clicking [`Ask Question`](https://stackoverflow.com/questions/ask) . Make sure you present your code in the form of a [MCVE] – Sardar Usama Jul 17 '17 at 17:15
  • I tried loops earlier with the plot function, when I was declaring the variables dynamically and it did not work – Jack Jul 17 '17 at 17:16
  • I think I got it. for i=1:24 plot(data{i}(:,1) , data{i}(:,4)) hold on end – Jack Jul 17 '17 at 17:17
-1

As @Sardar said You should not use eval to proceed the works. However If you want, you can using eval likes the following:

 for idx = 1:24
     varName = ['data', num2str(idx)]; // concatenate the string
     columnName = ['PKA', num2str(idx)];
     xlsReadFunc = ['xlsread(''C:\Desktop\example.xlsx'',''',columnName,''''];
     finalExp = [varName,'=', xlsReadFun];
     eval(finalExp);
 end
OmG
  • 18,337
  • 10
  • 57
  • 90