MATLAB function handles - matlab

Im working on a MatLab homework and don't understand quite what the task is. The homework states:
Create a MATLAB funciona in an .m file which receives a handle for a two-dimensional function and which creates a surface plot with a range of values input while the .m file is running.
I can't make sense of what I'm supposed to be doing. Could anyone provide such an example?

Your homework is to define a MATLAB function. As it is a homework, you should know how to do that.
The input variable for your function should be a handle for a two-dimensional function. Again, you should know what this means, or you should be able to figure it out (hence homework). Hint: help function_handle.
The function should read the plot ranges during runtime, i.e. interactively from the user (writing something into the command line). You should know how to do this.
And finally, your function has to plot the 2d surface (provided as input parameter) over the range (provided as interactive input). You should know how to do this.

Related

Using output of Matlab protected file as input in Matlab

I am aware that .p files are Matlab protected files. So, I am not trying to access them. However, I was wondering if I could use their output onto the Matlab shell as input to a Matlab program.
What I mean is the following: I have to simulate a dynamic system in Matlab using a controller. Afterwards, I need to assess its performance. This is done by the .p file. Now, the controller behaviour is defined by six distinct variables. I pretty much know their range. So, what I did was create an optimization to find the optimal coefficients. However, when I run the .p file I see that the coefficients I obtained as optimal are in fact not optimal, i.e. my cost function is biased in some way.
So, what I would like to do is to use the output of the .p file (there are always six strings with only two numerical values - so they would be easy to extract if it were a text file) to run a new optimization so that I can understand what I did wrong in my original cost function.
The alternative is finding the parameters starting from my values by trial and error, but considering there are six variables I would prefer a more mathematically pure approach.
Basically, the question is how I can read the output onto the command prompt of a Matlab .p function, and use it as input in a Matlab function.
Thanks for the help!

Matlab Function inside simulink

