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

조회 수: 13 (최근 30일)
Yonathan Zarkovian
Yonathan Zarkovian 2021년 10월 7일
댓글: Yonathan Zarkovian 2021년 10월 13일
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]}
Yonathan Zarkovian
Yonathan Zarkovian 2021년 10월 13일
Thank you, this method seems to work.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Quantization, Projection, and Pruning에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by