文本句柄 uicontrol 在回调函数中未正确更新
Posted
技术标签:
【中文标题】文本句柄 uicontrol 在回调函数中未正确更新【英文标题】:Text Handle uicontrol Not Updating Correctly Within a Callback Function 【发布时间】:2015-10-27 09:46:12 【问题描述】:我有四个 progress
消息需要在 text_h
中显示,但只有第一个'正在加载音频...',第二个'计算价特征。 ..',最后一个 'Done!!!' 正在显示。第三条消息“计算唤醒功能...”似乎在此过程中丢失了。我还想知道如何让每条消息保持在text_h
内,并将一条新消息打印到下面的行,以便我可以看到所有进程?
function guiUpload(upload_button_h, evt, text_h, list_h, tracks, models)
global predict_arousal
global predict_valence
%% print to progress monitor
progress = 'Loading Audio...';
set(text_h, 'String', progress);
%% get reference to wave file
[filename, pathname] = uigetfile('.wav', 'File Selector'); % filter file type
complete_path = strcat(pathname,filename); % concat both strings together
%% convert complete_path to WAV with samp freq
[x,fs] = audioread(complete_path);
x = sum(x, 2); % convert to monophonic by averaging
%% trim leading and trailing zeros from WAV vector
x = x(find(x,1,'first'):find(x,1,'last'));
%% print to progress monitor
progress = 'Computing Valence Features...';
set(text_h, 'String', progress);
%% get short-term spread [2xN], then convert to [1xN]
st_spread = stSpread(x, fs, 0.02, 0.01);
st_spread = st_spread(2,:); % get spread (2), not centroid (1)!
%% get mid-term statistics spread [2xN] 1 mean, 2 median
v_stat = 'mean', 'median';
mt_spread = mtFeatureExtraction(st_spread, 3, 3, v_stat);
%% get global mean of spread statistics
mean_spread = mean(mt_spread(1,:));
median_spread = mean(mt_spread(2,:));
%% get global spectral flatnesss statistic - MIR TOOLBOX 1.6.1
flatness = mirflatness(complete_path);
flatness = get(flatness, 'Data');
flatness = flatness1,11,1;
%% get valence predictions for spread and flatness
predict_spread = models.valence.spread(mean_spread);
predict_med_spread = models.valence.med_spread(median_spread);
predict_flatness = models.valence.flatness(flatness);
%% test valence predictions to see if they are greater or less than -4, 4
predict_spread = guiTestPrediction(predict_spread);
predict_med_spread = guiTestPrediction(predict_med_spread);
predict_flatness = guiTestPrediction(predict_flatness);
%% average all valence predictions
valence = mean([predict_spread predict_med_spread predict_flatness]);
%% add valence statistic to predictions structure
predict_valence(end+1) = valence;
%% print to progress monitor
progress = 'Computing Arousal Features...';
set(text_h, 'String', progress);
%% get short-term energy [1xN]
st_energy = stEnergy(x, fs, 0.02, 0.01);
%% get mid-term statistics energy [3xN] 1 mean, 2 std, 3 median
a_stat = 'mean', 'std', 'median';
mt_energy = mtFeatureExtraction(st_energy, 3, 3, a_stat);
%% get global mean of energy statistics
mean_energy = mean(mt_energy(1,:));
std_energy = mean(mt_energy(2,:));
median_energy = mean(mt_energy(3,:));
%% get arousal predictions for energy
predict_energy = models.arousal.energy(mean_energy);
predict_std_energy = models.arousal.std_energy(std_energy);
predict_med_energy = models.arousal.med_energy(median_energy);
%% test arousal predictions to see if they are greater or less than -4, 4
predict_energy = guiTestPrediction(predict_energy);
predict_std_energy = guiTestPrediction(predict_std_energy);
predict_med_energy = guiTestPrediction(predict_med_energy);
%% average all arousal predictions
arousal = mean([predict_energy predict_std_energy predict_med_energy]);
%% add arousal statistic to predictions structure
predict_arousal(end+1) = arousal;
%% add new filename to the end of the tracks array
tracksend+1 = filename;
%% re-populate list_h with new songs array
set(list_h, 'String', tracks);
%% print to progress monitor
progress = 'Done!!!';
set(text_h, 'String', progress);
end
text_h
我的代码如下:
text_position = [0.025 0.05 0.95 0.90];
text_h = uicontrol('Parent', text_panel_h, 'Style', 'text','FontSize', 12,'HorizontalAlignment', 'Left','BackgroundColor','white','units', 'normalized', 'position', text_position, 'String', '');
【问题讨论】:
你确定它丢失了,而不是后续任务的执行速度比预期的快吗? 我不确定,您是否建议在四个进程之间延迟? 不,你为什么要人为地减慢你的程序?我建议学习如何使用MATLAB's debugger,到目前为止,您的所有问题都可以自己简单地调查。 好的,我会在接下来的几个月里构建更多的用户图形界面 【参考方案1】:如果您的text_h
大到足以容纳所有消息,您可以尝试使用新消息更新其String
,而不是重写它以保留所有消息:
% example of initialized text
set(text_h, 'String', 'test 1');
% update its String property
set(text_h, 'String', [get(text_h, 'String'); 'here is the new message']);
请注意,如果您使用多行,get(text_h, 'String')
将返回一个字符串元胞数组。
【讨论】:
A textuicontrol
接受字符串的单元格数组并将每个单元格显示在新行上,没有理由连接字符串。添加一个新单元格。
如何在不设置和获取的情况下添加新单元格?据我所知,除非你有r2014b+,否则你不能直接写text_h.Stringend+1 = 'new message';
以上是关于文本句柄 uicontrol 在回调函数中未正确更新的主要内容,如果未能解决你的问题,请参考以下文章
如何从matlab中用户定义的函数返回一个值,该函数的回调在uicontrol中指定
有没有办法将 uicontrol 回调函数的输出关联到它自己的“用户数据”?