configuration file for gui executable

17 views (last 30 days)
Mario
Mario on 5 May 2011
I created from a gui an executable with the Matlab Compiler. Now a configuration file has to be created containing the strings of different edit fields and the values for checkboxes and pushbuttons.
In the noncomiled case one can create a m-file script containing different variable definitions, eg.:
% Script-file init1.m:
variable = 'Text';
% Matlab-program test1.m:
init1 % read variable
msgbox(variable) % apply variable
This is impossible for the compiled program.
Question:
How can I read a configuration file from a executable?
Best regards,
Mario

Answers (2)

Walter Roberson
Walter Roberson on 5 May 2011
There will be some difficulties in getting the right directory name to find the configuration file, but there are others here that know exactly how to get that part to work.
You will have to parse the configuration file, such as by using
fid = fopen(ConfigFileName,'rt');
inconfig = textscan('%s %*[=] %[^\n]', 'CommentStyle', '%');
fclose(fid);
Then inconfig{1} should be a cell array of strings that are the variable names, and inconfig{2} should be a cell array of strings that are the corresponding values. You would need to examine the values further to determine whether they are valid numbers or strings or whatever you want to allow, and after validation you would use str2double() and the like to extract the corresponding value.
If instead of independent variables, you use a structure of configuration variables, then after validating that the variable names are valid (and known) identifiers, you could use dynamic structure field names to do the assignments.
Another approach to this all, especially with more complicated initialization schemes, is to use regexp to do the parsing of the configuration file.
  1 Comment
Kaustubha Govind
Kaustubha Govind on 5 May 2011
In addition, you can use UIGETFILE to let the user select such a configuration file at run-time.

Sign in to comment.


Mario
Mario on 19 May 2011
A simple and convenient parse function that I implemented reads the whole configuration file into a string matrix and evaluates it afterwards:
% file: csvconfig.m
name = 'PIV';
head_compare = {'x-pos' 'y-pos' 'z-pos' '' 'V'};
% ...
% file: csvconvert.m
fid = fopen('csvconfig.m','r'); % open configuration file
if ~isequal(fid,-1)
cmdStringMatrix = fscanf(fid, '%c'); % read whole text from Matlab file
fclose(fid); % close file
eval(cmdStringMatrix) % interpret text as Matlab commands
if exist('head_compare','var') && iscell(head_compare)
% code ...
end
if exist('name','var') && ischar(name)
% code ...
end
% ...
else % no file found
% ...
end

Community Treasure Hunt

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

Start Hunting!