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?
strread()is what you are looking for. Check it out here: mathworks.com/help/matlab/ref/strread.htmlsscanfandfscanfalso exists in Matlab and the syntax is very similar to theCequivalent if you're used to it.sscanfin matlab does not allow us to control output variables.If I am not wrong,we can have just one output insscanfsscanfis an array of all variables specified, in the order specified.fgetsand 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 myCcode.