I need to use a Matlab function inside of a Simulink model. I know how to use a Matlab function to do simple stuff. But what I need now is a little bit more complicated. Let me give you a basic example.
Assume that I need to have a block to generate a sine wave to be viewed directly on the scope (I know that there is already a sine-wave block, I'm just taking that as an example). If I'm writing in Matlab NOT in Simulink, I would do something like:
t = [0:1/30000:0.2];
A = 1;
f =10000;
y = A*sin(2*pi*f*t);
plot(t(1:100),y(1:100))
How can I build the same function inside a Simulink matlab-function block and see the results directly on the Scope?
Remember: The Matlab Function Block has two ports, u and y. Which represents input and output respectively. In the above-given example, a sine-wave generator doesn't need an input.
Perhaps it's just that you haven't chosen a very good example, but there are several things to be aware of when translating the code you've given into Simulink.
The easiest way to get the simulation time into a MATLAB Function block is by feeding a Clock block into an input port (which as #Daniel indicates, are optional, but in this case I suggest would be used for t). So I think you do want an inport in this example.
Your use of plot in your example only plots the first 100 points, where as a Simulink Scope rolls through the data being displayed. There's no concept of only displaying the first X points in a Scope when the simulation runs for longer than that.
You need to remember that Simulink generates data one simulation time step at a time, so you can't generate them all (as per your MATLAB code) and then plot them all. (Well... you can if you want to use frame based signals, but I assume that's not what you're asking here.)
So, to implement the equivalent of what you have would involve doing the following.
write a MATLAB Function block containing the following code (although you might want to make A and f input parameters rather than hard coding them)
function y = myCustomSineWave(t)
A = 1;
f =10000;
y = A*sin(2*pi*f*t);
Feed a Clock block into the above block, and have a Scope block on its output

Structure as output of MATLAB Function Block through Simulink.Bus object

I have a MATLAB Function Block where I compute X variables that I want to use later in a script.
My first option was to use To Workspace blocks for each of them, but since I have X variables, it fills up the model with blocks. The problem is that these X variables have different dimensions, otherwise I could just build a matrix.
My idea was to build a structure within the MATLAB Function Block and then use a single To Workspace block, but my research told me I would need a Simulink.Bus object to do so.
Is this approach at all the best way to achieve my goal? Thanks in advance for your input!

Structure as input for a Matlab function Block

I have a, in my opinion, little problem using a Matlab-Function Block in a Simulink Model.
I want one of the inputs to be a structure.
But that doesn't work so easily, I've come across
suggestions on other semi-related problems, which tell about defining Simulink.Bus and so on,
but to be honest I don't understand how this can be applied on my problem.
It seems simple:
Structure as input for a Matlab function Block
But I don't know how.
Sorry for the inconvenience.
Regards BZAD
Within Simulink you create a Bus signal using the Bus Creator Block. This can then be fed into a MATLAB Function block, but only if an accompanying Simulink.Bus object is created in the Base Workspace.
See Types of Structures in MATLAB Function Blocks and Create Structures in MATLAB Function Blocks and the links off those pages for more information.

How to deal with video frames in simulink

I am working on videos in simulink.As we know that multimedia file block reads one frame at a time so when I will attach it with matlab function block it should read one frame at a time with imread command. If I double click the matlab function block (as shown at http://tinypic.com/view.php?pic=dggujd&s=6) then you will see that I have to give the name of the function as written in matlab mfile along with the input; which in this case is video(named as convid.avi). I am reading one frame at a time but I have given the whole video as an argument to the matlab function.This is the problem how can I resolve it.What should be given as an argument in matlab function block rather than the whole video.I have also uploaded my model at http://tinypic.com/view.php?pic=55jggw&s=6. The code which I am using for vidfunc is:
function h=vidfunc(u)
a=imread(u); % read frame
BW = edge(a,'sobel'); %sobel edge detection
[H,thetaa,rhoo]=hough(BW); % Hough Transform
P = houghpeaks(H,6,'threshold',ceil(0.5*max(H(:))));
lines=houghlines(BW,thetaa,rhoo,P,'FillGap',15,'Minlength',15)
figure,imshow(I),hold on
for k = 1:length(lines) % Draw lines
xy = [lines(k).point1; lines(k).point2];
z(k)=lines(k).point2(2);
plot(xy(:,1),xy(:,2),'LineWidth',1,'Color','green');
end
h=z(k)
It looks like the meat of the work done in this "Simulink" model is actually being performed by your MATLAB function. So the answer to this question is going to largely rely on what that function is actually doing. Specifically, what is the expected input to vidfunc, and what is this function's output? I suspect that this function may need to be revised to fit into your model.
To debug your model, it's useful to think about what the signal output is from each block. At each time step, your From Multimedia File block will output a single image frame, which according to the doc looks to be structured as an
M-by-N-by-P color video signal where P is the number of color planes.
Moving downstream, we next come to the Color Space Conversion block, which in this case looks like it will most likely output an image frame in the form of an M-by-N matrix (where each element of the matrix corresponds to the image's intensity at that pixel).
Now we come to the interesting part -- the MATLAB Fcn block. As we just saw, the input to this block will be an M-by-N matrix representing a single image frame. When you look in the parameter dialog box for the MATLAB Fcn block, the input to this block is represented by the variable u. Therefore, to execute the vidfunc function on the image frame being input to this block, you would simply enter vidfunc(u) for your MATLAB function.
Now, based on the input going to the MATLAB Fcn block, and the fact that you have a Video Viewer block connected to the output, vidfunc should be structured such that it operates on a single image frame as its input and outputs another single image frame. If vidfunc is not structured in this way, you will need to edit it (or just re-implement the same functionality using Simulink blocks).
That said, let's assume that vidfunc is also returning an M-by-N matrix representing a processed image frame. You will want to set the Output Dimensions parameter for your MATLAB Fcn block to -1 to indicate that the output will have the same dimensions as the input. Also, (as indicated in the doc) you will want to make sure Collapse 2-D results to 1-D is unchecked or else your image output will be in the form of one long vector rather than an M-by-N matrix.
Provided that vidfunc is structured correctly, this should solve your problem.
NOTE: To make your life a lot easier, I would strongly suggest displaying the signal data type and dimensions in your Simulink model. This can help to avoid a lot of confusion. This doc describes exactly how to do this.
--UPDATE--
After looking at your code, this confirms my suspicion that the inputs/outputs of vidfunc are inconsistent with what your Simulink model expects. The way that you proceed is highly dependent on what your own design constraints are and what you actually want out of this system. Basically, your Simulink model and MATLAB function disagree... which one is right? I'll give some general thoughts based on my best guesses at what you're aiming for.
First, Simulink is passing an image (in the form of an M-by-N matrix) into vidfunc. This means that vidfunc no longer needs to load an image at the start of the code. So I believe that you could update the first few lines of code to be as such:
function h=vidfunc(a)
BW = edge(a,'sobel'); %sobel edge detection
See that now vidfunc is taking the actual image (not the filename that contains the image) as its input. Basically, you are removing the a=imread(u); line and jumping right in to processing a.
The other issue is the output of vidfunc. Simulink is expecting the output to be an image, but it is not. I'm not 100% sure what h is supposed to be in this code (when I first glanced at your code I thought these were handles to line objects but that doesn't seem to be the case). It looks to possibly be the y coordinate of the endpoint of one of the houghlines. Nevertheless, it's not what your Simulink model is expecting. This one is not as straight-forward to fix. Possibly you could try to use getframe to grab an image from the plot of your lines.
I actually feel that the best advice that I can give to you is to just scrap the MATLAB function and implement everything in Simulink. I think this will be a lot easier than trying to get vidfunc to play nicely with your model. vidfunc does not actually contain that much code, so this should not be too difficult of a task for you. Another benefit is that at the end of this process you'll have a nice Simulink model that explicitly shows all of the image processing steps that you are taking.
I believe that all of the image processing that you are doing with MATLAB functions can also be done with Simulink blocks (see the Simulink Blocks section of this doc).
Good luck.