textscan difficulties

6 visualizaciones (últimos 30 días)
Jason
Jason el 28 de Oct. de 2011
Hello. How can i use textscan to read in a certain table that is under a user defined heading.
# Data150
A B C D E
A 1 21 7 6 4
B 26 2545 8 7 1
C 10 12 2453 12 2
D 19 24 27 3046 6
E 0 0 0 0 0
My text file has a few headerlines at the top but is then listed in the above format sequentially, so theres another table underneath that has a heading # Data149. so how could I search for the heading e.g. ~ Data150 and just pick up the numbers in the table (they are seperated by tabs.
thanks
  2 comentarios
Jason
Jason el 28 de Oct. de 2011
Just to add, I have say 150 of these tables, each on eunder its heading and descending, so at the end of the text file is Data1.
I eventually want to average all data tables say from table 1 - 50.
Jan
Jan el 28 de Oct. de 2011
what have you tried so far to solve your problem?
I assume TEXTSCAN is not sufficient. I'd use FOPEN/FGETL/FSCANF and a WHILE loop instead.

Iniciar sesión para comentar.

Respuesta aceptada

Jan
Jan el 31 de Oct. de 2011
Assuming that the data index in "# Data%d" starts at 1:
function Data = readTheData(FileName)
FID = fopen(FileName, 'r');
if FID == -1, error('Cannot open file %s.', FileName);
CC = textscan(FID, '%s', 'Delimiter', '\n');
% perhaps this in addition: 'WhiteSpace', ''
fclose(FID);
C = CC{1};
index = find(strncmp(C, '#', 1));
Len = numel(index);
Data = zeros(5, 5, Len);
for i = 1:Len
iLine = index(i) + 1; % Skip the header line
for j = 1:5 % Read 5 lines
aData = sscanf(C{iLine + 1}, ' *c %d %d %d %d %d');
Data(j, :, i) = transpose(aData);
end
end
  2 comentarios
Jason
Jason el 31 de Oct. de 2011
Hi, many thamks. Mt data index actually counts backwards so it will start say feom "# Data150" and at the end of the page have the last table with "# Data1#". I also notice when I use textscan to read in, when it reads in the numbers from the table, it just puts them all together in a single line with no spacing or tabs between them. Is it possible to tell textscan to do this -
i couldn't find it in the help.
Thankyou very much.
Jason
Jan
Jan el 31 de Oct. de 2011
@Jason: Then you can either reorder Data afterwards: "Data = Data(:, :, end:-1:1)". Or you can consider this during the creation: "Data(j, :, Len+1-i) = ...".
Please show the inputs of TEXTSCAN. It matters if you are readibng a line using the '%s' or '%g' formats. Did you set the WhiteSpace property correctly?

Iniciar sesión para comentar.

Más respuestas (1)

Jason
Jason el 31 de Oct. de 2011
Yes, I have finally got your solution to work - brilliant.
Now I have a set of matrixes called "Data". Is it possible to perform a vector sum, i.e. create another matrix where each element has been summed.
Thanks
  1 comentario
Jan
Jan el 31 de Oct. de 2011
Yes. See SUM(Data, Dim).

Iniciar sesión para comentar.

Categorías

Más información sobre Data Type Conversion en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by