For Loop iteratio record results

3 次查看(过去 30 天)
Thiago Kalife
Thiago Kalife 2016-10-14
评论: dpb 2016-10-16
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

回答(3 个)

dpb
dpb 2016-10-14
编辑:dpb 2016-10-16
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 个评论
Thiago Kalife
Thiago Kalife 2016-10-14
I want to have separate vectors so I can compare them to each row in the original table and I don't want to manually assign each collumn to one student.
dpb
dpb 2016-10-14
编辑:dpb 2016-10-15
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.

请先登录,再进行评论。


Roche de Guzman
Roche de Guzman 2016-10-14
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 个评论
dpb
dpb 2016-10-14
编辑:dpb 2016-10-16
YEEECH! :(
This is abadidea (tm) as outlined in the FAQ linked to above.

请先登录,再进行评论。


Image Analyst
Image Analyst 2016-10-16
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 个评论
dpb
dpb 2016-10-16
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...

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Interactive Control and Callbacks 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by