0

So I have an m. file script that contains 2 functions.
The first one (that's loaded when I run the script) and the second.
Each of them has a different GUI setup and different text in text boxes (sorry for tautology).

My program is about calculating stuff using economic formulas, and the final version of the program is going to have about 50 formulas in it. AND I don't want to make 50 separate scripts for each formula. What I want is to be able to switch between formulas inside one script.
So I made a specific push button for that purpose (code below) but when I press it nothing happens.

Can someone who's experienced enough tell me what did I do wrong? (I'm new to MATLAB).
Let me know if more information is needed, or the question is not clear enough. Thanks in advance!

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);

Here's the whole script:

    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
6
  • 1
    The one line you supplied us with looks fine! But what does the callback function (SecondScript) do? Commented Jul 2, 2015 at 14:16
  • It loads entirely different program (calculator) with its unique interface and a dozen parameters to calculate. But it doesn't work! And what surprises me most, MATLAB doesn't display any error messages. Commented Jul 2, 2015 at 14:44
  • Did you try setting a breakpoint into SecondScript code and step in? Since the problem seems in this function, perhaps you might post it. Commented Jul 2, 2015 at 15:17
  • Okay I posted the whole script, hope this helps. Commented Jul 2, 2015 at 15:26
  • You still haven't posted the SecondScript-function, as I (as well as il_raffa) mentioned before the problem seems to be with that function. Commented Jul 3, 2015 at 8:44

2 Answers 2

0

I have a trick, although i myself find it not very good-looking:

Assuming you put all of these functions inside a file myforms.m without any blank line. Then, at the beginning of your GUI, open and read your file:

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

alllines{1} is now a cell array, and each element is one line from myforms.m.

Now, when you want to switch to a formula, you need to know the line number of its section in myforms.m, for example starting from line 10, ending at line 15.

Create a new file, i.e callme.m which should be called by your button, and write these lines to the file:

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

Your callback function can then be @callme.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the answer! But I don't understand what do you mean by "...at the beginning of your GUI..." I only have 2 m. files (myforms.m and callme.m). Are you talking about .fig file or some other matlab file extension I'm not yet aware of? This might sound stupid, but where exactly do I have to put this code you gave me?
0

I've put source and eventdata in a set of parentheses directly after the function's name and it solved the problem! I can now easily switch between formulas as fast as I can push the button.

Here's how my function's name looked like before:

function SecondScript

Here's how it looks now:

function SecondScript(source,eventdata)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.