Split cell each 13 characters
2 views (last 30 days)
Show older comments
Hello everyone,
I'm creating a code that reads a txt file, stores it as a matrix and keeps only a range of value. This first part is done, code below. So i have a matrix of about 32x1
I would like to split my values so each one goes into a cell, so i have a 32x6 matrix. Thing is, the only thing that can split the values is their lenght, as they are all made of 13 characters (exemple right below)
-0.198181E-10-0.100000E+01-0.173525E-10 0.445807E-16-0.142817E-10 0.122740E-11
0.100000E+01-0.333333E+00-0.198181E-10-0.100000E+01-0.198181E-10-0.173525E-10
-0.167526E-10-0.100000E+01-0.195269E-10 0.325840E-11-0.171677E-10 0.139359E-11
0.100000E+01-0.333333E+00-0.167526E-10-0.100000E+01-0.195269E-10-0.167526E-10
Is there a way to split this at each 13 characters ?
Thanks for any help
Matt
file = 'G:\\Matthieu\\DangVan\\QuarterCube\\Job1_job1.t19';
A = textscan(fopen(file),'%s','delimiter', '\n');
starter='=beg=52300 (Element Integration Point Values) ';
ender='=beg=52401 (Nodal Results) ';
stressloc1=find(strcmp(A{1,1},starter))+1 %find start of my range
stressloc2=find(strcmp(A{1,1},ender))-2 %find end of my range
stressvector = A{1,1}([stressloc1:stressloc2],1); % Extract rows
0 Comments
Accepted Answer
Stephen23
on 17 Dec 2021
The better approach is to use READMATRIX or READTABLE with the fixed-wdith importing option:
fnm = 'test.txt';
opt = detectImportOptions(fnm, 'FileType','fixedwidth', 'VariableWidths',13*ones(1,6), 'ReadVariableNames',false);
mat = readmatrix(fnm,opt)
More Answers (2)
Jan
on 17 Dec 2021
It would be usful, if you post your input data and the wanted output. Currently I find the information
- matrix of about 32x1
- each one goes into a cell
- so i have a 32x6 matrix
- Example (which neither a 31x1 matrix, nor a cell, nor a 32x6 matrix nor is it clear, if the shown code produces it):
-0.198181E-10-0.100000E+01-0.173525E-10 0.445807E-16-0.142817E-10 0.122740E-11
0.100000E+01-0.333333E+00-0.198181E-10-0.100000E+01-0.198181E-10-0.173525E-10
-0.167526E-10-0.100000E+01-0.195269E-10 0.325840E-11-0.171677E-10 0.139359E-11
0.100000E+01-0.333333E+00-0.167526E-10-0.100000E+01-0.195269E-10-0.167526E-10
5. Some code which produces something, but what?
So what exactly should be split to what?
A bold guess:
C = '-0.198181E-10-0.100000E+01-0.173525E-10 0.445807E-16-0.142817E-10 0.122740E-11';
D = sscanf(C, '%13E');
format longg
disp(D)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!