Split cell each 13 characters
    2 visualizzazioni (ultimi 30 giorni)
  
       Mostra commenti meno recenti
    
    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 
0 Commenti
Risposta accettata
  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)
5 Commenti
  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
format short E
pi
format long G
pi
format bank
pi
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.
Più risposte (2)
  Jan
      
      
 il 17 Dic 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)
  Matthieu Aoun
 il 17 Dic 2021
        
      Modificato: Matthieu Aoun
 il 17 Dic 2021
  
      
      2 Commenti
  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?
Vedere anche
Categorie
				Scopri di più su Spreadsheets in Help Center e File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!