<div dir="ltr"><div>Hallo Claus,</div><div><br></div><div>from what I understand the question is: How can GUI updated outside of the function "calc" ?</div><div><br></div><div>In other words:</div><div>right now "calc" updates the GUI.</div><div>I guess the aim is that "calc" only returns output values, which will be used to update the GUI .</div><div><br></div><div>Way 1:<br></div><div>Use global variables, which can be used in every function of the GUI.<br></div><div>The return value of the function will change this global variable.<br></div><div><br></div><div>Way 2</div><div>Give the calc() function output variables, such as:</div><div>function [out_var1, out_var2] = calc(arg1, arg2)</div><div>    out_var1 = arg1 + ...</div><div>    out_var2 = arg2 +....</div><div>endfunction</div><div>With this it should be possible to update the GUI outside of a callback function.</div><div><br></div><div>Way 3.....similar to way 1...and the way I did in the past</div><div>use a single global variable, called: handles</div><div><br></div><div>handles is more a structure, than a single variable.</div><div>It contains all uicontrols aswell as all variables necessary to update the GUI.</div><div>e.g.:</div><div><br></div><div>handles.f1 = figure(....)      // that would be the main GUI</div><div>handles.f2 = figure(....)      // that would be a sub GUI</div><div></div><div>handles.pb1 = uicontrol()   // that would be a push button of the GUI</div><div>handles.var1 = ....             // that would be a variable, necessary to keep available to all uicontrols</div><div>handles.var2 = ....             
 // that would be another variable, necessary to keep available to all uicontrols

</div><div><br></div><div><br></div><div>When definig the callback function it would look like this:</div><div><br></div><div>function calc(handles)</div><div>  locVar1 = handles.var1</div><div>  locVar2 = handles.var2</div><div></div><div>  //...do the calculations...to change locVar1, locVar2<br></div><div>  //...update the handle variable(s)</div><div></div><div>  handles.var1 = locVar1</div><div>  handles.var2 = locVar2</div><div></div><div>  handles = resume(handles)</div><div>endfunction<br></div><div>  <br></div><div>one would call the function just as:     calc(handles)<br></div><div><br></div><div></div><div><br></div><div>A general note:</div><div>It helps to put all callback functions into a separate subfolder</div><div>The main function (that only defines the GUI) will call and load all callback functions from that subfolder.</div><div><br></div><div>I give callback function scripts the extension:  *.sci</div><div>I give the main GUI script the extension: *.sce</div><div></div><div>This is all not mandatory, but increases the overview.<br></div><div><br></div><div>
<div>To create the global variable "handles" I define a separate function:</div><div><br></div><div>function handles = Init(handles)</div><div>  handles.var1 = 0  // init value of var1</div><div>  handles.var2 = 0  // init value of var2</div><div>  handles.cmap = graycolormap(255)  // init GUI colormap</div><div>  handles.statusSlider = 0    // can be a status variable for a slider..if it has been used or not<br></div><div>  handles.statusPB = 0       // can be a status variable for a push bitton ..such as: has been pressed or not<br></div><div>  // etc...</div><div><br></div><div>  handles = resume(handles)</div><div>endfunction</div>

