configuration file for gui executable

3 ビュー (過去 30 日間)
Mario
Mario 2011 年 5 月 5 日
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

回答 (2 件)

Walter Roberson
Walter Roberson 2011 年 5 月 5 日
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 件のコメント
Kaustubha Govind
Kaustubha Govind 2011 年 5 月 5 日
In addition, you can use UIGETFILE to let the user select such a configuration file at run-time.

サインインしてコメントする。


Mario
Mario 2011 年 5 月 19 日
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

カテゴリ

Help Center および File ExchangeMATLAB Compiler についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by