How to extract data from table variable names?

조회 수: 8 (최근 30일)
Daniel Bridges
Daniel Bridges 2018년 3월 14일
편집: Stephen23 2018년 3월 15일
MIM Maestro puts volume data inside the table variable names:
Rectum (4)(Volume: 57.77) Bladder (5)(Volume: 139.40)
How do we extract this volume data?

답변 (2개)

Daniel Bridges
Daniel Bridges 2018년 3월 14일
This method works, but I suspect there is a more elegant solution.
% get list of variable names
opts = detectImportOptions(filepath, 'NumHeaderLines', 1);
% we find the column containing the rectum volume
RectumSearching = regexp(opts.VariableNames,'Rectum_');
for loop = 1:numel(opts.VariableNames)
if RectumSearching{loop} == 1
index = loop;
end
end
% extract volume from said string
volume = extractAfter(opts.VariableNames{index},'Volume_');
volume = str2num(strrep(volume(1:end-1),'_','.'))
Result:
volume =
57.7700

Stephen23
Stephen23 2018년 3월 14일
편집: Stephen23 2018년 3월 14일
>> C = {'Rectum (4)(Volume: 57.77)','Bladder (5)(Volume: 139.40)'};
>> str2double(regexp(C,'\d+(\.\d+)?(?=\)$)','once','match'))
ans =
57.770 139.400
Or to require the preceding 'Volume' substring:
str2double(regexp(C,'(?<=Volume: )\d+(\.\d+)?(?=\)$)','once','match'))
  댓글 수: 6
Daniel Bridges
Daniel Bridges 2018년 3월 15일
Okay.
Stephen23
Stephen23 2018년 3월 15일
편집: Stephen23 2018년 3월 15일
@Daniel Bridges: thank you for uploading that .mat file. The char vectors in that cell array have a different format to the one that you showed in your question, apparently with the parentheses and decimal point replaced by underscores. You can easily process this by first replacing the underscore with period characters:
>> S = load('varnames.mat');
>> C = strrep(S.vn,'_','.');
>> str2double(regexp(C,'\d+(\.\d+)?(?=\.$)','once','match'))
ans =
Columns 1 through 7
NaN 18494 27.49 4.9 57.77 139.4 479.8
Columns 8 through 14
1.08 29.15 95.8 97.26 72.3 93.45 80.69
Columns 15 through 16
83.06 32.39
As an alternative you could skip using detectImportOptions (which I guess makes these character replacements) and read the header lines using fgetl. This line could then be trivially process by a similar regular expression to the one I showed you.

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

Community Treasure Hunt

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

Start Hunting!

Translated by