Split cell each 13 characters

2 views (last 30 days)
Matthieu Aoun
Matthieu Aoun on 17 Dec 2021
Commented: Matthieu Aoun on 17 Dec 2021
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

Accepted Answer

Stephen23
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)
mat = 4×6
-1.9818e-11 -1 -1.7352e-11 4.4581e-17 -1.4282e-11 1.2274e-12 1 -0.33333 -1.9818e-11 -1 -1.9818e-11 -1.7352e-11 -1.6753e-11 -1 -1.9527e-11 3.2584e-12 -1.7168e-11 1.3936e-12 1 -0.33333 -1.6753e-11 -1 -1.9527e-11 -1.6753e-11
  5 Comments
Matthieu Aoun
Matthieu Aoun on 17 Dec 2021
Thanks a lot Stephen. I have no particular reason to keep the same format, except elegancy.
Your code works like a charm, thanks a lot. Also, in the future the code has to be used for matrixes of half a million rows, so I think it's a godd solution.
It's not the end of this program which has a lot of other purposes to be developed, but here I share the v1 of the code.
file = 'G:\\Matthieu\\DangVan\\QQCUBE\\yo_job1.t19';
A = textscan(fopen(file),'%s','delimiter', '\n'); %import file
starter='=beg=52300 (Element Integration Point Values) '; %start of the matrix of interest
ender='=beg=52401 (Nodal Results) '; %end of the matrix of interest
stressloc1=find(strcmp(A{1,1},starter))+1 %location of the beginning of the matrix
stressloc2=find(strcmp(A{1,1},ender))-2 %location of the end of the matrix
range=stressloc2-stressloc1+1 %N of rows
opt = detectImportOptions(file, 'FileType','fixedwidth', 'VariableWidths',13*ones(1,6), 'ReadVariableNames',false);
opt.DataLines = [stressloc1,stressloc2];
mat = readmatrix(file,opt) %thanks Stephen :)
for i=1:range %future of the code, taking values to create new matrixes and do some calculations and plot some graphs...
row=sprintf('%i',i)
mac_stress=sprintf('mac_stress%i',i);
mic_stress=sprintf('mic_stress%i',i);
dev_stress=sprintf('dev_stress%i',i);
% n_stress=
% s_stress
mac_stress=[mat(row,3) mat(row,4) mat(row,5)]
end
Thanks again,
Matt

Sign in to comment.

More Answers (2)

Jan
Jan on 17 Dec 2021
It would be usful, if you post your input data and the wanted output. Currently I find the information
  1. matrix of about 32x1
  2. each one goes into a cell
  3. so i have a 32x6 matrix
  4. 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)
-1.98181e-11 -1 -1.73525e-11 4.45807e-17 -1.42817e-11 1.2274e-12
  1 Comment
Matthieu Aoun
Matthieu Aoun on 17 Dec 2021
Hey, thanks for the reply. I didn't know I wasn't clear. I did a post were I ass precision.
Your guess wasn't far from what I would like.

Sign in to comment.


Matthieu Aoun
Matthieu Aoun on 17 Dec 2021
Edited: Matthieu Aoun on 17 Dec 2021
Hey guys, thanks a lot for the quick answers. It seems that I wasn't clear enough.
So, basically, I created a program that finds a matrix in the attached file, which comes from a finite element software.
With this code i now have a matrix (32 rows by 1 column) that looks like this in Matlab, called stressvector in my program :
-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
...
...
But, i would like it to look like that, each value should be stored in 1 cell to get a 32x6 matrix (@Jan) :
-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
you will notice that each value has 13 charaters, that's the only kind of delimiter I found.
I hope it is clearer.
Matt
  2 Comments
Matthieu Aoun
Matthieu Aoun on 17 Dec 2021
I posted it for Jan, because he had some questions. Your code works fine and i'll go from there!

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by