Combine multiple columns into one column (2024)

37 views (last 30 days)

Show older comments

Ali Osman Gökcan on 12 Dec 2022

  • Link

    Direct link to this question

    https://matlabcentral.mathworks.com/matlabcentral/answers/1876362-combine-multiple-columns-into-one-column

  • Link

    Direct link to this question

    https://matlabcentral.mathworks.com/matlabcentral/answers/1876362-combine-multiple-columns-into-one-column

Commented: Ali Osman Gökcan on 13 Dec 2022

Accepted Answer: Voss

Open in MATLAB Online

Combine multiple columns into one column (2)

Hi. There is a table with the content in the image. I used the following code to convert the data in the 2nd column to integers:

matrix = PersontrainingData{:,:};

second_column = cell2mat(matrix(:, 2));

second_column = round (second_column);

Now I want to put together the first column in the picture (imagefilename) and the 4 columns I got after rounding, but I can't. I made various experiments and got results, but I do not share it so as not to prolong the topic. Thank you for your interest and have a nice day...

0 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

Sign in to answer this question.

Accepted Answer

Voss on 13 Dec 2022

  • Link

    Direct link to this answer

    https://matlabcentral.mathworks.com/matlabcentral/answers/1876362-combine-multiple-columns-into-one-column#answer_1126667

Open in MATLAB Online

If you want to replace the original (unrounded) second column with a rounded version, try this:

% a table similar to yours:

PersontrainingData = table({'file1.png';'file2.png'},{[630 195 30 38];[626.9255 194.9444 29.7777 37.7592]}, ...

'VariableNames',{'imageFilename' 'person'})

PersontrainingData = 2×2 table

imageFilename person _____________ _____________________________________ {'file1.png'} {[ 630 195 30 38]} {'file2.png'} {[626.9255 194.9444 29.7777 37.7592]}

% replace of the existing "person" variable with its rounded values:

PersontrainingData.person = cellfun(@round,PersontrainingData.person,'UniformOutput',false)

PersontrainingData = 2×2 table

imageFilename person _____________ _________________ {'file1.png'} {[630 195 30 38]} {'file2.png'} {[627 195 30 38]}

2 Comments

Show NoneHide None

Ali Osman Gökcan on 13 Dec 2022

Direct link to this comment

https://matlabcentral.mathworks.com/matlabcentral/answers/1876362-combine-multiple-columns-into-one-column#comment_2514772

  • Link

    Direct link to this comment

    https://matlabcentral.mathworks.com/matlabcentral/answers/1876362-combine-multiple-columns-into-one-column#comment_2514772

Thank you so much. Finally happened. :)

Thank you to everyone who tried to help.

Voss on 13 Dec 2022

Direct link to this comment

https://matlabcentral.mathworks.com/matlabcentral/answers/1876362-combine-multiple-columns-into-one-column#comment_2514777

  • Link

    Direct link to this comment

    https://matlabcentral.mathworks.com/matlabcentral/answers/1876362-combine-multiple-columns-into-one-column#comment_2514777

You're welcome! Have a nice day.

Sign in to comment.

More Answers (1)

Jiri Hajek on 12 Dec 2022

  • Link

    Direct link to this answer

    https://matlabcentral.mathworks.com/matlabcentral/answers/1876362-combine-multiple-columns-into-one-column#answer_1126017

  • Link

    Direct link to this answer

    https://matlabcentral.mathworks.com/matlabcentral/answers/1876362-combine-multiple-columns-into-one-column#answer_1126017

Open in MATLAB Online

Hi, I believe you are almost there... To make the discussion simple, let's consider just one of the filenames. You need to make sure you have a variable (e.g. "fileName") containing the vector of characters (char array). Then you can separate parts of the filename using the fileparts function like this:

[filepath,name,ext] = fileparts(fileName)

Now you will be able to e.g. append your integer (myInteger) to the filename, for which you must turn it into char using num2str function like this:

newFileName = [name,num2str(myInteger)]

Hope this answers your question.

3 Comments

Show 1 older commentHide 1 older comment

Ali Osman Gökcan on 12 Dec 2022

Direct link to this comment

https://matlabcentral.mathworks.com/matlabcentral/answers/1876362-combine-multiple-columns-into-one-column#comment_2513752

  • Link

    Direct link to this comment

    https://matlabcentral.mathworks.com/matlabcentral/answers/1876362-combine-multiple-columns-into-one-column#comment_2513752

Thank you very much for the super quick reply but I couldn't understand why we are using the fileparts command. :)

I'm trying to convert the table in the screenshot I shared in my question to the situation in the screenshot I shared in my current answer.

Combine multiple columns into one column (8)

Jiri Hajek on 13 Dec 2022

Direct link to this comment

https://matlabcentral.mathworks.com/matlabcentral/answers/1876362-combine-multiple-columns-into-one-column#comment_2514757

  • Link

    Direct link to this comment

    https://matlabcentral.mathworks.com/matlabcentral/answers/1876362-combine-multiple-columns-into-one-column#comment_2514757

Open in MATLAB Online

