Matlab gui to m and vice versa?

  • MATLAB
  • Thread starter smokeput
  • Start date
  • Tags
    Gui Matlab
In summary: MATLAB function: by specifying the name of the .m file and the function name. If you're wanting to share data between functions, you'll need to use the 'stdio' library:http://www.mathworks.com/help/techdoc/ref/stdio.html
  • #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
 
Physics news on Phys.org
  • #2
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

The first hit I got when I Googled for MATLAB GUI was this tutorial, which seems to be quite popular, given that even the first page has several hundred comments.
http://blinkdagger.com/matlab/matlab-gui-graphical-user-interface-tutorial-for-beginners/

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

Go forth, grasshopper.
 
  • #3


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


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.
 
  • #5


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


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.
 
  • #7


smokeput said:
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 [Broken]

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.

MATLAB *does* allow you to use global variables, but global variables are nearly universally frowned upon as bad programming practice:
http://www.mathworks.com/help/techdoc/ref/global.html

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. :biggrin:

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!
 
Last edited by a moderator:

1. What is a Matlab GUI?

A Matlab GUI (Graphical User Interface) is a tool that allows users to interact with Matlab programs through a visual interface, rather than writing code. It typically includes buttons, menus, and other graphical elements that can be clicked on to perform specific actions or inputs.

2. How do I convert a Matlab GUI to M-file?

To convert a Matlab GUI to an M-file, you can use the "GUIDE" tool within Matlab. This tool allows you to create a GUI through a visual interface, and then generates the corresponding M-file code for that GUI. You can then use this code to modify or customize your GUI further.

3. Can I convert an M-file to a Matlab GUI?

Yes, you can convert an M-file to a Matlab GUI using the "GUIDE" tool. Simply copy and paste your M-file code into the tool, and it will generate a GUI based on that code. Keep in mind that some modifications may be needed to ensure the GUI functions properly.

4. What are the advantages of using a Matlab GUI?

Using a Matlab GUI can make your program more user-friendly and intuitive, as it allows for a visual interface rather than just code. This can also make it easier for non-programmers to use your program. Additionally, a GUI can help with data visualization and organization, and can make your program more visually appealing.

5. Can I create a Matlab GUI without using the "GUIDE" tool?

Yes, you can create a Matlab GUI without using the "GUIDE" tool. You can write the code for your GUI manually using Matlab's built-in functions and syntax, or you can use external tools and libraries to create a GUI. However, using the "GUIDE" tool may be easier and more efficient for beginners or those not familiar with GUI programming in Matlab.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
16K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
5K
  • Programming and Computer Science
Replies
7
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
17K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
1K
Back
Top