Parse a cell array of strings without looping

I've got a nx1 cell array of strings that correspond to spatial coordinates. Here's an example where n=4,
'122.8233,216.1323,671.8740'
'122.8233,216.1323,671.8740'
'122.8233,216.1323,671.8740'
'122.8233,216.1323,671.8740'
Each row of the cell array is a string containing x,y, and z coordinates separated by a comma. I need to parse each string to separate the coordinates, convert the coordinates to numbers, do an operation on them, then put them back in their original format. Is there a way to do this without looping through each row of the cell array? Any tips or insight would be greatly appreciated.
Regards, Justin

댓글 수: 6

per isakson
per isakson 2012년 10월 11일
Why are these strings in a cell array? Did they originate in a file?
Yes. They are read in using the textscan() function.
per isakson
per isakson 2012년 10월 11일
편집: per isakson 2012년 10월 11일
Why not parse the data with textscan when reading? If you need the cell array of strings read twice with textscan. When the file is in the system cache it is faster to parse with textscan( fid, ...) than to parse the cell array of strings.
The file has stuff in a format other than what is listed above. The text file actually looks like this (but much longer of course):
head
39 :M
63 :N
U Knot Vector
0
0
0
0
0.0167
0.0333
0.05
.
.
.
Control Points
122.8233,216.1323,671.8740
122.8233,216.1323,671.8740
122.8233,216.1323,671.8740
122.8233,216.1323,671.8740
.
.
.
chest_surface
50 :M
53 :N
U Knot Vector
.
.
.
My strategy is to read in the whole file with textscan() then parse it after I have the whole thing read in. I suppose I could parse everything while reading in. This will still require me looping through each line and checking if its a line that I want to mess with right? The overall goal is to read in the file, change all the 'Control Points', then write out a new file. Is this possible without looping?
Thanks for your insight.
this is another question
per isakson
per isakson 2012년 10월 11일
편집: per isakson 2012년 10월 11일
With 'Headerlines' and 'N' of textscan it would still be possible to read and parse in one step. However, you need to find out the values of the two first.

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

 채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2012년 10월 11일
편집: Azzi Abdelmalek 2012년 10월 11일

2 개 추천

clear
A={'122.8233,216.1323,671.8740'
'122.8233,216.1323,671.8740'
'122.8233,216.1323,671.8740'
'122.8233,216.1323,671.8740'}
B=cellfun(@(x) regexp(x,',','split'),A,'uni',false)
out=cell2mat(cellfun(@(x) [cellfun(@(y) str2num(y),x)],B,'uni',false))
%or easier
out=str2num(cell2mat(A))

추가 답변 (2개)

Matt Fig
Matt Fig 2012년 10월 11일
편집: Matt Fig 2012년 10월 11일

0 개 추천

C = {'122.8233,216.1323,671.8740'
'122.8233,216.1323,671.8740'
'122.8233,216.1323,671.8740'
'122.8233,216.1323,671.8740'};
% Extract the numbers:
D = cellfun(@str2num,C,'Un',0);
% Do the 'operation' on the coords with cellfun:
E = cellfun(@(x) x+[1 2 3],D,'Un',0)
% Put them back in original format.
F = cellfun(@(x) sprintf('%.4f,',x),E,'Un',0)
Note that the step from D to E is ambiguous given your description, but you get the idea.
per isakson
per isakson 2012년 10월 11일
편집: per isakson 2012년 10월 11일

0 개 추천

On possibility for your first step - if the file is not huge:
str = {
'122.8233,216.1323,671.8740'
'122.8233,216.1323,671.8740'
'122.8233,216.1323,671.8740'
'122.8233,216.1323,671.8740'
};
M = str2num( char(str) );
or I didn't get it.

댓글 수: 3

My file is pretty big (> 1 million lines). This works pretty well despite the file size! Any ideas on how to convert M back to str after I perform the desired operation?
per isakson
per isakson 2012년 10월 11일
편집: per isakson 2017년 2월 9일
cellstr( num2str( M, '%f,%f,%f\n' ) )
However, write the numerical array directly to the file with fprintf in a loop I guess that fast enough. Otherwise, transpose the numerical array and write with fprintf, but only if profile showed that it could be worth the trouble.
Perfect. Thanks everyone for your help. I didn't know I could only accept one answer or I would have accepted everyone's.
Justin

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

카테고리

도움말 센터File Exchange에서 Data Type Conversion에 대해 자세히 알아보기

제품

질문:

2012년 10월 11일

편집:

2017년 2월 9일

Community Treasure Hunt

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

Start Hunting!

Translated by