OK, perhaps it wasn't the expected answer after all, but please look at the title you gave to it... It's always good to ask a question precisely...Unfortunately, you did not provide a sample of the data, so I have to guess based on your code snippet that the second column was originally a column of cells, containing several columns of numeric values. Now that you tried to assign your "second column" variable, which is however a four-column matrix into a single column, that is not allowed. You can either convert rows of your data back into cells to make them a single column again, that way Matlab will accept it (here's how to do that: https://www.mathworks.com/matlabcentral/answers/386673-combine-each-row-of-matrix-into-a-single-vector-within-a-cell-array)

But tables also enable to put a matrix into a single column, but you need to assign it anew:

myTable = table(['f';'g';'h'],rand(3))

Ali Osman Gökcan on 13 Dec 2022

Direct link to this comment

https://matlabcentral.mathworks.com/matlabcentral/answers/1876362-combine-multiple-columns-into-one-column#comment_2514817

  • Link

    Direct link to this comment

    https://matlabcentral.mathworks.com/matlabcentral/answers/1876362-combine-multiple-columns-into-one-column#comment_2514817

I will take into account what you said. Thank you for being with me in my journey of program development with Matlab.

Sign in to comment.

Sign in to answer this question.

See Also

Categories

MATLABLanguage FundamentalsMatrices and Arrays

Find more on Matrices and Arrays in Help Center and File Exchange

Tags

  • matrix
  • array
  • cell array
  • arrays
  • data import
  • multiple

Products

  • MATLAB

Release

R2021b

Community Treasure Hunt

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

Start Hunting!

An Error Occurred

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.


Combine multiple columns into one column (11)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom(English)

Asia Pacific

Contact your local office

Combine multiple columns into one column (2024)

FAQs

How to combine multiple columns in Excel into one column? ›

Instructions for merging columns in Excel
  1. Highlight the columns. Open the spreadsheet you plan to work on and highlight the columns you'd like to merge. ...
  2. Open the home tab. Once you've highlighted the columns you want to merge , click on the "Home" button in the top, upper left corner of your screen. ...
  3. Select the merge icon.
Jun 24, 2022

How do I concatenate multiple columns into one column? ›

How to concatenate (combine) multiple columns into one field in Excel
  1. Use the CONCATENATE function in column D: =CONCATENATE(A1,B1,C1).
  2. In the menu bar, select Insert, Function. ...
  3. Enter A1 in the text1 field, B1 in the text2 field, and C1 in the text3 field.
  4. Click OK. ...
  5. Copy and paste for as many records as needed.

How do I merge 3 columns in Excel without losing data? ›

There are three easy ways to combine columns in your spreadsheet—Flash Fill, the ampersand (&) symbol, and the CONCAT function. Unlike merging cells, these options preserve your data and allow you to separate values with spaces and commas.

How do I insert multiple columns into one column in Excel? ›

To insert a single column: Right-click the whole column to the right of where you want to add the new column, and then select Insert Columns. To insert multiple columns: Select the same number of columns to the right of where you want to add new ones. Right-click the selection, and then select Insert Columns.

How do I group multiple columns in one column in Excel? ›

To group columns in Excel, perform these steps: Select the columns you want to group, or at least one cell in each column. On the Data tab, in the Outline group, click the Group button. Or use the Shift + Alt + Right Arrow shortcut.

How do you consolidate data from multiple columns in Excel? ›

Step-by-Step Guide to Consolidating Columns in Excel

Once the cells have been selected, click on the “Data” tab in the ribbon at the top of the Excel window. Select the “Consolidate” option from the ribbon. In the “Consolidate” dialog box, select the type of consolidation you would like to perform.

How do I move multiple columns to one column in Excel? ›

Combine data from 2 columns into 1 column
  1. Select the cell where you want to put the combined data.
  2. Type = and select the first cell you want to combine.
  3. Type & and use quotation marks with a space enclosed.
  4. Select the next cell you want to combine and press enter. An example formula might be =A2&" "&B2.

How do I put data from multiple columns into one column in sheets? ›

Once the columns are selected, go to the menu bar and click on Format, then select Merge Cells. This tool will merge the cells in the selected columns into one column. If your sheet has data in other rows, confirm whether or not you want to overwrite those cells. If you do, select "Merge All" in the popup message.

How do I CONCATENATE multiple columns in Excel? ›

Combine data using the CONCAT function
  1. Select the cell where you want to put the combined data.
  2. Type =CONCAT(.
  3. Select the cell you want to combine first. Use commas to separate the cells you are combining and use quotation marks to add spaces, commas, or other text.
  4. Close the formula with a parenthesis and press Enter.

How to merge columns in a spreadsheet? ›

  1. On your computer, open a spreadsheet in Google Sheets.
  2. Select the rows, columns, or cells to merge.
  3. At the top, click Format. Merge cells, then select how you want your cells to be merged.

How do I consolidate data from multiple columns? ›

Step-by-Step Guide to Consolidating Columns in Excel

Once the cells have been selected, click on the “Data” tab in the ribbon at the top of the Excel window. Select the “Consolidate” option from the ribbon. In the “Consolidate” dialog box, select the type of consolidation you would like to perform.

How to combine two columns in Excel and keep formatting? ›

  1. You can also use =A2&” “&B2. ...
  2. You can use paste special to do this. ...
  3. If you want to combine two columns of values, you can add them together with another Paste Special command.
  4. Once you click on OK, Excel will add the values from Column E to the values in Column D.
Aug 15, 2023

How do I combine first and last name columns in Excel? ›

To combine first and last names, use the CONCATENATE function or the ampersand (&) operator. Important: In Excel 2016, Excel Mobile, and Excel for the web, this function has been replaced with the CONCAT function.

Top Articles
Latest Posts
Article information

Author: Catherine Tremblay

Last Updated:

Views: 5892

Rating: 4.7 / 5 (47 voted)

Reviews: 86% of readers found this page helpful

Author information

Name: Catherine Tremblay

Birthday: 1999-09-23

Address: Suite 461 73643 Sherril Loaf, Dickinsonland, AZ 47941-2379

Phone: +2678139151039

Job: International Administration Supervisor

Hobby: Dowsing, Snowboarding, Rowing, Beekeeping, Calligraphy, Shooting, Air sports

Introduction: My name is Catherine Tremblay, I am a precious, perfect, tasty, enthusiastic, inexpensive, vast, kind person who loves writing and wants to share my knowledge and understanding with you.