# LabVIEW

cancel
Showing results for
Did you mean:

Highlighted

## Handling array in mathscript?

i==0
p=Sx
q=Sy

while (Sx~=p && Sy~=q)

for n=0:n
if Cx(n)==Sx+1
if Cy(n)==Sy
Sx=Sx+1
end
if Cy(n)==Sy-1
Sx=Sx+1
Sy=Sy-1
end
if Cy(n)==Sy+1
Sx=Sx+1
Sy=Sy+1
end
end
end

i=1+i
Rx[i]=Sx

Ry[i]=sy

end

-----------------------------------------------

I am trying to write this simple code in Mathscript, But Last 3 lines before last END is showing error. I have taken n, Cx, Cy, Sx, Sy, Rx, Ry as input and need only Rx, Ry as output. Cx, Cyy, Rx, Ry are 1D array, Sx, Sy, n are integer numbers.

This is the very 1st time I'm using mathscript and not able to find usefull support in internet to solve this problem. Probably I am not using the proper key words for the search. Can anyone plese help me to solve the isuue or to find suitable material to study to solve such isuues?

Message 1 of 4
(1,428 Views)
Highlighted

## Re: Handling array in mathscript?

Why are you using LabVIEW?  Why are you using Matlab?  You do realize, I hope, that these are sort of different, have different "models" of How the World Works, and should (probably) only rarely be mixed.

Sometimes you need a little function that can be written in procedural code simpler than finding the right Block Diagram functions and wiring them together.  However, if you are going to code an algorithm (as you are doing), it will probably be (a) easier to do, (b) easier to debug, and (c) more likely to "do what you want" (as opposed to "do what you told it to do").

Since you are here on the LabVIEW Forums asking your question, I'll simply recommend that you write the For Loop inside the While Loop and figure out how to bring your LabVIEW Variables (which you need in any case), Cx, Cy, Rx, Ry, Sx, Sy, and n into and out of these structures, and how to wire them together.  Does "n" have anything to do with the size of the C, R, and S arrays?  Do you understand what i, p, and q represent?  Are some of your arrays 0-indexed (i.e. CX .. CX[n]) while others are 1-indexed (i.e. Rx .. ?)?

Put some thought into this problem.  Think about what you want to do, then do it.

Bob Schor

Message 2 of 4
(1,401 Views)
Highlighted

## Re: Handling array in mathscript?

@Bob_Schor wrote:

Why are you using LabVIEW?  Why are you using Matlab?  You do realize, I hope, that these are sort of different, have different "models" of How the World Works, and should (probably) only rarely be mixed.

Sometimes you need a little function that can be written in procedural code simpler than finding the right Block Diagram functions and wiring them together.  However, if you are going to code an algorithm (as you are doing), it will probably be (a) easier to do, (b) easier to debug, and (c) more likely to "do what you want" (as opposed to "do what you told it to do").

Since you are here on the LabVIEW Forums asking your question, I'll simply recommend that you write the For Loop inside the While Loop and figure out how to bring your LabVIEW Variables (which you need in any case), Cx, Cy, Rx, Ry, Sx, Sy, and n into and out of these structures, and how to wire them together.  Does "n" have anything to do with the size of the C, R, and S arrays?  Do you understand what i, p, and q represent?  Are some of your arrays 0-indexed (i.e. CX .. CX[n]) while others are 1-indexed (i.e. Rx .. ?)?

Put some thought into this problem.  Think about what you want to do, then do it.

Bob Schor

Thanks Bob.
coming to your confusion I would like to put some light in this way --
1. I can write program in from BASIC, FORTRAN, C, JAVA, MATLAB and LabVIEW. so which kind of coding is useful where is clear to me.

2. Exploring a feature in depth always good for better understanding; so neglecting mathscrip or formula node kind of things can never be wise.

3. To know mathscript properly I must have to understand it through complex codes (as I have decided to de, and its just the starting).

4. dimenion of Cx=dimension of Cy=n

5. n controls the number of run of for loop.

6. As per the written code the loop should never run (while loop has such condition), and it is intentional. So please don't worry about that.

Note: there is a typing mistake at the starting of the code pested here.
i==1  - is wrong, it should be

i=1

But till now the facts are not clear for which Last 3 lines before last END is showing error. Red cross mark is coming infront of each line when I tried to write these.

I shall be more thankful to you, if you please put some light on it.

Message 3 of 4
(1,389 Views)
Highlighted

## Re: Handling array in mathscript?

for n=0:n
if Cx(n)==Sx+1

According to the rules of Matlab, this code will have "n" take on the "n+1" values 0, 1, ... n.  In Matlab, arrays use 1-based indexing, so the first element of an array is 1.  Thus the code should immediately fail as it tries to access Cx(0).

I'll go back to my question -- "What do you want to do?"  Note that I'm not asking "How do you want to do it?", as that will differ depending on whether you are doing LabVIEW or Matlab.

Here's an example -- "I want to sum the elements of an array".  That's What you want to do.  You could also say (or show) , which would be How to do this in LabVIEW (I'm not going to bother writing down "how" to do this in Matlab -- it almost certainly wouldn't be as elegant).

Now, once you have the "What" down, we could discuss whether or not LabVIEW could implement it easily, or whether it would be "worth the trouble" (which you seem to be having) of doing it in a Mathscript node, but I would hope, if you did want to use Matlab to accomplish your goal, you'd be able to write correct Matlab code that would implement a well-defined algorithm.

I can't tell what you want to do, I can only tell that the Matlab code you've provided is "wrong".  I can also venture my opinion that your time and effort would be better spent doing everything in LabVIEW and eliminating the troublesome Mathscript node.  You are, of course, free to disagree with this opinion, but it won't get your code written any faster ...

Bob Schor

Message 4 of 4
(1,372 Views)