xjtu 发表于 2011-4-21 17:18:46

matlab的计算结果写进一个文本框里面?

本帖最后由 xjtu 于 2011-4-23 18:14 编辑

请问如何将Design()中的计算结果写到data_out里面,即图中的等待设计计算区域?多排同时输入,如下面示例回调函数中的Result1和Result2一起输出。
界面图和程序段所示。
刚学MATLAB,请指点一下。

主程序
%初始化根窗体   
clf reset;   
set(gcf,'Units','pixels','position' ,,'name', '设计计算',...   
    'numbertitle', 'off', 'Tag', 'dsp');   
set(gcf, 'defaultuicontrolfontsize' ,12);   
set(gcf, 'defaultuicontrolfontname' , ' 隶书 ' );
%添加组件   
%设计参数输入区域
labelhead1 = uicontrol(gcf,'Style', 'text', 'String', '','Position', ,...   
    'BackgroundColor', [.5 .6 .6], 'FontSize', 12);
labelhead11 = uicontrol(gcf,'Style', 'text', 'String', '设计参数输入','Position', ,...   
    'BackgroundColor', [.8 .8 .8], 'FontSize', 14);   
%设计结果输出区域   
labelout = uicontrol(gcf,'Style', 'text', 'String', '设计结果输出','Position', ,...   
    'BackgroundColor', [.8 .8 .8], 'FontSize', 14);   
data_out = uicontrol(gcf,'Style', 'edit', 'String', '等待设计计算','Position', ,...
    'BackgroundColor', [.6 .7 .9],'FontSize', 10);
%设计参数输入
% 制冷量Qo
label_Qo = uicontrol(gcf,'Style', 'text', 'String', '制冷量','Position', ,...   
    'BackgroundColor', [.5 .6 .6], 'FontSize', 12);   
data_Qo = uicontrol(gcf,'Style', 'edit', 'String', '10','Position', , 'FontSize', 12);
label_Qo = uicontrol(gcf,'Style', 'text', 'String', 'kW','Position', ,...   
    'BackgroundColor', [.5 .6 .6], 'FontSize', 12);   
% 热源温度th
label_th = uicontrol(gcf,'Style', 'text', 'String', '热源温度','Position', ,...   
    'BackgroundColor', [.5 .6 .6], 'FontSize', 12);   
data_th = uicontrol(gcf,'Style', 'edit', 'String', '85','Position', , 'FontSize', 12);
label_th = uicontrol(gcf,'Style', 'text', 'String', '℃','Position', ,...   
    'BackgroundColor', [.5 .6 .6], 'FontSize', 12);
% 冷冻水进口温度tw
label_tw = uicontrol('Style', 'text', 'String', '冷却水进口温度','Position', ,...   
    'BackgroundColor', [.5 .6 .6], 'FontSize', 12);   
data_tw = uicontrol(gcf,'Style', 'edit', 'String', '32','Position', , 'FontSize', 12);
label_tw = uicontrol(gcf,'Style', 'text', 'String', '℃','Position', ,...   
    'BackgroundColor', [.5 .6 .6], 'FontSize', 12);
% 冷冻水出口温度tc1
label_tc1 = uicontrol(gcf,'Style', 'text', 'String', '冷冻水进口温度','Position', ,...   
    'BackgroundColor', [.5 .6 .6], 'FontSize', 12);   
data_tc1 = uicontrol(gcf,'Style', 'edit', 'String', '11','Position', , 'FontSize', 12);
label_tc1 = uicontrol(gcf,'Style', 'text', 'String', '℃','Position', ,...   
    'BackgroundColor', [.5 .6 .6], 'FontSize', 12);
% 冷却水进口温度tc2
label_tc2 = uicontrol(gcf,'Style', 'text', 'String', '冷冻水出口温度','Position', ,...   
    'BackgroundColor', [.5 .6 .6], 'FontSize', 12);   
data_tc2 = uicontrol(gcf,'Style', 'edit', 'String', '8','Position', , 'FontSize', 12);
label_tc2 = uicontrol(gcf,'Style', 'text', 'String', '℃','Position', ,...   
    'BackgroundColor', [.5 .6 .6], 'FontSize', 12);
%用于计算的按钮   
CalculateH = uicontrol(gcf,'Style', 'pushbutton', 'String', 'Calculate',...   
    'Position', , 'FontSize', 10);
%设置回叫函数
set(CalculateH,'Callback', 'Design(data_Qo,data_th,data_tw,data_tc1,data_tc2)');
Design(data_Qo,data_th,data_tw,data_tc1,data_tc2);

回叫函数(一个简单示例)

function Result=Design(data_Qo,data_th,data_tw,data_tc1,data_tc2)
Qo=str2num(get(data_Qo,'string'));
th=str2num(get(data_th,'string'));
tw=str2num(get(data_tw,'string'));
tc1=str2num(get(data_tc1,'string'));
tc2=str2num(get(data_tc2,'string'));
Result1=Qo/th+tw/tc1+tc2;
Result2=Qo+th+tw+tc1+tc2;
界面如下

xjtu 发表于 2011-4-23 10:37:43

怎么没人回应呀!!!

nwcwww 发表于 2011-4-24 06:07:29

给你的输出窗口加一个TAG,然后Design函数直接findobj去set就行了。

假设你还想在”等待设计计算“那个位置显示结果,
可以先在界面主函数的定义里把data_out后面加上一个属性:
data_out = uicontrol(gcf,'Style', 'edit', 'String', '等待设计计算','Position', ,...
    'BackgroundColor', [.6 .7 .9],'FontSize', 10, 'tag','OutputWindow');

然后稍微改动下Design函数,找到相应的Tag并显示结果:
function Design(data_Qo,data_th,data_tw,data_tc1,data_tc2)
OW=findobj('tag','OutputWindow');

Qo=str2double(get(data_Qo,'string'));
th=str2double(get(data_th,'string'));
tw=str2double(get(data_tw,'string'));
tc1=str2double(get(data_tc1,'string'));
tc2=str2double(get(data_tc2,'string'));

strResult1=num2str(Qo/th+tw/tc1+tc2);
strResult2=num2str(Qo+th+tw+tc1+tc2);
OutputString=['Result1:   ',strResult1,'   Result2:   ',strResult2];
set(OW,'string',OutputString);
return

还有就是主程序最后一行应该删掉。

xjtu 发表于 2011-4-25 13:21:20

非常感谢!
页: [1]
查看完整版本: matlab的计算结果写进一个文本框里面?