sscanf syntax/usage question

조회 수: 2 (최근 30일)
Steve
Steve 2012년 7월 2일
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
Thomas
Thomas 2012년 7월 2일
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 2012년 7월 2일
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

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2012년 7월 2일
sscanf() re-uses the format if you have not reached the end of the string, but stops if there is a format mismatch.
  댓글 수: 1
Steve
Steve 2012년 7월 2일
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

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Simulink Environment Customization에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by