Split cell each 13 characters

2 visualizzazioni (ultimi 30 giorni)
Matthieu Aoun
Matthieu Aoun il 17 Dic 2021
Commentato: Matthieu Aoun il 17 Dic 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

Risposta accettata

Stephen23
Stephen23 il 17 Dic 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 Commenti
Stephen23
Stephen23 il 17 Dic 2021
Modificato: Stephen23 il 17 Dic 2021
"But how is it possible to keep the same number format,i.e scientific ?"
Numeric data classes do not store any formatting information at all, so what you are requesting is like asking for the number to be blue or red or bold. Formatting is purely an artefact of how data are displayed. It has NOTHING to do with how any numeric data are stored in your computer. Those numbers are imported as DOUBLE type with their full precision.
You can trivially change how the data are displayed in he command window by changing the FORMAT. Here is exactly the same numeric value displayed using different formats:
format short
pi
ans = 3.1416
format short E
pi
ans =
3.1416e+00
format long G
pi
ans =
3.14159265358979
format bank
pi
ans =
3.14
Or you could write your own FPRINTF commands.
Alternatively you could convert the data to text via SPRINTF, COMPOSE, etc.
If you still believe that you need to import the data exactly as it looks in the file then you will have to import it as text, which will just make working with your (then no-longer) numeric data slower and more complex.
Matthieu Aoun
Matthieu Aoun il 17 Dic 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

Accedi per commentare.

Più risposte (2)

Jan
Jan il 17 Dic 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 Commento
Matthieu Aoun
Matthieu Aoun il 17 Dic 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.

Accedi per commentare.


Matthieu Aoun
Matthieu Aoun il 17 Dic 2021
Modificato: Matthieu Aoun il 17 Dic 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 Commenti
Stephen23
Stephen23 il 17 Dic 2021
Modificato: Stephen23 il 17 Dic 2021
"It seems that I wasn't clear enough"
It was quite clear, which is why once you uploaded your file two hours ago I showed you here how to import it as a fixed-width text file. The output is a 32x6 numeric array, which seems to match what you are expecting.
Is there a particular reason why the code I gave in my comment does not work for you?
Matthieu Aoun
Matthieu Aoun il 17 Dic 2021
I posted it for Jan, because he had some questions. Your code works fine and i'll go from there!

Accedi per commentare.

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by