</div><div><br></div><div>sorry for the long Mail,</div><div>Best regards,</div><div>Philipp<br></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">Am Do., 28. Nov. 2019 um 08:57 Uhr schrieb Antoine Monmayrant <<a href="mailto:amonmayr@laas.fr" target="_blank">amonmayr@laas.fr</a>>:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Hello Claus,<br>
<br>
I've been playing a bit with GUIs for teaching, so maybe I can help.<br>
The issue is that I don't get what your problem is exactly here, so I my answer might be a bit off-topic.<br>
Do not hesitate to rephrase your issue, I'll try a more focused answer.<br>
<br>
Anywya, here is how I think your code could be improved:<br>
<br>
(1) You can use 'calc' as the callback for all of the editable text field so that your result gets shown right away, without having to press a button.<br>
(2) You should populate all the uicontrols of your gui first, then only update the displayed values, the visibility of the uicontrols, etc inside your calc function. (ie no more creating a uicontrol inside a callback)<br>
(3) The 'tag' property of any uicontrol together with 'findobj()' are really nice to get/set the properties of each existing uicontrol.<br>
(4) You can rely on a proper Model-View-Controler approach (or any other well established method to avoid mixing gui stuff with calculation stuff).<br>
<br>
I attached a small gui I use to illustrate optical anti-reflection coating.<br>
It is far from perfect (I did not implement a proper model-view-controler for example).<br>
But you can see how I tried to separate the different parts of the code an how I use findobj/tag/get/set,  etc.<br>
<br>
Hope it helps,<br>
<br>
Antoine<br>
<br>
Le Mercredi, Novembre 27, 2019 19:21 CET, Claus Futtrup <<a href="mailto:cfuttrup@gmail.com" target="_blank">cfuttrup@gmail.com</a>> a écrit: <br>
<br>
> Hi there<br>
> <br>
> I'm trying to build a GUI. For simplicity on the forum, I've built a <br>
> really simple example of what I'm trying to do. How can I make the <br>
> "conditional" GUI output work and not have it inside the calc function? <br>
> ... or, how would you do it? Thanks.<br>
> <br>
> Best regards, Claus.<br>
> <br>
> // GUI_EXAMPLE.SCE<br>
> //<br>
> // Demo of how to build a simple GUI in Scilab.<br>
> // Real simple, with two input variables and one output.<br>
> // The example uses the basic mechanical example of a mass and a spring as<br>
> // input parameters and calculates the resonance frequency of the mechanical<br>
> // system.<br>
> <br>
> // Initialize variables<br>
> m  =  1;  // Moving mass 'm'(kilogram)<br>
> k  =  1;  // Stiffness, spring constant 'k'(Newton per meter)<br>
> fres  =  1;  // Resonance frequency (Hertz)<br>
> show_result  =  %f;<br>
> <br>
> function  calc()<br>
>      m  =  evstr(get(ge_m,"string"));  // get content in uicontrol ge_m<br>
>      k  =  evstr(get(ge_k,"string"));  // get content in uicontrol ge_k<br>
>      fres  =  sqrt(m  *  k);<br>
>      // putting GUI updates inside the calculation routine is not pretty code.<br>
>      uicontrol("style","text","string","Result :","position",  ..<br>
>                [10  as(2)-110  80  20]);<br>
>      uicontrol("style","text","string",string(fres),  ..<br>
>                "position",[100  as(2)-110  80  20]);<br>
>      uicontrol("style","text","string","Hz ","position",  ..<br>
>                [200  as(2)-110  30  20]);<br>
>      show_result  =  %t;<br>
>      // update global variables<br>
>      [m,k,fres,show_result]=return(m,k,fres,show_result);<br>
> endfunction<br>
> <br>
> function  goodbye()<br>
>      close(ge);  // Close GUI window<br>
>      printf("Resulting fres: %f Hertz\n",fres);<br>
>      abort  // Print result in console (e.g. for copy/paste), then kill the app<br>
> endfunction<br>
> <br>
> ge  =  scf();  // GUI Example, Initialize and 'set current figure'<br>
> as  =  ge.axes_size;  // read size of window, as = [width height]<br>
> ge.figure_name  =  "GUI Example";  // Change window header<br>
> <br>
> uicontrol("style","text","string","Moving mass :","position",  ..<br>
>            [10  as(2)-35  80  20],"background",[1  1  1]);  // white background<br>
>            // position properties has four parameters = x,y,width,height<br>
>            // y-position counts from lower left corner, so we subtract from 'as'<br>
> ge_m  =  uicontrol("style","edit","string",string(m),  ..<br>
>            "position",[100  as(2)-35  80  20]);<br>
> uicontrol("style","text","string","kg ","position",  ..<br>
>            [200  as(2)-35  30  20],"background",[1  1  1]);<br>
> <br>
> uicontrol("style","text","string","Stiffness :","position",  ..<br>
>            [10  as(2)-60  80  20],"background",[1  1  1]);<br>
> ge_k  =  uicontrol("style","edit","string",string(k),  ..<br>
>            "position",[100  as(2)-60  80  20]);<br>
> uicontrol("style","text","string","N/m ","position",  ..<br>
>            [200  as(2)-60  30  20],"background",[1  1  1]);<br>
> <br>
> uicontrol("style","pushbutton","string","Calculate",  ..<br>
>            "position",[10  as(2)-85  80  20],"callback","calc");<br>
> <br>
> // How do I make this "conditional"output show up in my GUI?<br>
> if  show_result  then  // If "Calculate"button was pushed at least once ...<br>
>      uicontrol("style","text","string","Result :","position",  ..<br>
>                [10  as(2)-110  80  20]);<br>
>      uicontrol("style","text","string",string(fres),  ..<br>
>                "position",[100  as(2)-110  80  20]);<br>
>      uicontrol("style","text","string","Hz ","position",  ..<br>
>                [200  as(2)-110  30  20]);<br>
> end<br>
> <br>
> uicontrol("style","pushbutton","string","Exit",  ..<br>
>            "position",[10  as(2)-135  80  20],"callback","goodbye");<br>
><br>
_______________________________________________<br>
users mailing list<br>
<a href="mailto:users@lists.scilab.org" target="_blank">users@lists.scilab.org</a><br>
<a href="http://lists.scilab.org/mailman/listinfo/users" rel="noreferrer" target="_blank">http://lists.scilab.org/mailman/listinfo/users</a><br>
</blockquote></div>