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 CenterFile Exchange에서 MATLAB Compiler에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by