shizhi214 发表于 2010-8-31 15:20:28

将command中的信息显示在edit中

本帖最后由 shizhi214 于 2010-8-31 15:47 编辑

请问:在matlab GUI中,如何将command窗口中的运行警告信息发送到GUI的edit窗口中?

另外就是希望edit框中的内容是可以追加的,也就是说,程序有多个地方向edit框输出信息,后一条输出的信息自动添加到前一条的后面,而不是象set()那样,之前的信息就没有了。

请问如何实现

qibbxxt 发表于 2010-8-31 16:07:11

1.这个问题是要获得command window窗口的字符串,希望下面几句话对你有所帮助mde = com.mathworks.mde.desk.MLDesktop.getInstance;
cw = mde.getClient('Command Window');
xCmdWndView = cw.getComponent(0).getViewport.getComponent(0);xCmdWndView 是结构体,他的text属性就是command窗口的字符串
2.自动添加,你可以把之前的字符串保存下面,新的接在后面,然后更新字符串
建议你可以参考一下
http://www.mathworks.com/matlabc ... -the-command-window

shizhi214 发表于 2010-9-1 14:29:46

哦。我试试哈。谢谢哈

lin2009 发表于 2010-9-12 10:02:55

2# qibbxxt

找不到你说的xCmdWndView 的Text 属性。

>>   mde = com.mathworks.mde.desk.MLDesktop.getInstance;
cw = mde.getClient('Command Window');
xCmdWndView = cw.getComponent(0).getViewport.getComponent(0);
>> fieldnames(xCmdWndView)
ans =
    'FOCUS_ACCELERATOR_KEY'
    'DEFAULT_KEYMAP'
    'WHEN_FOCUSED'
    'WHEN_ANCESTOR_OF_FOCUSED_COMPONENT'
    'WHEN_IN_FOCUSED_WINDOW'
    'UNDEFINED_CONDITION'
    'TOOL_TIP_TEXT_KEY'
    'TOP_ALIGNMENT'
    'CENTER_ALIGNMENT'
    'BOTTOM_ALIGNMENT'
    'LEFT_ALIGNMENT'
    'RIGHT_ALIGNMENT'
    'WIDTH'
    'HEIGHT'
    'PROPERTIES'
    'SOMEBITS'
    'FRAMEBITS'
    'ALLBITS'
    'ERROR'
    'ABORT'
>> fieldnames(cw)
ans =
    'WHEN_FOCUSED'
    'WHEN_ANCESTOR_OF_FOCUSED_COMPONENT'
    'WHEN_IN_FOCUSED_WINDOW'
    'UNDEFINED_CONDITION'
    'TOOL_TIP_TEXT_KEY'
    'TOP_ALIGNMENT'
    'CENTER_ALIGNMENT'
    'BOTTOM_ALIGNMENT'
    'LEFT_ALIGNMENT'
    'RIGHT_ALIGNMENT'
    'WIDTH'
    'HEIGHT'
    'PROPERTIES'
    'SOMEBITS'
    'FRAMEBITS'
    'ALLBITS'
    'ERROR'
    'ABORT'

qibbxxt 发表于 2010-9-12 10:49:32

说错了,不是结构体,我不知道是什么类型,应该是java class或者user class或者dataset之类吧
但是可以得到从text中找到输入的字符串>> magic(3)

ans =

   8   1   6
   3   5   7
   4   9   2

>>   mde = com.mathworks.mde.desk.MLDesktop.getInstance;
cw = mde.getClient('Command Window');
xCmdWndView = cw.getComponent(0).getViewport.getComponent(0);
>> get(xCmdWndView,'text')

ans =

>> magic(3)

ans =

   8   1   6
   3   5   7
   4   9   2

>>   mde = com.mathworks.mde.desk.MLDesktop.getInstance;
cw = mde.getClient('Command Window');
xCmdWndView = cw.getComponent(0).getViewport.getComponent(0);
>> get(xCmdWndView,'text')

lin2009 发表于 2010-9-12 11:16:04

5# qibbxxt
谢谢,现在我想把程序运行的时间记录到指定的文件中,但是有时可以实现,有时又不能实现,看看是什么问题。

下面是M文件中的程序:

clc;
tic
% 记录程序运行时间到指定的文件中。
% 程序段
for k = 1:10^6
    k;
end
% 程序段
toc
mde = com.mathworks.mde.desk.MLDesktop.getInstance;
cw = mde.getClient('Command Window');
xCmdWndView = cw.getComponent(0).getViewport.getComponent(0);
mystr = get(xCmdWndView,'text');
fid = fopen('myfile.m','w');
fprintf(fid,'%s\n',mystr);
fclose(fid);
type myfile.m

qibbxxt 发表于 2010-9-12 11:38:39

6# lin2009
你的这个程序在我的电脑上面运行正常

lin2009 发表于 2010-9-12 12:57:03

7# qibbxxt
多运行几次,不是每一次都正常,我的版本是2010a。

qibbxxt 发表于 2010-9-12 15:39:27

clc;clear
% 记录程序运行时间到指定的文件中。
% 程序段
if exist('myfile.m')
    delete myfile.m
end
tic
for k = 1:10^6
    k;
end
% 程序段
toc
mde = com.mathworks.mde.desk.MLDesktop.getInstance;
cw = mde.getClient('Command Window');
xCmdWndView = cw.getComponent(0).getViewport.getComponent(0);
mystr = get(xCmdWndView,'text');
fid = fopen('myfile.m','w');
fprintf(fid,'%s',mystr);
fclose(fid);
for i=1:20
    clc
    tic
    % 记录程序运行时间到指定的文件中。
    % 程序段
    for k = 1:10^6
      k;
    end
    % 程序段
    toc
    mde = com.mathworks.mde.desk.MLDesktop.getInstance;
    cw = mde.getClient('Command Window');
    xCmdWndView = cw.getComponent(0).getViewport.getComponent(0);
    mystr = get(xCmdWndView,'text');
    fid = fopen('myfile.m','at');
    fprintf(fid,'%s',mystr);
    fclose(fid);
end
dbtype myfile.m这是我运行了21次,得出的结果Elapsed time is 0.023347 seconds.

1   Elapsed time is 0.023180 seconds.
2   Elapsed time is 0.023322 seconds.
3   Elapsed time is 0.023339 seconds.
4   Elapsed time is 0.023270 seconds.
5   Elapsed time is 0.023552 seconds.
6   Elapsed time is 0.023547 seconds.
7   Elapsed time is 0.023327 seconds.
8   Elapsed time is 0.023478 seconds.
9   Elapsed time is 0.023269 seconds.
10    Elapsed time is 0.024980 seconds.
11    Elapsed time is 0.023209 seconds.
12    Elapsed time is 0.023510 seconds.
13    Elapsed time is 0.023289 seconds.
14    Elapsed time is 0.023346 seconds.
15    Elapsed time is 0.023244 seconds.
16    Elapsed time is 0.023416 seconds.
17    Elapsed time is 0.023432 seconds.
18    Elapsed time is 0.023425 seconds.
19    Elapsed time is 0.023427 seconds.
20    Elapsed time is 0.023284 seconds.
21    Elapsed time is 0.023347 seconds.第一行是最后一次显示在command window的,没有clc
页: [1]
查看完整版本: 将command中的信息显示在edit中