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

7 次查看(过去 30 天)
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 个评论
C B
C B 2021-10-7
编辑:C B 2021-10-8
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 =
']'

请先登录,再进行评论。

采纳的回答

Stephen23
Stephen23 2021-10-8
编辑:Stephen23 2021-10-8
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 个评论
Stephen23
Stephen23 2021-10-12
"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]}

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Image Processing and Computer Vision 的更多信息

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by