With this following code:
raw = fileread('testdata.txt');
data = regexp(raw,'[+-]?\d+\.?\d*', 'match');
g=(cellfun(@str2num,data))
It results in g to look like: [1003 ...... 1009...1016....1020] , where its a 1x242 array.
I want to split into multiple rows to have the data look like this instead:
[1003, ,........;
1009 , .......;
1016..........;
1020.........; ]
Could anyone provide some help in how to accomplish this?

 Respuesta aceptada

Star Strider
Star Strider el 10 de Abr. de 2021
Try this:
fidi = fopen('testdata.txt', 'rt');
C = textscan(fidi, '%f', 'Delimiter',' ', 'MultipleDelimsAsOne',1); % Cell Array Output
fclose(fidi);
V = [C{:}]; % Extract Numerical Vector
segments = [find(log10(V) >= 3); numel(V)+1]; % Determine Segment Limits
seglen = diff(segments); % Segment Lengths (For ‘mat2cell’)
Out = mat2cell(V, seglen, 1); % Cell Array Of Segments
Row1 = Out{1}(1:7).' % Display First 7 Elements
Row2 = Out{2}(1:7).' % Display First 7 Elements
Row3 = Out{3}(1:7).' % Display First 7 Elements
Row4 = Out{4}(1:7).' % Display First 7 Elements
I have no idea how robust it would be to other such files. It works with this one.

Más respuestas (0)

Categorías

Productos

Versión

R2020b

Preguntada:

el 10 de Abr. de 2021

Respondida:

el 10 de Abr. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by