MATLAB Answers

Dividing programmatic GUI into functions.

2 views (last 30 days)
Seyhan Emre Gorucu
Seyhan Emre Gorucu on 1 Jun 2012
Hello,
I am writing a GUI programmatically. As the number of lines reached 3000 I decided to divide the code into separate parts. I copy pasted some part of the working main function into a new m file. The new function starts as below:
function [varargout]=MMPCalculations(varargin)
I call this function from the main file as:
[varargout]=MMPCalculations(varargin)
It gives error. This function is not a callback function or so. This is just a part of code written in a separate m file. I want everything created in the main file visible to the function and everything created/changed in the new file/function visible to the main file. Therefore, I have used varargin, varargout. However, It did not work. Can you please help me?
Thank you,

  0 Comments

Sign in to comment.

Answers (2)

Walter Roberson
Walter Roberson on 1 Jun 2012
If you want "everything created in the main file visible to the function and everything created/changed in the new file/function visible to the main file" then you should be using a script rather than a function.

  2 Comments

Seyhan Emre Gorucu
Seyhan Emre Gorucu on 2 Jun 2012
Thank you so much but it gave another error.
Attempt to add "intro" to a static workspace.
See MATLAB Programming, Restrictions on Assigning to Variables for details.
Error in MMPCalculations (line 9)
intro={'CO2','Nitrogen&Flue Gas','Enriched Gas','Methane'};
Error in Main (line 1027)
MMPCalculations
Basically, MMPcalculations is the name of the script and intro is a cell type argument. Please let me know if you need more information. Thank you.
Walter Roberson
Walter Roberson on 2 Jun 2012
Remove the last "end" statement from all functions defined in Main.m
When you have an "end" statement matching a "function" statement then it is incompatible to use a call to a script that defines any new variable. Removing those matching "end" statements goes back to functions that are allowed to call scripts that define new variables.

Sign in to comment.


Seyhan Emre Gorucu
Seyhan Emre Gorucu on 4 Jun 2012
Hello thanks a lot for the reply again. I wasn't able to reach my computer during the weekend. My problem is that my functions are nested. When the functions are nested I have to put end at the end of the function. Is there anyway I have both nested function built in main.m and call a script from there?

  1 Comment

Walter Roberson
Walter Roberson on 4 Jun 2012
No, using nested functions is not compatible with "everything created in the main file visible to the function and everything created/changed in the new file/function visible to the main file". The difficult part is the "everything created/changed in the new file/function visible to the main file": the only MATLAB mechanism that can promise that is a "script" being run from an old-style function.
One of the main purposes of functions is to *prevent* the called function from interfering with the variables in the calling function, with the only linkage being the values returned by the called function. If your called function needs to change variables in the calling function then you do not have a good program design.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by