Borrar filtros
Borrar filtros

organising data with fscanf

1 visualización (últimos 30 días)
Mesut Sahin
Mesut Sahin el 4 de Oct. de 2022
Comentada: Mesut Sahin el 6 de Oct. de 2022
file=fopen('data.txt','r');
line1=fgetl(file);
formatSpec_1 = '%d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d\n';
formatSpec_2 = '%d, %d, %d, %d, %d\n';
element_row_1 = fscanf(file, formatSpec_1, [16,inf]);
element_row_2 = fscanf(file, formatSpec_2, [5,inf]);
Hello Community,
I am trying to organise my data but I am not successful. I attached my sample text data. My purpose is to reach 5 by 21 matrix where each row should be organised by concentrating two rows periodically. The desired first row is as below.
1, 16311, 16312, 39037, 39036, 1596, 1597, 16285, 16284, 49791, 49790, 49789, 49788, 49792, 49793, 49794, 49795, 49797, 49796, 49798, 49799
I have tried to assign two formatSpec for two rows that are containing 16 and 5 numerical values. If I was succeed for this, I plan to manipulate two element row matrices to make it one matrix.
How can I solve this issue?
Thanks,
Mesut
  2 comentarios
Jan
Jan el 4 de Oct. de 2022
The file looks like this:
*Element, type=C3D20R
1, 16311, 16312, 39037, 39036, 1596, 1597, 16285, 16284, 49791, 49790, 49789, 49788, 49792, 49793, 49794,
49795, 49797, 49796, 49798, 49799
2, 16312, 16313, 39038, 39037, 1597, 1598, 16286, 16285, 49802, 49801, 49800, 49790, 49803, 49804, 49805,
49793, 49796, 49806, 49807, 49798
3, 16313, 16314, 39039, 39038, 1598, 1599, 16287, 16286, 49810, 49809, 49808, 49801, 49811, 49812, 49813,
49804, 49806, 49814, 49815, 49807
4, 16314, 16315, 39040, 39039, 1599, 1600, 16288, 16287, 49818, 49817, 49816, 49809, 49819, 49820, 49821,
49812, 49814, 49822, 49823, 49815
5, 16315, 16316, 39041, 39040, 1600, 1601, 16289, 16288, 49826, 49825, 49824, 49817, 49827, 49828, 49829,
49820, 49822, 49830, 49831, 49823
Are you sure that there is a line break? Or tis this an artifact of the editor you view the data in? Did you note the trailing comma in the first lines of the data? Then "... %d, %d, %d\n'" doe not match, because the data end with: "... %d, %d, %d,\n'".
What are "two element row matrices"?
Mesut Sahin
Mesut Sahin el 4 de Oct. de 2022
I get this element data from another software. The numbers from 1 to 5 refer to individual element number. Each element has 20 node hence I need to organise my data starting with element number and associated node numbers (20 for each). As for the first two rows, I need to concentrate them into 1 by 21 array.
I am not sure if I can use fscanf by using two different formatspec so I want to scan first numerical row then scan the second numerical row (5 values). I need to repeat this process for many elements.
I have encountered the problem that only first numerical value row can be scanned and the remaining will not.
I mean two element matrices for getting 16 numerial value row (5*16) and 5 numerical value row (5*5). If I am successful to do this, I will manipulate these matrices to reach one matrix (5*21).

Iniciar sesión para comentar.

Respuesta aceptada

dpb
dpb el 4 de Oct. de 2022
file=readlines(websave('data.txt','https://www.mathworks.com/matlabcentral/answers/uploaded_files/1145645/data.txt'));
data=[];
for i=2:2:numel(file)
str=file(i)+" "+strtrim(file(i+1));
data=[data;cell2mat(textscan(char(str),'%f','delimiter',',')).'];
end
data
data = 5×21
1 16311 16312 39037 39036 1596 1597 16285 16284 49791 49790 49789 49788 49792 49793 49794 49795 49797 49796 49798 49799 2 16312 16313 39038 39037 1597 1598 16286 16285 49802 49801 49800 49790 49803 49804 49805 49793 49796 49806 49807 49798 3 16313 16314 39039 39038 1598 1599 16287 16286 49810 49809 49808 49801 49811 49812 49813 49804 49806 49814 49815 49807 4 16314 16315 39040 39039 1599 1600 16288 16287 49818 49817 49816 49809 49819 49820 49821 49812 49814 49822 49823 49815 5 16315 16316 39041 39040 1600 1601 16289 16288 49826 49825 49824 49817 49827 49828 49829 49820 49822 49830 49831 49823
  1 comentario
Mesut Sahin
Mesut Sahin el 6 de Oct. de 2022
Thank you for your contribution.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Text Data Preparation en Help Center y File Exchange.

Productos


Versión

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by