MATLAB Answers

Separating a colymn into two columns (.txt file)

2 views (last 30 days)
Ivan Mich
Ivan Mich on 24 Sep 2019
Commented: David K. on 1 Oct 2019
I would like to separate a column into two different columns from a .txt file in Matlab. I used these commands:
and the results of these commands are in the pictures. Finally, I mean that i would like to separate line 11 into two vertical columns.
Which command should I use?


Sign in to comment.

Accepted Answer

David K.
David K. on 24 Sep 2019
Edited: David K. on 24 Sep 2019
I would do it like this:
%t = your table;
dataCell = table2cell(t); % convert to cells
dataCell = dataCell(11:end); % remove unneccessary text
cellNums = cellfun(@str2num,dataCell,'UniformOutput',0); % apply str to num on all the data
output = cell2mat(cellNums); % convert to matrix with 2 columns
*Edit: fixed a few things


Show 7 older comments
David K.
David K. on 25 Sep 2019
One issue I see is that minY and maxY are only on 1 row so it creates issues with there being differently sized cells that cannot be converted to a matrix.
To fix that I would pad the cells so that they are all the same size. I am going to do that by padding each cell with zeros.
cellNums = cellfun(@(x)[x,zeros(1,8-length(x))],cellNums,'UniformOutput',0)
This will make it so that every cell has 8 columns and can then be put into the cell2mat function.
Ivan Mich
Ivan Mich on 1 Oct 2019
Ok, one last question please. If i would want to "pick" from this .txt file (see image up) only the number of 9th line which command would help me to make it?
David K.
David K. on 1 Oct 2019
Personally, I'm not great at reading in and out files all that well. I think there are some commands that let you set where you are reading in from and how how much to read in. But since the matrix is all ready read into matlab I would just use the matrix index to choose the value you need.
If you really only want to get one value then I would check out fseek and other f- commands.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!