Sorting data extracted from the name of a group of files

조회 수: 1 (최근 30일)
Peter
Peter 2014년 12월 29일
댓글: Peter 2014년 12월 29일
Hello,
I have a group of files named like this:
files=[{' x+0.000mm_y_+0.000mm_z_+0.000mm.dat_ '}
{' x+5.000mm_y_+10.000mm_z_+0.000mm.dat_ '
{' x+10.000mm_y_+20.000mm_z_+30.000mm.dat_ '}
{' x+15.000mm_y_+40.000mm_z_+0.000mm.dat_ '}
{' x+20.000mm_y_+60.000mm_z_+0.000mm.dat_ '}]
I need to have the X and Y coordinates this way and order:
0 0
5 10
10 20
15 40
20 60
When i use:
coord = regexp(files,'\d+(\.\d+)?','match')
I oibtain the three coordenates but HOWEVER they are not displayed in the same order as the name of the files, and it is a very important problem, to solve.
I need somehow to have the 3 coordinates in the same order as the files whose they are extracted.
Do you know how to doi it? Why do they follow another order? Is this about the 'match'??
Thanks

채택된 답변

Jan
Jan 2014년 12월 29일
files = {' x+0.000mm_y_+0.000mm_z_+0.000mm.dat_ ', ...
' x+5.000mm_y_+10.000mm_z_+0.000mm.dat_ ', ...
' x+10.000mm_y_+20.000mm_z_+30.000mm.dat_ ', ...
' x+15.000mm_y_+40.000mm_z_+0.000mm.dat_ ', ...
' x+20.000mm_y_+60.000mm_z_+0.000mm.dat_ '}
coordC = regexp(files,'\d+(\.\d+)?','match');
coord = cat(1, coords{:});
Reply:
'0.000' '0.000' '0.000'
'5.000' '10.000' '0.000'
'10.000' '20.000' '30.000'
'15.000' '40.000' '0.000'
'20.000' '60.000' '0.000'
This exactly the same order as the list of file names. Therefore I do not understand the question. Please post, what you observe.
  댓글 수: 1
Peter
Peter 2014년 12월 29일
Yes, exactly.
Both answers worked properly.
Thank you very much for the the help and for the explanation.

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

추가 답변 (2개)

Azzi Abdelmalek
Azzi Abdelmalek 2014년 12월 29일
coord=cell2mat(cellfun(@str2double,regexp(files,'(?<=(x\+)|(y_\+))\d+(\.\d+)?', 'match'),'un',0))

Geoff Hayes
Geoff Hayes 2014년 12월 29일
Peter - what order are they appearing in? If I do the following
files=[{' x+0.000mm_y_+0.000mm_z_+0.000mm.dat_ '}
{' x+5.000mm_y_+10.000mm_z_+0.000mm.dat_ '}
{' x+10.000mm_y_+20.000mm_z_+30.000mm.dat_ '}
{' x+15.000mm_y_+40.000mm_z_+0.000mm.dat_ '}
{' x+20.000mm_y_+60.000mm_z_+0.000mm.dat_ '}];
coord = regexp(files,'\d+(\.\d+)?','match');
xyzCoords = cell2mat(cellfun(@(x)str2num(char(x))',coord,'UniformOutput',false));
then
xyzCoords =
0 0 0
5 10 0
10 20 30
15 40 0
20 60 0
In the above line of code, since coord is a cell array, we use the cellfun to apply a function to each cell in the array. This function, @(x)str2num(char(x))', converts each element to a string (via char) then to a numeric array (3x1, via str2num), which is then transposed to get a row. As the result of cellfun is another cell array of numeric data, we just convert it from the cell array to a matrix via cell2mat.
  댓글 수: 1
Peter
Peter 2014년 12월 29일
Thank you very much Geoff Hayes, your code worked perfectlly

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

카테고리

Help CenterFile Exchange에서 Cell Arrays에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by