Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
making a strcture out of configuration file

Subject: making a strcture out of configuration file

From: Yoav Rubin

Date: 24 Jul, 2007 12:37:20

Message: 1 of 6

Hi All,
In my application I have an external configuration file, in which each line is a key value pair, e.g.,
key1 = value1
key2 = value2

I need somehow to read this file and create out of it a struct, which would (for the previous example) be like this:

myStruct.key1 : value1
myStruct.key2 : value2

is there a way to do it
thanks
Yoav

Subject: making a strcture out of configuration file

From: Titus

Date: 24 Jul, 2007 17:20:40

Message: 2 of 6

Hi Yoav,
yes there is. The name is dynamic field reference.
Say you read the first line into a string str (by e.g. fgetl).
Then one way is:

% generate cell array 'key1', '=', 'value1'
strCell = strread(str, '%s', 'delimiter', ' ');
% do the assignment
myStruct.(strCell{1}) = strCell{3};

Now: what are value1, value2? Numbers? In this case you would
need to change the last line to
myStruct.(strCell{1}) = str2double(strCell{3});

Hope this helps,
Titus


"Yoav Rubin" <yoavrubin@hotmail.com> schrieb im Newsbeitrag
news:f84rq0$mvk$1@fred.mathworks.com...
> Hi All,
> In my application I have an external configuration file, in which each
> line is a key value pair, e.g.,
> key1 = value1
> key2 = value2
>
> I need somehow to read this file and create out of it a struct, which
> would (for the previous example) be like this:
>
> myStruct.key1 : value1
> myStruct.key2 : value2
>
> is there a way to do it
> thanks
> Yoav

Subject: making a strcture out of configuration file

From: Gautam Vallabha

Date: 24 Jul, 2007 11:32:37

Message: 3 of 6

"Yoav Rubin" <yoavrubin@hotmail.com> wrote in message
news:f84rq0$mvk$1@fred.mathworks.com...
> In my application I have an external configuration file, in which each
> line is a key value pair, e.g.,
> key1 = value1
> key2 = value2
> I need somehow to read this file and create out of it a struct, which
> would (for the previous example) be like this:
> myStruct.key1 : value1
> myStruct.key2 : value2

% if your values are strings
[keynames,values]=textread('tmp.dat','%s=%s');
myStruct = cell2struct(values, keynames)

% if your values are numbers
[keynames,values]=textread('tmp.dat','%s=%d');
myStruct = cell2struct(num2cell(values), keynames)

% If your values are mixed numbers & strings
[keynames,values]=textread('tmp.dat','%s=%s');
v = str2double(values); idx = ~isnan(v);
values(idx) = num2cell(v(idx));
myStruct = cell2struct(values, keynames)

--

Gautam Vallabha
The MathWorks
Email: Gautam.Vallabha@mathworks.com

Subject: making a strcture out of configuration file

From: Srinivas

Date: 26 Aug, 2009 20:47:03

Message: 4 of 6

Hi Gautam,

I tried to follow the example you gave me and I am getting this error. Is there anything I am missing?

Contents of my file UB.conf are
riskStart=1
riskEnd=13

and the code I am using is
[keynames,values]=textread('UB.conf','%s=%d');
myStruct = cell2struct(values, keynames);

The error is
??? Error using ==> dataread
Trouble reading literal string from file (row 1, field 2) ==> \n

Error in ==> textread at 176
[varargout{1:nlhs}]=dataread('file',varargin{:});

Thanks,

Srini


"Gautam Vallabha" <gvallabh@mathworks.com> wrote in message <f8562l$ig3$1@fred.mathworks.com>...
> "Yoav Rubin" <yoavrubin@hotmail.com> wrote in message
> news:f84rq0$mvk$1@fred.mathworks.com...
> > In my application I have an external configuration file, in which each
> > line is a key value pair, e.g.,
> > key1 = value1
> > key2 = value2
> > I need somehow to read this file and create out of it a struct, which
> > would (for the previous example) be like this:
> > myStruct.key1 : value1
> > myStruct.key2 : value2
>
> % if your values are strings
> [keynames,values]=textread('tmp.dat','%s=%s');
> myStruct = cell2struct(values, keynames)
>
> % if your values are numbers
> [keynames,values]=textread('tmp.dat','%s=%d');
> myStruct = cell2struct(num2cell(values), keynames)
>
> % If your values are mixed numbers & strings
> [keynames,values]=textread('tmp.dat','%s=%s');
> v = str2double(values); idx = ~isnan(v);
> values(idx) = num2cell(v(idx));
> myStruct = cell2struct(values, keynames)
>
> --
>
> Gautam Vallabha
> The MathWorks
> Email: Gautam.Vallabha@mathworks.com
>
>

Subject: making a strcture out of configuration file

From: Deepak Kumar

Date: 18 Aug, 2010 15:55:29

Message: 5 of 6

I am a year late. But the error is caused by having no space around the "=" symbol in the txt file.

Subject: making a strcture out of configuration file

From: Deepak Kumar

Date: 18 Aug, 2010 15:57:26

Message: 6 of 6

I am a year late. But the error is caused by having no space around the "=" symbol in the txt file.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us