How to find call an external function from a GUI in MATLAB? - matlab

I have an external function (myfun.m) which will generate 4 images. I want to use a textbox in a GUI to give the function the input parameters (e.g. the name of the original picture), but I have no clue how I can run myfun.m in my GUI. PLease help

First, I would recommend having a different textbox for each input to your function. For example if your function asks for 4 inputs, I would have four textboxes. This will avoid pain on the user's end to format their text input a certain way, as well as pain on your end on parsing a long text string into several inputs.
Second, if your function is within MATLAB's path, then you can call that directly from your GUI no problem just as you would any of MATLAB's built-in functions. You probably want to make a push-button that has a callback to execute that functions.
The way the callback for this function should look is to l

Related

Matlab: how to show user possible function inputs like Matlab does using help dropdown

So in Matlab, if you start to type in a function like find( and then wait after the open parentheses, a small yellow popup will appear showing potential input options, like:
find(X)
find(X,K)
find(X,n,direction)
find(__)
So I'm wondering, is it possible to set up my own function so the same happens? I tried copying the find file in the format, so mine looks like this:
%MY_FUNCTION Description of function
% O = MY_FUNCTION(X) returns the output based on the input X.
%
% O = MY_FUNCTION(X,Y) returns the output based on the input Y.
But after saving it, when I type my_function(, all it shows is my_function(...). Is it just not possible for user functions? Thanks for any input!
It is possible to set up these hints for custom functions, but you can't do it in the function .m file. Instead, you need to put the information in a separate functionSignatures.json file in the same directory as the custom function. You can find the official documentation and file specification here.

How can I create multiple inputs for a matlab function block?

I want to restrict the variable that I use as input for a matlab function block, it must be only able to increase.
To achieve that i have tried to compare the variable and the previous sample of it in a matlab function, but i don't know how to create two inputs. To solve that i've tried to use a mux, but then i get an error. And google doesn't give me an explanation how to use a mux signal as input for a matlab function.
So that leaves me here with this low-level question.
Thanks in advance for your help and time. Cheers.
To use multiple variables in a function, you need to modify your function declaration at the first line of your function. The reference syntax is:
function [y1,...,yN] = myfun(x1,...,xM)
where x1 through xM are inputs. Your declaration with two inputs might look something like:
function [returnValue] = hasIncreased(previousSample, variable)
See the Matlab Function Documentation for more information.

MATLAB display a link to workspace elements

I'm trying to improve the readability of my outputs. In order to avoid displaying a lot of data, it would be nice to display links that point to specific elements in the workspace (i.e. a variable, a table, a figure, etc.).
Here is a picture to illustrate the idea:
Maybe we can use the disp function, as I know it allows the generation of hyperlinks to a webpage or a file stored in the computer.
Is this possible in MATLAB?
OK, so this is what I came up with. The first thing is to use the openvar function and you specify the variable you want wrapped around in single quotations. This will open up the variable in the Variable Editor (the image that is pictured in your snapshot).
Now, you can also use disp to allow clickable links to run MATLAB commands. Using these two ideas, you would combine the disp linking and openvar to allow a clickable link to execute the openvar function to display your desired variable.
As such, you would basically do this assuming our variable is stored in A:
A = magic(5);
disp('Click on me to show the matrix A')
The disp statement will show you a clickable link, and the desired function to be executed only runs if you click on the link. You can achieve this desired effect by specifying the matlab: keyword inside the URL in the href key, then after it, you write out whatever MATLAB function or statements you want to use. In our case, we use only need to run one function, and that's openvar. Make sure you specify single quotes around the variable you want inside the argument to openvar. The reason why is because the argument to disp is a string, and if you want single quotations to be recognized, you must use a pair of single quotes. As such, in the disp string, there are pairs of single quotes around the variable you want.
Here's the what I get in MATLAB. The steps are reproduced and shown in an animated GIF:

matlab function parser on gui

I need to do a parser on my gui in matlab in which you can type your own function formula that will be used for computations. I came up with to make function that gets function from edit box as a string and saves it to m-file as a function. But any further calling changed file do not bring expected results if the first form of function were stored in memory. Do you have any other ideas to solve it?
Instead of writing the code to an m-file, take a look at the eval function.

When using a multiple-output matlab function, do i need to callback all variables?

When using a multiple-output matlab function, do i need to callback all variables? or can I just take the first two variables? (if so..is it not recommended?)
lets say in function.m
[a, b, c] = function( )
in main.m
[var1, var2] = function;
When calling (almost) any function in matlab you can request fewer outputs than it specifies. So, yes the example you give should work perfectly fine.
There are some clever things you can do with this, such as using nargout within a function to see how many output arguments have been requested and only calculating the values that have been requested as an optimisation trick.
It depends on the definition of the function, and exactly which of the outputs you want to get.
Not all the function allow to do it, you can find all the options for each function in the beginning of the help documentation on the specific function.
If you want only the 2nd, or 3rd outputs, and you want also to save the computation-time of the results that does not interesting, you can use ~ option, like this (for versions 2009b and later):
[~, var1, var2]=function
Many functions allow for options to passed that change how the function behaves. I used/wrote various numerical solving functions a bit and one that nice amount of option, for instance is the LSMR function(s).
Otherwise, if you can manipulate the original either introduce an input(s) to do so before or at the end with an inline subroutine to generate the outputs you want.
Or if you can't it will return as either a cell array or a vector and you can pass an anonymous function to generate the desired outputs that way.
Really, can be done many ways. Very contextual.