Combine multiple columns into one column

28 vues (au cours des 30 derniers jours)
Ali Osman Gökcan
Ali Osman Gökcan le 12 Déc 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...

Réponse acceptée

Voss
Voss le 13 Déc 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'})
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 commentaires
Voss
Voss le 13 Déc 2022
You're welcome! Have a nice day.

Connectez-vous pour commenter.

Plus de réponses (1)

Jiri Hajek
Jiri Hajek le 12 Déc 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 commentaires
Ali Osman Gökcan
Ali Osman Gökcan le 13 Déc 2022
I will take into account what you said. Thank you for being with me in my journey of program development with Matlab.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Data Type Conversion dans Help Center et File Exchange

Produits


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by