How would I create a matrix from the following strings
Afficher commentaires plus anciens
I am trying to write a code with minimal preprocessing. I have many entries in a text file like this:
NODE{1 0 11 0 1.000000e+00 1.000000e-02 -1.000000e-02 1.500000e-03}
There are many rows of these in an Excel file. I want to try and read the Columns 1 and 6 to 8 inside the brackets, what would the best way to do this. I have tried fileread and textscan, but I haven't got anywhere because of the text NODE at the front of the brackets.
3 commentaires
Andrei Bobrov
le 26 Déc 2019
Please attach your xlsx-file here.
John D'Errico
le 26 Déc 2019
Please learn to use comments intead of answers if you are just responding to a followup question or wish to make a comment.
I've moved the file, attaching it to this comment instead.
JLV
le 26 Déc 2019
Réponse acceptée
Plus de réponses (2)
data = fileread('ThinPlateNodes.txt'); % read file(it is in text)
ext_data = regexp(data, '[^{\]]+(?=})', 'match'); % get data between {}
ext_data(1) = []; % first cell is not required so removed
num_data = zeros(length(ext_data), 8); % your complete data
for ii = 1:length(ext_data)
num_data(ii,:) = cellfun(@str2num, strsplit(cell2mat(ext_data(ii))));
end
% you can get data any colum from the "num_data"
col_1 = num_data(:,1);
col_6_to_8 = num_data(:, 6:8);
4 commentaires
JLV
le 26 Déc 2019
JLV
le 26 Déc 2019
Bhaskar R
le 27 Déc 2019
Stephen Cobeldick provided a sophisticated answer !!
I am just giving his answer according to your context
opt = {'HeaderLines',4,'CollectOutput',true};
fmt = '"TET4{%f %f %f %f %f %s %f %f %f %f %f %f}"';
[fid,msg] = fopen('NodeNosatElementsUnedited.txt','rt');
assert(fid>=3,msg)
C = textscan(fid,fmt,opt{:}); % open C in variable editor so that you can know extracted data C
fclose(fid);
NodesatElements = [C{1}(:,1),C{3}(:, 3:6)]; % this is final data
"What would be the best way to speed up the code."
- By not importing numeric data as character/strings, and then awkwardly converting it to numeric afterwards.
- By not using str2num (which hides slow eval inside).
- By not expanding the output arrays nearly half-a-million times inside a loop.
- By not using a cell array to store one numeric scalar per cell.
- By not importing any data that you do not need.
fmt = '"TET4{%f%*f%*f%*f%*f%*s%*f%*f%f%f%f%f}"'; % note the ignored fields!
opt = {'HeaderLines',4,'CollectOutput',true};
[fid,msg] = fopen('NodeNosatElementsUnedited.txt','rt');
assert(fid>=3,msg)
C = textscan(fid,fmt,opt{:});
fclose(fid);
M = C{1};
Andrei Bobrov
le 26 Déc 2019
T = readtable('Path\your\txt\file\ThinPlateNodes.txt');
T.Varend = str2double(regexp(T{:,end},'(\-)?\d+(\.\d+e\-\d+)?(?=\}$)','match','once'));
T.Var0 = str2double(regexp(T{:,1},'\d+(?=$)','match','once'));
T = T(:,[end,6:7,end-1]);
T.Properties.VariableNames = {'LABEL','x','y','z'};
Catégories
En savoir plus sur Text Files dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!