+ Reply to Thread
Results 1 to 2 of 2

Help populating cells from vba script.

Hybrid View

  1. #1
    Registered User
    Join Date
    11-23-2010
    Location
    London
    MS-Off Ver
    Excel 2007
    Posts
    1

    Help populating cells from vba script.

    Hi,

    I was trying to "convert" this matlab *.m file that I have to excel vba but just don't have enough knowledge to do so in regard.

    What i'm trying to do here is to plot a value of x against time.
    %MATLAB m script 
    
    a = 5;
    b = 3;
    c = 7;
    t = 0.1;
    x(1) = 0.3;
    x(2) = 0.2;
    
    for i=2:79
        x(i+1) = (a*x(i-1)-(b/(t*t))*x(i-1)+x(i)+ t*x(i)) / (b/(t*t) + c/t);
    end 
    plot(x);

    vba attempt:
    Sub calc()
        Dim i As Integer
        Dim a, b, c, x(1 To 79) As Double
        
        a = 5
        b = 3
        c = 7
        t = 0.1
        x(1) = 0.3
        x(2) = 0.2
        
        For i = 2 To 78
            x(i + 1) = (a * x(i - 1) - (b / (t * t)) * x(i - 1) + x(i) + t * x(i)) / (b / (t * t) + c / t)
        Next i
    Cells(1, 1).Value = x()    <------  what should i put here so that all the values of x in the range from 1 to 78 would populate cells a1 to a78?
                         because all that is doing is outputting only one value in that particular cell.
     
    End Sub
    And then when the cells are populated by the values of x I would plot them against, say b1:b78, which would be time t+t, to get my graph.

    ANY help would be appreciated.

    Thanks.

  2. #2
    Forum Contributor mewingkitty's Avatar
    Join Date
    09-29-2008
    Location
    Fort McMurray, Alberta, Canada
    MS-Off Ver
    Excel 2003
    Posts
    949

    Re: Help populating cells from vba script.

    For i = 2 To 78
    x(i + 1) = (a * x(i - 1) - (b / (t * t)) * x(i - 1) + x(i) + t * x(i)) / (b / (t * t) + c / t)
    Next i
    Cells(1, 1).Value = x()
    What this is doing, is calculating this value for i 2 to 78, then placing the value in cell 1 1 after it's all over.
    Try this
    For i = 2 To 78
    x(i + 1) = (a * x(i - 1) - (b / (t * t)) * x(i - 1) + x(i) + t * x(i)) / (b / (t * t) + c / t)
    cells( i, 1).value = x
    Next i
    I't adding in a line which increases the row referenced which has the value entered as i increases from 2 to 78.
    Last edited by mewingkitty; 11-23-2010 at 10:13 PM.
    =IF(AND(OR(BLONDE,BRUNETTE,REDHEAD),OR(MY PLACE,HER PLACE),ME),BOW-CHICKA-BOW-WOW,ANOTHER NIGHT ON THE INTERNET)

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1