For Loop iteratio record results
조회 수: 5 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
답변 (3개)
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
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
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
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
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 Center 및 File Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!