0

I came across a situation where I have to use Simulink.

I have a csv file ( 1000 rows x 6 columns ) that contain data. I need to search column 6 of my csv file and find the closest match to whatever I'm looking for, and then return columns 1 2 3 4.

For simplicity, lets go with a smaller version of my problem.

Csv file example

0  0  0  0 0 20
0 10 15  2 4 30
1 50  2 54 2 40

I search for 21. I find that row 1, column 6, is closest to 21. I then return 0 0 0 0 (the first four columns.

I search for 34, and find that row 3, column 6 is the closest to 34. I then return 1 50 2 54.

Hopefully that explains what I need.

Originally I wrote a working script in matlab using find(..) and it worked, but this has to be done using simulink.

I noticed that there is the 2d loopup table block which can work, however, it appears I need to know my indexes before hand.

If anyone can point me in the right direction of what blocks I can use, and i can google around for how to use them, it would be much appreciated.

1
  • 1
    What is the reason that you "have" to use Simulink? If you already have a working Matlab function then the easiest approach is to make use of either the Matlab Function block or Interpreted Matlab Function block to incorporate this existing function into your Simulink model. If for some reason you must have an "all Simulink" solution then you may want to look into the Selector block.
    – grungetta
    Oct 14, 2012 at 5:07

2 Answers 2

2

As I said in my comment, your best bet is to simply make use of the Matlab Function or Interpreted Matlab Function blocks in order to incorporate your already existing function into your Simulink model (no need to reinvent the wheel if you don't have to).

If you're working under the constraint that this must be "all Simulink" (i.e. no user-defined blocks), then a few blocks that will probably be helpful are Selector, Find, Min, and Compare To Zero.

One approach that I might take is to use a selector to isolate that 6th column. Find the "error" between each value in the column and the value that you are trying match (i.e. take the absolute value of the difference). Use the Min block to find the minimum error. Subtract that min error from your vector of errors so that the index of the closest match will now have a value of 0. Using Compare To Zero and Find you should then be able to determine the index of the closest match. Once you have that, you can use it to drive another Selector block in order to select elements as desired.

There are other blocks that I can think of that might be really helpful (Find Local Maxima is something that comes to mind that I bet you could incorporate into a solution); however, they require additional toolboxes.

1
  • I didnt see that this question had a reply. I ended up writing a custom matlab function and converted it to a simulink block where i received my inputs from the rest of the design. What you proposed was essentially the same thing. I appreciate your answer!
    – efox29
    Nov 9, 2012 at 3:08
0

You can achieve this using a Minimum block. Set it's mode to index so that it outputs the index of the item having minimum error.

Pass this index into a Variable Selector block, taking the original rows/columns via In1 and the index of the minimum on Idx.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.