How to create class from .txt
2 views (last 30 days)
Show older comments
EDWARD IJAU PELIAS POG
on 13 Mar 2019
Commented: EDWARD IJAU PELIAS POG
on 15 Mar 2019
Hi.. i have 10 X 10001 matrix in .txt file. i want to make a class for each row of the matrix. how do i do that?
4 Comments
Guillaume
on 13 Mar 2019
Again, what do you call "a class"?
It's a term that can have several meaning in matlab, none of which seem to fit what you're asking. I suggest that you give an example of input with the desired output, using valid matlab syntax
Accepted Answer
Steven Lord
on 14 Mar 2019
While you can create individual variables from the rows or columns of a matrix, doing so is strongly discouraged. If your matrix was stored in the variable M use M(1, :) wherever you would have used "Class A".
If you want to refer to the rows and/or columns by name rather than by number, consider storing your data in a table.
M = [1 2 3; 4 5 6; 7 8 9];
T = array2table(M, 'RowNames', {'A', 'B','C'})
row2OfMAsTable = T('B', :) % Parentheses extracts a subtable
row2OfM = T{'B', :} % Curly braces extracts the contents of that subtable
To give your columns of M names when they are converted into variables in the table T by array2table, use 'VariableNames' instead of (or in addition to, if you want both row and variable names) 'RowNames' in the array2table call.
3 Comments
Guillaume
on 14 Mar 2019
how do i access it? by naming all of the rows A,B,C..J?
By not naming the row and simply using indexing
M(1, :) to access the first row
M(999, :) to access the 999th row
So much simpler than having to figure out what name the 999th row would be.
More Answers (0)
See Also
Categories
Find more on Whos in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!