To link a MATLAB GUI with a separate .m file for calculations, the GUI's pushbutton callback function should call the desired .m file as a function, passing any necessary inputs. The .m file should be structured to accept these inputs and return results back to the GUI. It's essential to ensure that both the GUI and the .m file are in the same directory for proper access. Understanding variable scope is crucial, as variables defined in functions are not accessible outside unless explicitly returned. For effective integration, it's recommended to focus on writing functions rather than scripts, allowing for better data handling and project scalability.
#1
smokeput
7
0
matlab gui to m and vice versa?
Hi guys, i am currently deferring the project and doing a GUI for it.
but I am trying to disect the workload and try from the basics
i would like to enquire how do i actually link up gui m file and a normal m file?
lets say i want to evaluate the addition of 2 values.
i put 2 text box in the gui and asked user to insert the values.
and i want to link up a "ADD" pushbutton to another m file in the same directory path by which the maths is done in the m file.
what should i do??
i know this is very very basic to most of u out there. hope to hear ur advice soon. cheers
There are several tutorials out there on MATLAB GUI making. When I had to make one a few years back, I used the Mathworks' own getting started guide:
http://www.mathworks.com/help/techdoc/creating_guis/bqz79mu.html#creating_guis
Actually, the adder GUI example you're trying to build is addressed as the first example in the tutorial above (use the MATLAB GUIDE--GUI Integrated Development Environment to get started, quick and dirty-like).
As with most UIs, you have a pretty-looking graphical interface with buttons, knobs, graphs, etc. that you design (in this case, the .fig file you create using the editor). Then you have another file (the .m file) which contains all the callback functions--the MATLAB code that does the adding, or graphing, or whatever.
When you start the UI, or press a button, or perform some sort of action (even if the 'action' is just a timer that runs down if you don't click on anything), one of the callback functions executes. If you lay down a button in the .fig using the GUIDE, you should have a callback function templated for you in the .m file (if I recall correctly).
Based on your other thread (I've asked a mod to merge / partition the two based on their discretion), I'm operating on the assumption that you're trying to make code for your GUI.
The GUI should be named, for instance, foo.fig (a figure) while the code for the GUI should be named foo.m--the only difference in the filenames should be in the extension.
There's some ambiguity in your phrasing that leads me to think that you may be confusing the .m with the .fig--dump all your GUI code in the .m (unless one of your buttons executes another .m file, or functions contained there-in).
#4
smokeput
7
0
nonono. sorry for all the confusion. yeap of cos i have googled my homework and all. have also tried the blinkdagger tutorial.
i understand that by creating the .fig thru the GUIBuilder, its corresponding .m file will be created when the gui is saved.
yes, i understand ur point that all calculations can be done in the pushbutton callback for example.
however, the reason why i am asking "how to send input typed in the gui to another .m file/function in the same directory (not a gui) and agn ltr returning the values back into the gui" is because its for a bigger version of the project I'm currently tasked to do. i am asking this only because i thought it would be the simplest way for me to break down the project. who knows both uses the same concept and ways to do it.
I assume that you mean that you want to share data between functions, and not literally rewrite a .m file? (Though there is a way to do that, via file access).
Generally, MATLAB is sequential--only one function / script / program operates at any given time. As far as I'm aware, it's not really a multitasking environment. Thus, your question (and resultant answer) is either really simple, or really complicated. Since I don't know your level of understanding, please forgive me if I'm patronizing. Actually, before I go even that far, have you taken a basic programming course before (concepts like function, function handle, variable scope)?
For the really simple concept: if you're asking, for instance how to send a value for A and a value for B to an m-file that adds the two of them together, you'd write the .m file as a function that accepts two inputs, and returns the sum to whatever function called it in the first place: http://www.mathworks.com/help/techdoc/ref/function.html
As long as the .m file that contains the function is in the active directory, it'll be in the namespace of MATLAB (i.e. you can call it whenever, wherever you happen to be). For example, if you write a bunch of functions and slap them in a .m file, you can call them from the GUI's .m file.
For the more complex answer: you need to write (or re-write) a main function or .m that either calls all your other functions, or allows for global scope for all your data.
#6
smokeput
7
0
yeap. share data between gui and .m files. not actually rewriting a .m file from scratch.
frankly my programming skills is vry basic i would say. learned some of it in my courses ( i am in engineering) but no interest in them. however, i am now desperately finding for solutions to my problem as i need to get it done asap =(
i've been reading up some stuffs with regards to the gui too. tho may not be indepth at all.
im currently loading the mathwork function site u linked to me. but how do i direct the GUI to the .m files? by just writing the filename.m? or global filename.m? or userdata, getappdata, setappdata and such? or write a = function name of the .m file?
yeap. share data between gui and .m files. not actually rewriting a .m file from scratch.
frankly my programming skills is vry basic i would say. learned some of it in my courses ( i am in engineering) but no interest in them. however, i am now desperately finding for solutions to my problem as i need to get it done asap =(
i've been reading up some stuffs with regards to the gui too. tho may not be indepth at all.
im currently loading the mathwork function site u linked to me. but how do i direct the GUI to the .m files? by just writing the filename.m? or global filename.m? or userdata, getappdata, setappdata and such? or write a = function name of the .m file?
pardon me for my very basic questions.
I would strongly advise you read through the Programming Fundamentals section of the Getting Started Guide:
http://www.mathworks.com/help/techdoc/matlab_prog/bqjgwp9.html
Focus there instead of on your GUI: the fundamentals you learn there are necessary for your GUI.
A .m file can be just a script (as you're probably used to writing them) or can contain formally declared functions (as per the page you may or may no longer be reading).
Variables (in every programming language I'm familiar with) have scope. The data contained in a variable (or even the existence of the variable itself) is only valid / accessible in certain parts of the code. A variable introduced in a function is only valid inside the function (including your 'main' function). A variable introduced outside these has global scope (i.e. is accessible to any function).
In MATLAB, you have your standard workspace. You type in A=[1,2,3], you've created yourself a variable named A in this workspace. If you write yourself a straight .m file (no function declaration or any other fanciness--just a script) it can access, and modify A, since it runs in your workspace. It's a script, and behaves just as if you typed the contents of that .m script yourself.
However, if you call a function (and the function just happens to also have a variable named A) this function will not touch the A in the workspace--its version of A only has function scope, and exists and is accessible only while the function runs.
There are more variations on the theme, but basically,the only way you modify A using a function is if you assign A as the output of the function. A = sin(A), for instance will take the original A values, and then assign the sine of these as the 'new' A.
When you write a MATLAB script (a straight .m file--let's call it foo.m), and save it in your working directory (and of course, set the 'current' directory to the working directory--this is all default behaviour, by the way) you can access it by typing its name at the command line--the script will then run:
>> foo
Now let's say you write yourself two functions, bob (which doubles every element of the input) and joe (which halves all the elements of the input), and save these inside bar.m:
Code:
function B = bob(A)
B=A.*2
function B = joe(A)
B=A./2
As long as bar.m is in the working directory, you can call either the bob or joe functions. Functions contained within .m files can be accessed globally (as long as the current directory is the right one)--whether in a function or not (however, the variables contained within cannot, and disappear as soon as the function finishes running).
What you have to do is to stop writing scripts, and start writing functions. Or start making global variables. If, as you say, you're writing code that has to interface with a big project, you'd better start writing functions.
It's tremendously difficult for me / us to teach basics--you'll need to do a lot of the legwork yourself, or call some of your coworkers to give you a hand with some of the concepts. Hopefully, this starts you on your way!