Removing a character from a table (that's within a struct)

7 visualizaciones (últimos 30 días)
Hi,
I have a .mat file with the name faceDatasetGroundTruth.mat (a struct) that contains a table (called 'faceDataset') with two columns.
Each value in the second column starts and ends with a ' character, which I'd like to remove so that I'm only left with [95,71,226,313] in the first row, for example.
Thanks in advance.
  1 comentario
C B
C B el 7 de Oct. de 2021
Editada: C B el 8 de Oct. de 2021
Is there any specific reason for doing so ?
because when i run following it shows me that first char is '[' and not '''
>> faceDataset.face{1}(1)
ans =
'['
>> faceDataset
faceDataset =
3×2 table
filename face
________ ___________
'ab' '[1 2 2 4]'
'bc' '[1 2 2 4]'
'cd' '[1 2 2 4]'
>> faceDataset.face{1}
ans =
'[1 2 2 4]'
>> faceDataset.face{1}(1)
ans =
'['
>> faceDataset.face{1}(end)
ans =
']'

Iniciar sesión para comentar.

Respuesta aceptada

Stephen23
Stephen23 el 8 de Oct. de 2021
Editada: Stephen23 el 8 de Oct. de 2021
As far as I understand, you want to convert one column/variable of the table from cell array of character vectors to numeric. This is easy with convertvars:
S = load('faceDatasetGroundTruth.mat')
S = struct with fields:
faceDataset: [202599×2 table]
T = S.faceDataset
T = 202599×2 table
imageFilename face _________________________ _____________________ {'faceImages/000001.jpg'} {'[95,71,226,313]' } {'faceImages/000002.jpg'} {'[72,94,221,306]' } {'faceImages/000003.jpg'} {'[216,59,91,126]' } {'faceImages/000004.jpg'} {'[622,257,564,781]'} {'faceImages/000005.jpg'} {'[236,109,120,166]'} {'faceImages/000006.jpg'} {'[146,67,182,252]' } {'faceImages/000007.jpg'} {'[64,93,211,292]' } {'faceImages/000008.jpg'} {'[212,89,218,302]' } {'faceImages/000009.jpg'} {'[600,274,343,475]'} {'faceImages/000010.jpg'} {'[113,110,211,292]'} {'faceImages/000011.jpg'} {'[166,68,125,173]' } {'faceImages/000012.jpg'} {'[102,31,104,144]' } {'faceImages/000013.jpg'} {'[89,132,247,342]' } {'faceImages/000014.jpg'} {'[110,122,234,324]'} {'faceImages/000015.jpg'} {'[93,86,190,263]' } {'faceImages/000016.jpg'} {'[39,89,283,392]' }
F = @(c)sscanf([c{:}],'[%f,%f,%f,%f]',[4,Inf]).';
T = convertvars(T,'face',F);
T = convertvars(T,'imageFilename',@string) % optional, a bit slow.
T = 202599×2 table
imageFilename face _______________________ ________________________ "faceImages/000001.jpg" 95 71 226 313 "faceImages/000002.jpg" 72 94 221 306 "faceImages/000003.jpg" 216 59 91 126 "faceImages/000004.jpg" 622 257 564 781 "faceImages/000005.jpg" 236 109 120 166 "faceImages/000006.jpg" 146 67 182 252 "faceImages/000007.jpg" 64 93 211 292 "faceImages/000008.jpg" 212 89 218 302 "faceImages/000009.jpg" 600 274 343 475 "faceImages/000010.jpg" 113 110 211 292 "faceImages/000011.jpg" 166 68 125 173 "faceImages/000012.jpg" 102 31 104 144 "faceImages/000013.jpg" 89 132 247 342 "faceImages/000014.jpg" 110 122 234 324 "faceImages/000015.jpg" 93 86 190 263 "faceImages/000016.jpg" 39 89 283 392
  7 comentarios
Stephen23
Stephen23 el 12 de Oct. de 2021
"Unfortunately it results in this error"
Yes, because you are not supplying that tool with the data in the format that it requires. You are using the tool, which means that you have its documentation, which means that you can read its documentation and find out what are the specific needs for the data.
The error message also gives some hints, perhaps something like this might be what that tool needs:
S = load('faceDatasetGroundTruth.mat')
S = struct with fields:
faceDataset: [202599×2 table]
T = S.faceDataset
T = 202599×2 table
imageFilename face _________________________ _____________________ {'faceImages/000001.jpg'} {'[95,71,226,313]' } {'faceImages/000002.jpg'} {'[72,94,221,306]' } {'faceImages/000003.jpg'} {'[216,59,91,126]' } {'faceImages/000004.jpg'} {'[622,257,564,781]'} {'faceImages/000005.jpg'} {'[236,109,120,166]'} {'faceImages/000006.jpg'} {'[146,67,182,252]' } {'faceImages/000007.jpg'} {'[64,93,211,292]' } {'faceImages/000008.jpg'} {'[212,89,218,302]' } {'faceImages/000009.jpg'} {'[600,274,343,475]'} {'faceImages/000010.jpg'} {'[113,110,211,292]'} {'faceImages/000011.jpg'} {'[166,68,125,173]' } {'faceImages/000012.jpg'} {'[102,31,104,144]' } {'faceImages/000013.jpg'} {'[89,132,247,342]' } {'faceImages/000014.jpg'} {'[110,122,234,324]'} {'faceImages/000015.jpg'} {'[93,86,190,263]' } {'faceImages/000016.jpg'} {'[39,89,283,392]' }
F = @(c)num2cell(sscanf([c{:}],'[%f,%f,%f,%f]',[4,Inf]).',2);
T = convertvars(T,'face',F)
T = 202599×2 table
imageFilename face _________________________ ___________________ {'faceImages/000001.jpg'} {[ 95 71 226 313]} {'faceImages/000002.jpg'} {[ 72 94 221 306]} {'faceImages/000003.jpg'} {[ 216 59 91 126]} {'faceImages/000004.jpg'} {[622 257 564 781]} {'faceImages/000005.jpg'} {[236 109 120 166]} {'faceImages/000006.jpg'} {[ 146 67 182 252]} {'faceImages/000007.jpg'} {[ 64 93 211 292]} {'faceImages/000008.jpg'} {[ 212 89 218 302]} {'faceImages/000009.jpg'} {[600 274 343 475]} {'faceImages/000010.jpg'} {[113 110 211 292]} {'faceImages/000011.jpg'} {[ 166 68 125 173]} {'faceImages/000012.jpg'} {[ 102 31 104 144]} {'faceImages/000013.jpg'} {[ 89 132 247 342]} {'faceImages/000014.jpg'} {[110 122 234 324]} {'faceImages/000015.jpg'} {[ 93 86 190 263]} {'faceImages/000016.jpg'} {[ 39 89 283 392]}
Yonathan Zarkovian
Yonathan Zarkovian el 13 de Oct. de 2021
Thank you, this method seems to work.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Image Processing and Computer Vision en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by