MATLAB Answers

For Loop iteratio record results

7 views (last 30 days)
Thiago Kalife
Thiago Kalife on 14 Oct 2016
Commented: dpb on 16 Oct 2016
Hello everyone! I have a piece of code that runs through a 46x46 excel spreadsheet and a for loop that associates each column to a vector. However, it only records the last step, ad not all the columns as individual vectors. How should I proceed to create the 46 individual column vectors?
Full_Matrix = xlsread('Table_try');
[numrow,numcol]=size(Full_Matrix);
for column=1:numcol
Student_name_column = Full_Matrix(:,column)
column=column+1;
end

Answers (3)

dpb
dpb on 14 Oct 2016
Edited: dpb on 16 Oct 2016
You've already got all the columns as individual vectors --
Full_Matrix(:,n)
where n is the column of interest. The power in Matlab is to be had by using array syntax as much as possible, not in creating a whole bunch of individual variables. Surely each of the 46 columns isn't a StudentName but a particular response for a group of students? What do you want to do with the data now that you've read it...go on to that, don't try to create more variables needlessly.
In fact, it looks from the coding by using Student_name_column as a variable name you're falling into the trap of trying to create a series of variables with the same base name but _1, _2, ... as suffixes. This is abadidea (tm) as outlined in the FAQ <How [to] create variables A1 A2 ....A10?>
  2 Comments
dpb
dpb on 14 Oct 2016
You can compare whatever to whatever far simpler by referencing column indices rather than actual full variable names...or, at worst see the dynamic structure fieldnames or, if you've a recent release, perhaps the table class would suit better than an array.
What are the data and what is/are the comparisons you wish to make and I'm sure better solutions will be forthcoming than 'poofing' variables into the workspace.

Sign in to comment.


Roche de Guzman
Roche de Guzman on 14 Oct 2016
Try this using the eval function to create new variable names:
Full_Matrix = xlsread('Table_try');
[numrow,numcol]=size(Full_Matrix);
for column=1:numcol
eval(['Student_name_' num2str(column) ' = Full_Matrix(:,column)']);
end
  1 Comment
dpb
dpb on 14 Oct 2016
YEEECH! :(
This is abadidea (tm) as outlined in the FAQ linked to above.

Sign in to comment.


Image Analyst
Image Analyst on 16 Oct 2016
As dpb already said, you already have the data (I think), and making 46 separate, differently named variables is a bad idea (even though it's possible like the other poster show).
If the spreadsheet has names (text strings) and numbers, and you're expecting to get both of them out, then you'll have to use xlsread() like this:
[numbers, studentNames, rawData] = xlsread(filename);
Numbers will be in the first array. Strings will be in the second array, but may not be synced up with the numbers as far as row,column location goes. rawData fill have both in there with no syncing problem.
  1 Comment
dpb
dpb on 16 Oct 2016
Indeed, it's unfortunate that many times especially new users post questions on how to do the above or very similar and aren't willing to hear the truth--they want to know how to do, specifically, what they're trying to do; not hear that there are other techniques to solve their problem that are much better and easier to implement. They have a given idea and are generally just bound and determined to proceed in that direction.
If OP would just come back and give a little more info, undoubtedly the solution to their dilemma would be forthcoming quite quickly and be far, far simpler than the myriad of troubles forthcoming if try the eval route...

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by