Combine multiple columns into one column
44 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Ali Osman Gökcan
il 12 Dic 2022
Commentato: Ali Osman Gökcan
il 13 Dic 2022
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 Commenti
Risposta accettata
Voss
il 13 Dic 2022
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'})
% replace of the existing "person" variable with its rounded values:
PersontrainingData.person = cellfun(@round,PersontrainingData.person,'UniformOutput',false)
2 Commenti
Più risposte (1)
Jiri Hajek
il 12 Dic 2022
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 Commenti
Jiri Hajek
il 13 Dic 2022
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))
Vedere anche
Categorie
Scopri di più su Matrices and Arrays in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!