Parse a cell array of strings without looping

9 vues (au cours des 30 derniers jours)
Justin Solomon
Justin Solomon le 11 Oct 2012
Modifié(e) : per isakson le 9 Fév 2017
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 commentaires
Azzi Abdelmalek
Azzi Abdelmalek le 11 Oct 2012
this is another question
per isakson
per isakson le 11 Oct 2012
Modifié(e) : per isakson le 11 Oct 2012
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.

Connectez-vous pour commenter.

Réponse acceptée

Azzi Abdelmalek
Azzi Abdelmalek le 11 Oct 2012
Modifié(e) : Azzi Abdelmalek le 11 Oct 2012
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))
  1 commentaire
Sogand Karbalaieali
Sogand Karbalaieali le 8 Fév 2017
Thank you for : out=str2num(cell2mat(A))

Connectez-vous pour commenter.

Plus de réponses (2)

Matt Fig
Matt Fig le 11 Oct 2012
Modifié(e) : Matt Fig le 11 Oct 2012
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 le 11 Oct 2012
Modifié(e) : per isakson le 11 Oct 2012
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 commentaires
per isakson
per isakson le 11 Oct 2012
Modifié(e) : per isakson le 9 Fév 2017
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.
Justin Solomon
Justin Solomon le 11 Oct 2012
Perfect. Thanks everyone for your help. I didn't know I could only accept one answer or I would have accepted everyone's.
Justin

Connectez-vous pour commenter.

Catégories

En savoir plus sur Data Import and Export dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by