How to create class from .txt

2 views (last 30 days)
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
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
EDWARD IJAU PELIAS POG
EDWARD IJAU PELIAS POG on 14 Mar 2019
as an example, i have below matrix in my .txt.
[1, 2, 3,
4, 5, 6,
7, 8, 9]
then i want make a class on each row.
Class A = 1,2,3
Class B = 4, 5, 6
Class C = 7, 8, 9
does any way to do it?

Sign in to comment.

Accepted Answer

Steven Lord
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
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.

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!