How to separate an Excel column into two columns?
Show older comments
I have a column in an Excel sheet that I would like to separate into two columns. The numbers are separated by a comma (Ex. [1, 4]). I would like one column to contain the 1 and the other the 4.
This column could also consist of matrices...looking like {[1, 4], [3, 6]}...I would also like to separate these into two different columns; one containing the 1 and 3, the other the 4 and 6.
I would like to know how to separate a column into two columns by the comma between the two numbers...
8 Comments
Bob Thompson
on 14 Feb 2018
How does matlab interpret the excel data by default? Does it read it as an array, a string, or a double?
Lexington Stoyell
on 14 Feb 2018
Bob Thompson
on 14 Feb 2018
Assuming the values are all the same size (single digit) you could turn specific numbers in the string into doubles using str2num().
data = '[1,4]';
data1 = str2num(data(2));
data2 = str2num(data(4));
end
This method probably isn't the best simply because it doesn't react well to numbers of different sizes. You might try doing str2num on the whole string and see if it will turn it into an array for you.
There is also a command to split strings, strsplit(), that works in later versions of matlab. You can pick the comma as a delimiter and it will create multiple strings separated by the comma.
Also, another command that might work is regexp(). I believe this command is more powerful than strsplit(), and works in older versions of matlab, but I do not have a lot of personal experience with it.
Lexington Stoyell
on 14 Feb 2018
Bob Thompson
on 14 Feb 2018
If you open the cell, what type of data does it contain? If it is a string then the previous comments apply, it just simply means that you need to adjust your indexing: data{1}(2) rather than data(2). If the data is stored as a double array then it should be easy to work with the array with proper indexing.
Lexington Stoyell
on 14 Feb 2018
Bob Thompson
on 15 Feb 2018
Ok, so depending on the data type inside the cell you can go back to the original suggestion for string manipulation. You just need to have an index to make sure you are looking inside your cell.
Jan
on 20 Feb 2018
@Lexington: Your question is still not clear. Does it concern Excel or Matlab? "[1, 4] or {[1, 4], [3, 6]}" This is a substantial difference. While the first is a vector, the second is a cell, which contains two vectors. Therefore I do not have an idea about what your inputs are and what you want as output.
separate a column into two columns by the comma between the two numbers
This is not meaningful, because the vectors do not contain a comma. This comma is just inserted for the output on the screen. A vector consists of 2 elements only, not of square brackets or commas. "Columns" are something you know from Excel, but this is a Matlab forum and here a "column" is not a known object. Change your point of view to arrays, not columns.
Answers (0)
Categories
Find more on Spreadsheets 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!