organising data with fscanf
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
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
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"?
Respuesta aceptada
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
Más respuestas (0)
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!