Cannot read properly with fscanf a file configuration.in

1 Ansicht (letzte 30 Tage)
Morgane Borreguero
Morgane Borreguero am 30 Okt. 2016
Kommentiert: dpb am 30 Okt. 2016
Dear all,
I am trying to read a file with the extension ".in" looking as followed:
typePerturbation=0
fo=1
xo=300000
sigma=50000
Ninter=1000
Then in my matlab code:
fid = fopen('configuration.in','r'); % open filehandle
fo = fscanf(fid, '%g', 1);
xo = fscanf(fid,'%g', 1);
yo = fscanf(fid,'%g', 1);
sigma = fscanf(fid,'%g', 1);
but Matlab is apparently unable to rad properly the file since it gives me as output:
fo =
[]
xo =
[]
yo =
[]
uconst =
[]
Instead of fo=1 etc. Can someone help me? I really don't know where the problem come from... Thank you

Antworten (2)

dpb
dpb am 30 Okt. 2016
Your format doesn't account for the nonnumeric field ahead of the number in each record. Use something like
fmt='%*s%f'; % skip a string, read a numeric
fid = fopen('configuration.in','r');
values=cell2mat(textscan(s,fmt,'delimiter','=','collectoutput',1));
Above will give you all the values in the file in the array, including the typePerturbation value. If you really don't want it, add in the 'headerlines',1 optional parameter.
Note the use of the '=' in the record as the delimiter; otherwise the '%&s' would "eat" the full record as there's no whitespace in the file and a string will be interpreted until a whitespace character or a delimiter is found.
See the doc for textscan for the details...you can read into individual variables if so choose, just showed "the Matlab way" using arrays where possible...
  2 Kommentare
Morgane Borreguero
Morgane Borreguero am 30 Okt. 2016
Bearbeitet: Morgane Borreguero am 30 Okt. 2016
Thank you @dpd but that is not what I need! If I do like you wrote then I get all value in one array... In matlab I want to associate one value to one variable....
dpb
dpb am 30 Okt. 2016
Well, it's not the Matlab way, but as noted, simply either assign the array values to variables or write a line for each one analogous to your existing code excepting for textscan in lieu of fscanf and the proper format string. You then don't need the 'collectoutput' argument, of course, and will want to use a counted read to only read a single value each call...
firstvarname=cell2mat(textscan(s,fmt,1,'delimiter','='));
nextvarname=cell2mat(textscan(s,fmt,1,'delimiter','='));
...

Melden Sie sich an, um zu kommentieren.


Les Beckham
Les Beckham am 30 Okt. 2016
For your specific example, I would just change the extension of the file to .m and run it. Your example is valid Matlab m code. This would result in separate variables in your Matlab workspace with the values you want.
  2 Kommentare
Morgane Borreguero
Morgane Borreguero am 30 Okt. 2016
Thank you @Les Beckham! But it is already an .m extension :/
dpb
dpb am 30 Okt. 2016
Actually, that's not a bad idea if that's all of the file content--altho would need to add ';' on the ends of the records to prevent echoing the variables to the command window although that might not be considered a problem.
Morgane, iiuc, you're saying the above code snippet in your Question is (part of) a script m-file. But, the code is opening another file; Les is saying instead of doing that, rename 'configuration.in' to 'configuration.m' and then write the code as simply
...
configuration
...
and you'll have the variables listed in the resulting workspace (with the above caveat about echoing, of course).

Melden Sie sich an, um zu kommentieren.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by