sscanf syntax/usage question

2 visualizaciones (últimos 30 días)
Steve
Steve el 2 de Jul. de 2012
Hi
I have been using sscanf to extract information from some ascii filenames as follows:
files = dir('*.ascii);
values = sscanf(files(X).name, '%*c%d%*3c');
My filenames are in the format x10mm y20mm.ascii where the numbers vary depending on the x and y position of the data inside the file, and (for this example) the above code gives me the output:
values = [10;20]
This is the outcome that I want, but the problem is that I don't understand why it works. I managed to work out the format syntax by trial and error, reasoning that %*1c ignores the first character (x), %d reads the integers (1 and 0), %*3c ignores the next three characters (m,m,y) and so on; but I expected to require another %d and then a %*8c to read the second set of integers and ignore the remainder.
Can anyone explain why the code above gives the correct outcome?
Thanks for any help Steve
  2 comentarios
Thomas
Thomas el 2 de Jul. de 2012
sscanf reads data from a str. Where have you loaded the data in the string?
You question is a little ambiguous as to where you are getting stuck, is it at the file read stage or have you read your file and cannot get the values in the array.. (the dir command only lists the files .ascii and puts the name in the varaiable files it does not read the contents of the file..
Steve
Steve el 2 de Jul. de 2012
Hi there
Sorry about the confusion. I am loading the data contained in the file elsewhere in the code with csvread, that's not the problem. The issue is I have 300+ files, each of which has x and y position 'metadata' stored in the filename. I need to associate each spatial position with the relevant data from inside the file so I can 'tag' the processed data in an array.
For that purpose, I am using sscanf to extract the x and y positional data from the filename. It wasn't so much a question about how to make sscanf work or how to extract the data, but that I've discovered that sscanf works if I do it this way and I didn't know why.
I wanted to make sure I knew exactly what my code was doing to avoid any problems later.
Hope that clears up the ambiguity.
Thanks Steve

Iniciar sesión para comentar.

Respuesta aceptada

Walter Roberson
Walter Roberson el 2 de Jul. de 2012
sscanf() re-uses the format if you have not reached the end of the string, but stops if there is a format mismatch.
  1 comentario
Steve
Steve el 2 de Jul. de 2012
Ah, I think this pretty much answers the question.
If I understand that correctly then, '%*1c %d %*4c %d %*8c' would give the same result? (I'm at home now, can't check - but now that I know that I can experiment with it when I'm back at work).
Thanks for your help. Steve

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Simulink Environment Customization en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by