2015-07-02 79 views
0

所以我有一个m。包含2个函数的文件脚本。
第一个(当我运行脚本时加载)和第二个。
他们每个人都有不同的GUI设置和文本框中的不同文本(对于重言式抱歉)。在多个函数之间切换MATLAB

我的程序是关于使用经济公式计算的东西,程序的最终版本将有大约50个公式。而且我不想为每个配方制作50个独立的脚本。我想要的是能够在一个脚本内的公式之间切换。
所以我做了一个特定的按钮(代码如下),但是当我按下它时什么都没有发生。

经验丰富的人能否告诉我我做错了什么? (我是MATLAB新手)。
让我知道是否需要更多信息,或者问题不够清楚。提前致谢!

uicontrol('Style','pushbutton','Position',[136,88,194,27],'String','Next formula','FontSize',10,'FontName','MS Reference Sans Serif','BackgroundColor',[0.756862759590149 0.866666674613953 0.776470601558685],'CallBack',@SecondScript); 

这里是整个脚本:

function FirstScript 

clc 
clear 
close all 

global ZatratyNaSozdanieProgProdukta hEditZzpspp hEditZmvspp hEditZobsh 

ScreenSize = get(0,'ScreenSize'); 
set (0, 'DefaultFigureColor', [0.23137255012989 0.443137258291245 0.337254911661148]) 

hFig = figure('Visible','off','Position',[ScreenSize(3)/2,ScreenSize(4)/2,550,450]); 

uicontrol('Style','Pushbutton','Position',[371,136,98,27],'String','Рассчитать','FontSize',10,'FontName','MS Reference Sans Serif','Callback',@CalculateCallback,'BackgroundColor',[0.756862759590149 0.866666674613953 0.776470601558685]); 

uicontrol('Style','pushbutton','Position',[136,88,194,27],'String','Next formula','FontSize',10,'FontName','MS Reference Sans Serif','BackgroundColor',[0.756862759590149 0.866666674613953 0.776470601558685],'CallBack',@SecondScript); 

axes('units','pixels','position',[20 100 200 24],'visible','off'); 
message = sprintf('Формула определения затрат на\nсоздание программного продукта:\n \nЗ^З^П_С_П_П+З^М^В_С_П_П+З_О_Б_Щ'); 
text(0,4.6,message,'interpreter','tex','Position',[1.18 8.64166666666667 0],'HorizontalAlignment','center','FontSize',12,'FontName','MS Reference Sans Serif','BackgroundColor',[0.756862759590149 0.866666674613953 0.776470601558685]); 

axes('units','pixels','position',[20 100 200 24],'visible','off'); 
hTextZzpspp = text(0,4.6,'З^З^П_С_П_П','interpreter','tex','Position',[0.55 4.14166666666666 0],'FontSize',12,'FontName','MS Reference Sans Serif','BackgroundColor',[0.756862759590149 0.866666674613953 0.776470601558685]); 
hEditZzpspp = uicontrol('Style','Edit','Position',[117,150,72,25],'String','','FontSize',10,'FontName','MS Reference Sans Serif'); 

axes('units','pixels','position',[20 100 200 24],'visible','off'); 
hTextZmvspp = text(0,4.6,'З^М^В_С_П_П','interpreter','tex','Position',[0.935 4.14166666666666 0],'FontSize',12,'FontName','MS Reference Sans Serif','BackgroundColor',[0.756862759590149 0.866666674613953 0.776470601558685]); 
hEditZmvspp = uicontrol('Style','Edit','Position',[195,150,72,25],'String','','FontSize',10,'FontName','MS Reference Sans Serif'); 

axes('units','pixels','position',[20 100 200 24],'visible','off'); 
hTextZobsh = text(0,4.6,'З_О_Б_Щ','interpreter','tex','Position',[1.32 4.05833333333332 0],'FontSize',12,'FontName','MS Reference Sans Serif','BackgroundColor',[0.756862759590149 0.866666674613953 0.776470601558685]); 
hEditZobsh = uicontrol('Style','Edit','Position',[274,150,72,25],'String','','FontSize',10,'FontName','MS Reference Sans Serif'); 

uicontrol('Style','Text','Position',[370,191,100,29],'String','Результат:','FontSize',12,'FontName','MS Reference Sans Serif','BackgroundColor',[0.756862759590149 0.866666674613953 0.776470601558685]); 
ZatratyNaSozdanieProgProdukta = uicontrol('Style','Text','Position',[370,168,100,23],'String','','FontSize',10,'FontName','MS Reference Sans Serif'); 


set(hFig,'Visible','on') 
    function CalculateCallback(~,~) 
     Zzpspp = str2double(get(hEditZzpspp,'String')); 
     Zmvspp = str2double(get(hEditZmvspp,'String')); 
     Zobsh = str2double(get(hEditZobsh,'String')); 

     Calculation = Zzpspp+Zmvspp+Zobsh; 

     set(ZatratyNaSozdanieProgProdukta,'String',sprintf('%0.2f',Calculation));  


    end 


end 
function SecondScript 

clc 
clear 
close all 

global RashodyNaOplatuTrudaRazrabotchikaProgrammy hEditZosnzp hEditZdopzp hEditZotchzp 

ScreenSize = get(0,'ScreenSize'); 
set (0, 'DefaultFigureColor', [0.23137255012989 0.443137258291245 0.337254911661148]) 

