【问题标题】:MATLAB GUI, how to extract words from Static Text in GUI?MATLAB GUI,如何从 GUI 中的静态文本中提取单词?
【发布时间】:2016-07-11 06:58:22
【问题描述】:

我还是这个 MATLAB GUI 的新手。 在我的项目中,我加载了一个文件并在静态文本中显示了内容,但我希望它是一个更可读的版本,以便在用户界面中显示。

这是文件的内容:

!MLF!#

"*/test001.rec"

0 200000 sent-start -162.580292

200000 4500000 five -2768.522217

4500000 7900000 five -2114.920898

7900000 12300000 one -2661.298828

12300000 15800000 two -2209.799805

15800000 29800000 sent-end -6030.099609
.

我想知道有没有办法从GUI中的静态文本中提取单词,然后将“五五一二”转换为“5512”。

我已经在谷歌上苦苦挣扎了将近一个星期来学习如何做到这一点。 任何帮助都非常感谢。 提前致谢! :)

已编辑,

这是我目前的编码:

data1 = importdata('C:\Users\User\Desktop\bin.win32\recout.mlf','') 
set(handles.txtMsg, 'Max', 2); 
set(handles.txtMsg,'String',data1) 

%capturedString = get(handles.txtMsg,'String');
%capturedString = strjoin(captureString')

capturedString = 'nine one';
%StaticTextInString = regexprep(captureString,'[^\w'']','')

WordsToDigit=find(not(cellfun('isempty',strfind({'zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'},capturedString)))) - 1;

set(handles.txtMsg,'String',WordsToDigit);'

让我们先假设 captureString = 'nine one',

如果我让 captureString = 'nine',那么 WordsToDigit = '9'。但是,如果有超过 1 个单词,例如上面的一个:'九一',那么结果将是 'Empty matrix: 1-by-0'..

是否可以检测字符串中的多个子字符串?

例如,cappedString = "dasd 312 九 wqej 七 98w 一",WordsToDigit = '971'。

谢谢!

【问题讨论】:

  • 您是否尝试过使用查找表将单词形式的数字转换为数字?
  • 你能分享你的代码吗?您是否从回调函数访问它,以便您可以访问句柄结构并可以通过以下方式读取静态文本:StaticTextInString = get(handles.yourstatictext,'String');

标签: matlab user-interface


【解决方案1】:

首先,从 GUI 中获取静态文本到字符串中。例如,如果您有权访问句柄结构:

StaticTextInString = get(handles.yourstatictext,'String');

之后,如果你只有单词形式的数字,你可以使用以下函数获取数字编号:

find(not(cellfun('isempty',strfind({'zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'},StaticTextInString ))))-1

例如,对于 StaticTextInString = 'five',前面的命令返回 5。

多个词的扩展名:

capturedString = 'dasd 312 nine wqej seven 98w one'
words = strread(capturedString,'%s','delimiter',' ');
digits = {'zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'};
WordsToDigit = 0;
j = 1;
for i = 1:size(words)
    if sum(ismember(digits, words(i)))==1
        newdigit = find(not(cellfun('isempty',strfind({'zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'}, char(words(i)) ))))-1;
        WordsToDigit = WordsToDigit*10 + newdigit;
        j=j+1;
    end
end

WordsToDigit = 971 的结果

【讨论】:

  • 首先,感谢您抽出宝贵时间帮助我解答问题,非常感谢!非常感谢你!那真的很有帮助!我试图学习您向我展示的代码,最终还剩下 1 个问题。我无法在这里回复,所以我在上面的帖子上进行了编辑,因为它超过了评论的最大字符数。
  • 如果您的 Matlab 版本低于 2013a 或简单地使用 strsplit( 'dasd 312 九 wqej 七 98w one')如果你有更新的版本。
  • 它神奇地起作用了!太感谢了!言语无法表达我的感激之情。再次,谢谢!
猜你喜欢
  • 2011-02-24
  • 1970-01-01
  • 1970-01-01
  • 2013-06-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-25
  • 1970-01-01
相关资源
最近更新 更多