How to extract data from table variable names?

2 次查看(过去 30 天)
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 个评论
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 3-D Volumetric Image Processing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by