How do I form a loop to perform calculations on series of variables in workspace?

Matlab novice here; I have a set of data sheets with numbers loaded into workspace named, data1, data2,...,data50.
I am hoping to extract certain columns of data for analysis from data1 to data50 in a loop e.g.
for j=1:50
A = data{j}(:,3);
B = myAnalysis(A)
The idea is to extract the 3rd column from dataj and then use it for calculations. Any help would be much appreciated!

1 Comment

While "data1" looks like a bad programming practize, "data{1}" is most likely the best way to go.

Sign in to comment.

 Accepted Answer

Firstly, have a look at this:
Naming the variables "data1, data2,...,data50" creates trouble and extra work.
"data sheets". Have you read the data from Excel?

2 Comments

Many thanks, the link was very helpful. Is there a better way to name the variables?
Well why did you get 50 differently named variables in your program in the first place? Why not just read in one at a time and then pull out the column you want into a single 2D array that grows larger with each worksheet you read in? If there any reason you need all 50 full worksheets in memory at one time? I think not - you only need one column from each and you can store all those third columns in one array.

Sign in to comment.

More Answers (2)

I assume datasheets mean "worksheets" in an Excel "workbook" - just trying to get the terminology accurate. If so, you definitely don't want to use xlsread() unless you don't mind waiting forever. How comfortable or confident are you with using, or learning, ActiveX programming? Because that's what you'll need to do this efficiently.
Apologies, data1...data50 were loaded originally each from single excel spreadsheet of data into the matlab workspace. I then saved them in one .mat file. I open the .mat file and then run my script:
A = data1(:,3); B = myAnalysis(A)
then I copy and pasted, but changing the number after 'data',
A = data2(:,3); B = myAnalysis(A)
A = data3(:,3); B = myAnalysis(A)
all the way until data50. I save the variables into .txt.
I know I need to keep learning, had do the above in the meantime to start my analysis, its silly and cumbersome but so far, has worked. It just makes life difficult if I want to adjust things.
Thanks.

Tags

Community Treasure Hunt

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

Start Hunting!