Extracting the numbers from file names and listing them in a column
1 次查看(过去 30 天)
显示 更早的评论
Hi all ,
I have png files like this ;
Rpma26siatBz 9.500000 Bx 0.000000mT WWait 2.000000Sec.Bzat9.5mT83.png,
Rpma26siatBz 9.500000 Bx 100.000000mT WWait 1.000000Sec.Bzat9.5mT85.png
and so on
I want to extract numbers 0.000000 from first file ,100.000000 from second file etc .and put in one column.
And again extract the numbers 2.000000 from first file,1.000000 from second file etc. and put in another column.
I tried writing the code like this but it didnt work ( I mean I got NAN)? It would be great if you share any ideas for doing it .
thank you
clear all;
close all;
clc;
listI=dir('*.png');
B=[];
for i=1:length(listI);
n=listI(i);
A1 =n(1:end-26);
A2 = n(1:end-19);
x1= str2double(A1);
x2=str2double(A2);
Bx=[B;(x1),(x2)];
end
0 个评论
采纳的回答
Stephen23
2021-1-21
编辑:Stephen23
2021-1-21
C = {'Rpma26siatBz 9.500000 Bx 0.000000mT WWait 2.000000Sec.Bzat9.5mT83.png',...
'Rpma26siatBz 9.500000 Bx 100.000000mT WWait 1.000000Sec.Bzat9.5mT85.png'};
M = sscanf([C{:}],'Rpma%*dsiatBz%*f Bx%fmT WWait%fSec.Bzat%*fmT%*d.png',[2,Inf]).'
Or
W = regexp(C,'(?<=\s)\d+\.\d+(?=[A-Za-z])','match');
M = str2double(vertcat(W{:}))
5 个评论
Stephen23
2021-1-22
"..it does not work for these png files"
The first name has extra character/s which do not match the format string:
%...SecBzat.... 1st name
% ^ not in format string!
%...SeBzat.... 2nd and 3rd names
%...SeBzat.... format string
sscanf will stop parsing the string as soon as it reaches that 'c' character, because it does not match the format string and so does not know how to handle it. You can perform more flexible matching like this:
C = {'Rpma26siatz -9.600000 Bx -100.000000mT Waait 600.000000SecBzat-9.6mTDAQ02.png',...
'Rpma26siatz -9.600000 Bx 40.000000mT Waait 2000.000000SeBzat-9.6mTDAQ45.png',...
'Rpma26siatz -9.600000 Bx 130.000000mT Waait 1500.000000SeBzat-9.6mTDAQ27.png'};
M = sscanf([C{:}],'%*[ A-Za-z.]%f',[6,Inf]).'
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Type Conversion 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!