ni.com is currently undergoing scheduled maintenance.

Some services may be unavailable at this time. Please contact us for help or try again later.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Matlab to Labview

Dear All,

 

I have the following MATLAB code. I tried it in mathscript node in Labview 8.6, but the execution was slow.

How can this be converted into Labview.

 

X: array that contains max values

S: orginal signal

 

for i= 1 : (length(X)-1)

   

if (X(i+1)-X(i)<50)

        m(k)=S(X(i));

        k=k+1;

else

        m(k)=S(X(i));

        [mx,ind]= max(m(1:k));

        max_val(n)= mx;

        max_ind(n)=X(ind);

        m=0;

        k=k+1;

        n=n+1;

end

end

 

Thanks

Your help is much appreciated.

0 Kudos
Message 1 of 10
(5,023 Views)

Hi,

 

I can either tell you how to do it, or teach you. What would you prefer?

 

Regards,

Prashanth

Prashanth N
National Instruments
0 Kudos
Message 2 of 10
(5,007 Views)

Hi Pachi,

 

Of course I prefer to teach me. Alot of my coding depends on getting to know how to do it.

 

Regards,

0 Kudos
Message 3 of 10
(5,003 Views)

Why don't you attach your matlab code and include some typical data.

 

Why is "m(k)=S(X(i));" in both cases. Wouldn't it be sufficient to have it once before the decision then?

0 Kudos
Message 4 of 10
(4,999 Views)

Dear altenbach,

 

The two statements should be there to include the last point of X since the for loop i=1:length(X) - 1

Look at the attached sample data and m file.

 

Regards,

0 Kudos
Message 5 of 10
(4,988 Views)

AR_1000,

Are you comfortable using lor loops and case structures in LabVIEW?

 

If not, here are some useful links:

https://forums.ni.com/t5/NI-LabVIEW-Student-Ambassadors/While-and-For-Loops/td-p/3517201

https://www.ni.com/docs/en-US/bundle/labview-api-ref/page/structures/case-structure.html

https://forums.ni.com/t5/NI-LabVIEW-Student-Ambassadors/LabVIEW-Proficiency-Workshop-3-Loops-and-Cas...

 

Your code needs a very simple understanding of how loops and case structures work, and the basics of input/output (controls/indicators)

 

Regards,

Prashanth N
National Instruments
0 Kudos
Message 6 of 10
(4,966 Views)

Tanks Pachi for the links.

 

I think I know how to use loops and cases .

The main problem how to handle arrays and their elements inside looping structures as described in the matlab code above.

 

0 Kudos
Message 7 of 10
(4,955 Views)

Yes, for that you will need to know the concept of auto-indexing in loops.

LabVIEW allows you to automatically access each element of an array in an iteration by auto-indexing.

For your application, use a array control or constant autoindexed to a for loop (note that you no longer have to tell the loop what the value of N is)

Prashanth N
National Instruments
0 Kudos
Message 8 of 10
(4,952 Views)

Please at the VI attached. It is a simple try to implement what I understand so far. But I could not complete it.

Any suggestions?

0 Kudos
Message 9 of 10
(4,947 Views)

Hi,

This is exactly why I asked you to study auto-indexing and shift registers, These concepts inside loops greatly ease your programming in LabVIEW. 

Here's a quick introduction to them:

When you wire a signal into and out of a loop, you can either have a tunnel, auto-indexed tunnel, or a shift register.

A tunnel takes a copy of the value and uses this for each loop iteration.

An unto-indexed tunnel indexes out elements of an array for each iteration

A shift register is a feedback loop and sends the output of an interation to the next.

 

For your application, your S needs to be a tunnel and X needs to be auto-indexed. Right click the tunnel (orange box on the loop) on the X signal and click "Enable Indexing". Notice that you no longer need to wire the N-input of the for loop. It understands that the loop should run for each element of X.

 

Regards

Prashanth N
National Instruments
0 Kudos
Message 10 of 10
(4,934 Views)