Periodically Updated Static Text and Reading from Key-Value File

3 views (last 30 days)
I have two questions concerning a GUI application I'm writing:
  1. Is it possible to code a static text label so it periodically updates itself (from a database, or whatever) without interfering with the execution of the main application? The label would be used to cycle through news, updates, etc. That sort of thing.
  2. Currently, my application sets user-supplied variables by running a script, generate_appvars. I'll be compiling soon, but want to preserve this method of reading in run params at runtime. I was thinking a key-value params file which I can read into a dynamically named struct, but am unsure of how to handle vector and cell assignments (k/v pairs key = [1, 3, 5] or key = {'this', 'that'} for instance). Thinking of using eval for the right hand assignment, but don't think it's the best solution. How do other people handle runtime parameters like this?
Thanks!

Accepted Answer

Fangjun Jiang
Fangjun Jiang on 22 Sep 2011
For 1, you can probably use a timer object and set up the callback to refresh your text label.
For 2, you can use the parameter file to specify your key-value pairs:
par1, 1
par2, 2
par3, 3
After reading in the files, try to organize the data as:
Keys={'par1','par2','par3'}; Values={1,2,3};
Then run
a=cell2struct(Values,Keys,2)
You will get a.par1, a.par2, a.par3 holding the correct value and you've avoided the notorious evil eval.

More Answers (2)

Meric Ozturk
Meric Ozturk on 26 Sep 2011
Thanks guys, the timer suggestion worked perfectly. If anyone runs into problem #2, my implementation's below (based around Fangjun's suggestion). I'll add a function version to FEX when I get a chance. The code reads key-value assignments in matlab syntax into struct p, ignoring lines beginning with '#'.
%%Populate parameters struct from parameters.txt
fid = fopen('parameters.txt', 'r');
if fid < 0; error('Parameters file is missing. Please replace.'); end
% Extract key-value pairs from params file
keys = {}; values = {};
while ~feof(fid)
this_line = fgets(fid);
% Check to see if the line is a comment or whitespace
switch this_line(1)
case {'#', ' ', char(10)}
otherwise
% First token is key; second is value
[keys{end+1} inds] = strtok(this_line, '=');
values{end+1} = strtok(inds, '=');
% If the value is convertible to num, convert to num
if ~isnan(str2double(values{end}))
values{end} = str2double(values{end});
end
end
end
fclose(fid);
% Remove extra padding from key-value pairs
keys = strtrim(keys);
str_ind = cellfun(@ischar, values);
values(str_ind) = strtrim(values(str_ind));
% Remove extra ' ' around strings
data_type_ind = [];
for i = find(str_ind)
str = values{i};
if str(1) == ''''
values{i} = str(2:end-1);
else
data_type_ind(end+1) = i;
end
end
% Convert data type assignments to actual data types
% This may actually be a valid use of the eval function!
for i = data_type_ind
eval(['values{i} = ' values{i} ';']);
end
% Construct parameter struct from read key-value pairs
p = cell2struct(values, keys, 2);

Walter Roberson
Walter Roberson on 22 Sep 2011
1. No -- background activity always interferes with the execution of the main application.
Perhaps for your purposes it would be acceptable to code a timer whose callback fetched the data and set() the text control to the new content.
2. regexp() can help with the parsing -- though matching apostrophes can be a pain if your strings are allowed to include apostrophes.

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!