1

I am trying to read data from files line by line using fgets. I have several lines of this format-

0.0000   -0.5808    0.0000 F   0  0  0  0  0  0  0  0  0  0  0  0

The spaces between each is variable.

str=fgets(fid)

using this I get the entire string.I want to extract information from this line and change the values.For eg,I want to change first two float values and say second last integer.So after changes this will be my new line-

0.4500   9.5808    0.0000 F   0  0  0  0  0  0  0  0  0  0  6  0

One way to do this is to go throughout the length of the string character by character,get values between spaces convert them to float or integer and replace them in the string.I want to know is there some other method by which I can extract the information without traversing the entire string.Because format of the string is always same- three float values followed by a character followed by 12 integers.Is there some way I can access these elements directly?

Edit- To be precise,can I do something like the following which I have done in c.It is quite simple-

sscanf(str,"%f %f %f %c %d %d ",&a,&b,&c,&d,&e,&f);

So, by this I could easily what I want.So,is there something like this in matlab?

7
  • 1
    Ah, yes. I believe strread() is what you are looking for. Check it out here: mathworks.com/help/matlab/ref/strread.html Commented Jul 6, 2015 at 17:26
  • The functions sscanf and fscanf also exists in Matlab and the syntax is very similar to the C equivalent if you're used to it. Commented Jul 6, 2015 at 17:31
  • @Hoki yes but sscanf in matlab does not allow us to control output variables.If I am not wrong,we can have just one output in sscanf Commented Jul 6, 2015 at 17:33
  • @LifeIsGood I believe the output of sscanf is an array of all variables specified, in the order specified. Commented Jul 6, 2015 at 17:37
  • 1
    @Hoki Yes I have gone through fgets and actually I am using this to get an entire line.But since all of my lines are not of the format I mentioned above I wanted to use something in terms of my C code. Commented Jul 6, 2015 at 17:48

2 Answers 2

1

Use textscan for this purpose.Check it out here

In your case you can do something of this sort-

A = textscan(str,'%.4f %.4f %.4f %c %d %d %d %d %d %d %d %d %d %d %d %d');

You will get a 1X16 cell.You can access any element using A{i}.For example to get the character just do this-

ch=char(A{4});

Likewise,you can access any element without having to traverse the entire string.

Sign up to request clarification or add additional context in comments.

Comments

0

You should avoid loops or whiles in Matlab. Try to use one line sentences taking advantage of the matrix syntax.

In this case, instead of reading the file with fgets, use directly textscan to read the file.

C = textscan(fileID,formatSpec)

Avoid using the same commands than c if there is an specialized one in Matlab. It usually have better performance

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.