LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to make Matlab and Labview work together?

Hallo,
I made a Matlab script, that script takes an 6X6 array as input (Q), and should give an 1X12 array as output (T). I have tried that matlab script in Matlab Program and also in Octave, it works exactly like i want and gives me the result. But when i combined it with Labview (with Matlab script ,found it under pallete Mathematic) ,Labview won't give me the result. Can somebody help me? I've attached my Program . Thank you very much.
Download All
0 Kudos
Message 1 of 7
(3,968 Views)
Brandon
Your matlab code works just fine here after I delete the line containing "Q=[ ];". I have attached the modified VI containing your data in the front panel control (To retain these values permanently, you must set them as defaults as explained on my comment on the front panel). Please run it and tell me if you still don't get any output.

For your reference, I also created a second indicator, showing the results I get here when I run it. What matlab version do you have?

See if this helps. Personally, I don't use matlab. It would be quite easy to code all this directly in LabVIEW.
Message 2 of 7
(3,968 Views)
Brandon,

You have to be very careful using matlab code. For example, in the above case you can sucessfully run it once with the "Q=[ ];" removed, then put the "Q=[ ];" line back in place and now it appears to continue to work. However, if you would change the input data, you will notice that you get stale data (i.e. the old result of the run where the line was still missing)!

As a quick exercise, I have attached a VI containing also two alternate codings of the problem written in LabVIEW, thus avoiding the matlab node but give the same result. They are more efficient (see below).

One of the problems with your matlab code is the fact that it does not handle exceptions and there could be input combinations that are not defined. (What if A
, B, and C are all .00085??)

The middle example is a near literal translation of your matlab code. It runs quite fast.

The top example is a free interpretation. It seems the only thing you do is sort the array columns by the values in column 1-3 (taken together as 3bit binary), then create an output array containing only the last two columns reshaped as 1D array.

I have run a few benchmarks comparing the versions, here is the result (1GHz PIII):
Matlab version: 11 milliseconds
Literal version: 9 microseconds
Free version: 29 microseconds
As you can see, native LabVIEW code is ~300-1000 times faster!

(The free version uses two loops, bundling/unbundling, and sorting and is thus expected to be a bit slower. However, the code is a bit easier to read. Take your pick.)

I have wired these examples up very quickly, so I am sure significant further improvements are possible. Something to think about. 🙂
0 Kudos
Message 3 of 7
(3,968 Views)
Dear Mr Altenbach,

Thank you for your input. Actually the 1st program that i made, it was without that "Q=[ ];" and it did not work , so i searched in google and found some similiar case, but he initialized the input array in matlab script , so i followed him.... and also didn't work. And after i read your comment, i tried my 1st program (without "Q=[ ];") , and amazingly .... it works..I really don't know what was happen and where was my mistake. I think, i have to change a new PC :-)But Thanks for your input.

I am still trying to understand your labview version,it is a bit confusing for me cos i am new to labview.

1. I want to know, why u don't tell how many loop u want for that for loop.Normally one should initialize that N in for loop
with some number.

2.In "Free interpretation of matlab code" what does the number 1,2,4 mean? And why u bundle array of element 4 (with length2) with sum of 1,2,4 ( sum of 1,2,4 should be 7 , if i don't false interpret) ?

Thank you very much for your time.

Brandon
0 Kudos
Message 4 of 7
(3,968 Views)
Some quick answers:

(1) LabVIEW does "autoindexing" on loop boundaries if desired. The 2D array comes in and the first iteration gets the first row, the second iteration the second row, etc. until all input has been processed. The number of rows in the input array automatically determines how many times the loop must run. Isn't that nice!

(2) These are just some programming tricks. the 1,2,4 together with the comparison operation give a unique number for each possible combination of columns 1-3, basically a binary polynomial.

For example, if the numbers are [.999985, 0, .999985] and I do a [not equal zero?], I get a boolean array of [true, false, true], the "boolean to 0,1" converts this to [1, 0, 1]. Multiplied by the array
[1,2,4] gives [1, 0, 4] and the sum is 5. All other possible combinations also give a unique number:
[0,0,0]->0, [1,0,0]->1, [0,1,0]->2, [1,1,0]->3, ... [1,1,1]->7. Do you see the pattern? If you look at your matlab rules, we apparently need to sort the rows by this number determined from column 1-3, pick the last two numbers of each row, and line them all up for the final result.

The "sort array" tool has a feature that arrays of clusters are sorted primarily by the first cluster element. Thus, for each row I form a cluster of the magic number determined from columns 1-3 and an array of the last two elements in the row. This magically tuns into an array at the loop boundary. Each element of the array contains the magic number and the last two elements of the row. This array is now sorted according to the number. The next loop takes the now sorted array of clusters, and strips out the numbers we need for the result, giving us a 6x2 array that we need to reshape into a 12x1 array f
or the final result.

Let me know if this is clear enough. You can also create some temporary indicators (right-click on the wire... create indicator), e.g. for the wires right before and after sorting to see the intermediary data.
0 Kudos
Message 5 of 7
(3,968 Views)
altenbach,
Could you save your vi to 7.0 so I could view it? I am looking at writing some code in Octave and calling it from LabVIEW and am interested in looking at your example.
Thanks
0 Kudos
Message 6 of 7
(3,969 Views)
I saved it in LabVIEW 7.0, but I think there is a problem with it. If I try to open it in LabVIEW 7.0, it locks up. Maybe it is just a problem here. You can try it, see attached.
0 Kudos
Message 7 of 7
(3,969 Views)