hFig = figure('Visible','off','Position',[ScreenSize(3)/2,ScreenSize(4)/2,550,450]); 

uicontrol('Style','Pushbutton','Position',[371,136,98,27],'String','Рассчитать','FontSize',10,'FontName','MS Reference Sans Serif','Callback',@CalculateCallback,'BackgroundColor',[0.756862759590149 0.866666674613953 0.776470601558685]); 

axes('units','pixels','position',[20 100 200 24],'visible','off'); 
message = sprintf('Формула определения расходов на\nоплату труда разработчика программы:\n \nЗ^З^П_С_П_П+З^М^В_С_П_П+З_О_Б_Щ'); 
text(0,4.6,message,'interpreter','tex','Position',[1.18 8.64166666666667 0],'HorizontalAlignment','center','FontSize',12,'FontName','MS Reference Sans Serif','BackgroundColor',[0.756862759590149 0.866666674613953 0.776470601558685]); 

axes('units','pixels','position',[20 100 200 24],'visible','off'); 
hTextZosnzp = text(0,4.6,'З^З^П_С_П_П','interpreter','tex','Position',[0.55 4.14166666666666 0],'FontSize',12,'FontName','MS Reference Sans Serif','BackgroundColor',[0.756862759590149 0.866666674613953 0.776470601558685]); 
hEditZosnzp = uicontrol('Style','Edit','Position',[117,150,72,25],'String','','FontSize',10,'FontName','MS Reference Sans Serif'); 

axes('units','pixels','position',[20 100 200 24],'visible','off'); 
hTextZdopzp = text(0,4.6,'З^М^В_С_П_П','interpreter','tex','Position',[0.935 4.14166666666666 0],'FontSize',12,'FontName','MS Reference Sans Serif','BackgroundColor',[0.756862759590149 0.866666674613953 0.776470601558685]); 
hEditZdopzp = uicontrol('Style','Edit','Position',[195,150,72,25],'String','','FontSize',10,'FontName','MS Reference Sans Serif'); 

axes('units','pixels','position',[20 100 200 24],'visible','off'); 
hTextZotchzp = text(0,4.6,'З_О_Б_Щ','interpreter','tex','Position',[1.32 4.05833333333332 0],'FontSize',12,'FontName','MS Reference Sans Serif','BackgroundColor',[0.756862759590149 0.866666674613953 0.776470601558685]); 
hEditZotchzp = uicontrol('Style','Edit','Position',[274,150,72,25],'String','','FontSize',10,'FontName','MS Reference Sans Serif'); 

uicontrol('Style','Text','Position',[370,191,100,29],'String','Результат:','FontSize',12,'FontName','MS Reference Sans Serif','BackgroundColor',[0.756862759590149 0.866666674613953 0.776470601558685]); 
RashodyNaOplatuTrudaRazrabotchikaProgrammy = uicontrol('Style','Text','Position',[370,168,100,23],'String','','FontSize',10,'FontName','MS Reference Sans Serif'); 


set(hFig,'Visible','on') 
    function CalculateCallback(~,~) 
     Zosnzp = str2double(get(hEditZosnzp,'String')); 
     Zdopzp = str2double(get(hEditZdopzp,'String')); 
     Zotchzp = str2double(get(hEditZotchzp,'String')); 

     Calculation = Zosnzp+Zdopzp+Zotchzp; 

     set(RashodyNaOplatuTrudaRazrabotchikaProgrammy,'String',sprintf('%0.2f',Calculation));  


    end 


end 
+1

的一个符合看起来不错!但回调函数('SecondScript')是做什么的? –

+0

它加载完全不同的程序(计算器)与其独特的界面和十几个参数来计算。但它不起作用!而最让我惊讶的是,MATLAB不显示任何错误消息。 – Johnnik

+0

您是否尝试将断点设置为'SecondScript'代码并加入?由于这个问题似乎在这个功能,也许你可能会发布它。 –

回答

0

我已经把sourceeventdata放在函数名后面的一组括号中,它解决了问题!我现在可以像按下按钮一样快速地在公式之间切换。

这是我的函数的名字是什么样子像以前一样:

function SecondScript 

下面是它现在的样子:您提供给我们

function SecondScript(source,eventdata) 
0

我有一招,虽然我自己觉得不太好看:

假设你把所有这些功能的文件myforms.m没有任何空行。然后,在你的图形用户界面,开放的起点和阅读您的文件:

f1 = fopen('myforms.m'); 
alllines = textscan(f1, '%s', 'Delimiter', ''); 
fclose(f1); 

alllines{1}现在是一个单元阵列,每个元素是从myforms.m一行。

现在,当你想切换到一个公式,你需要知道它在myforms.m部分的行数,例如10号线开始,在15行

结束创建一个新的文件,即callme.m应该由你的按钮调用,并写这几行到文件中:然后

f2 = fopen('callme.m', 'w'); 
for i = 10:15 
    fprintf(f2, '%s\n', alllines{1}{i}); 
end 
fclose(f2); 

回调函数可以@callme

+0

感谢您的回答!但我不明白你的意思是“......在你的GUI开始时......”我只有2米。文件(myforms.m和callme.m)。你在谈论.fig文件还是我还没有意识到的其他一些matlab文件扩展名?这可能听起来很愚蠢,但我必须把这些代码放在哪里? – Johnnik