From: IJALAB on 21 Apr 2010 09:14 Hi all Thanks a ton for your help. I have stored each line from my sampletext file, which is like 204 -204 101 -209 etc with the following and stored in Str2 array Dim Str2(2048) As Variant Do Until EOF(IQTestFileNumber) Line Input #IQTestFileNumber, TempStr If TempStr = "" Then 'Do nothing Else Str2(Y) = Split(TempStr, " ") 'Count = Count + 1 Y = Y + 1 End If Loop Now I wanted to plot the str2(0)(0) as Xvalue and str2(0)(1) as Yvalue and plot a scatter graph like this. How do I do the same? - * - * ---------------------------------------- - * * - I tried with the following sample code. It is not working. ChartPoints(lRow, 0) = Str2(0)(0) is subscript out of range? For lRow = 1 To UBound(ChartPoints, 1) ChartPoints(lRow, 0) = Str2(0)(0) ChartPoints(lRow, 1) = Str2(0)(1) Next lRow With MSChart2 {init} .ColumnLabelCount = 1 * 2 If UBound(ChartPoints, 1) > OldRowCount& Then .RowCount = UBound(ChartPoints, 1) End If For lRow = 1 To UBound(ChartPoints, 1) .DataGrid.SetData lRow, 1 * 2 - 1, ChartPoints(lRow, 1), False .DataGrid.SetData lRow, 1 * 2, ChartPoints(lRow, 2), False Next For lRow2 = lRow To OldRowCount& .DataGrid.SetData lRow2, 1 * 2 - 1, 0, True .DataGrid.SetData lRow2, 1 * 2, 0, True Next For lRow = OldRowCount& + 1 To .RowCount For lRow2 = 1 To 1 - 1 .DataGrid.SetData lRow, lRow2 * 2 - 1, 0, True .DataGrid.SetData lRow, lRow2 * 2, 0, True Next Next
From: Mike Williams on 21 Apr 2010 10:47 "IJALAB" <balaji.draj(a)gmail.com> wrote in message news:6b68425c-b59b-47fe-9065-907acc52193e(a)n5g2000yqh.googlegroups.com... > Hi all. Thanks a ton for your help. > I have stored each line from my sample text file > with the following and stored in Str2 array > Dim Str2(2048) As Variant, etc Loading the string data from the file into an array of Variants, with each element of the Variant array holding a small two element String array, is one way of doing it, but Variants tend to be slower than other variable types and so it is not usually the best way. There are other faster ways. However, since you are dealing with loading data from disk (which is going to be relatively slow anyway) then I suppose it doesn't matter in your specific case, and certainly not for the fairly small amount of data you are dealing with, so it's probably going to be fine for your needs. You do need to remember though that when you declare an array as you have done (specifying the upper bound but not the lower bound) you will end up with an array whose lower bound is equal to the value you have set in any Option Base statement you may have used (0 or 1 as appropriate) and it will default to 0 if you have not used an Option Base statement. So, in your specific case the above Dim statement will almost certainly declare an array with the elements 0 to 2048, which is of course 2049 elements. Generally it is far better to explicitly quote the desired lower bound in the Dim statement, and it will then always be clear what it is, regardless of whether or not you have used an Option Base statement, for example: Dim Str2(0 to 2047) as Variant . . . or aternatively . . . Dim Str2(1 to 2048) as Variant . . . (whichever you prefer) I personally normally use zero as the lower bound. The other thing to bear in mind when loading data from disk is that you cannot always be totally certain how many data items there actually are in the file, so unless you add code to deal with such situations then you will get an error if you attempt to load more than the number of elements you have used to declare your array or if the data file does not contain as many elements as you are attempting to load. In both cases you will be okay of course if you are absolutely certain that your data fie does in fact contains exactlt the correct amount of data, but it is something to bear in mind for the future. > I tried with the following sample code. It is not working. > ChartPoints(lRow, 0) = Str2(0)(0) is subscript out of range? > For lRow = 1 To UBound(ChartPoints, 1) > ChartPoints(lRow, 0) = Str2(0)(0) > ChartPoints(lRow, 1) = Str2(0)(1) You haven't shown us your declaration for the ChartPoints array but presumably it is an array of integers (Longs perhaps) into which you are transferring the data from your Variant array. In that case you need to make sure that the ChartPoints array has been declared in a way that matches the data in the Str2 Variant array. In the Variant array (as your code stands at the moment) you are loading the 2048 pairs of items into elements 0 to 2047 of the Str2 array, and you are not storing anything in element 2048. So, perhaps you should declare the array Str2 as (0 to 2047) as Variant. Then you should declare your other array as ChartPoints(0 To 2047, 0 To 1) as Long. Then you should use For lRow = LBound(ChartPoints, 1) To UBound(ChartPoints, 1) . . etc and your Subscript Out of Range error should go away. Also, although this is perhaps being a little pedantic, since you appear to be transferring string data into an array of Longs you really should use the appropriate conversion function instead of simply relying on VB's "automagic" under the hood conversion, so you would have something like: ChartPoints(lRow, 0) = CLng(Str2(lRow)(0)) ChartPoints(lRow, 1) = CLng(Str2(lRow)(1)) The other very important point that needs mentioning (a problem you have not come to yet but which you will definitely come to after you have fixed the Subscript Out of Range problem) is that your code as it stands is copying only the first pair of items, and it is copying that same pair into /every/ position of the ChartPoints array. The problem code is as follows: For lRow = 1 To UBound(ChartPoints, 1) ChartPoints(lRow, 0) = Str2(0)(0) ChartPoints(lRow, 1) = Str2(0)(1) Next lRow There is a problem with the above code, and the problem will still be there when you have modified it to use LBound and CLng as suggested above, when the code will be: For lRow = LBound(ChartPoints) To UBound(ChartPoints, 1) ChartPoints(lRow, 0) = CLng(Str2(0)(0)) ChartPoints(lRow, 1) = CLng(Str2(0)(1)) Caption = lRow Next lRow The thing is that throughout the entire loop you are referencing the same (0) element of Str2. The code needs to be changed so that it is referencing the appropriate Str2 element within the loop in exactly the same way that is is referencing the ChartPoints element, as follows: For lRow = LBound(ChartPoints) To UBound(ChartPoints, 1) ChartPoints(lRow, 0) = CLng(Str2(lRow)(0)) ChartPoints(lRow, 1) = CLng(Str2(lRow)(1)) Caption = lRow Next lRow There may be other problems as well (I haven't looked at the other parts of the code you posted) but the above things definitely need sorting out first. Mike
From: Helmut Meukel on 21 Apr 2010 12:21 "IJALAB" <balaji.draj(a)gmail.com> schrieb im Newsbeitrag news:6b68425c-b59b-47fe-9065-907acc52193e(a)n5g2000yqh.googlegroups.com... > Hi all > > Thanks a ton for your help. > I have stored each line from my sampletext file, which is like > 204 -204 > 101 -209 etc with the following and stored in Str2 array > Dim Str2(2048) As Variant > Do Until EOF(IQTestFileNumber) > Line Input #IQTestFileNumber, TempStr > > If TempStr = "" Then > 'Do nothing > Else > Str2(Y) = Split(TempStr, " ") > 'Count = Count + 1 > Y = Y + 1 > End If > Loop Did you initialize Y to a start value? Your code snippet doesn't show it. Granted, VB automatically initialize it to 0, but then your code is starting with element 0 of Str2. You told us in another post your file contains 2048 lines, so your last pair of values goes into Str2(2047). > Now I wanted to plot the str2(0)(0) as Xvalue and str2(0)(1) as Yvalue > and plot a scatter graph like this. How do I do the same? > - > * - * > ---------------------------------------- > - * > * - > I tried with the following sample code. It is not working. > ChartPoints(lRow, 0) = Str2(0)(0) is subscript out of range? > > For lRow = 1 To UBound(ChartPoints, 1) > > ChartPoints(lRow, 0) = Str2(0)(0) > ChartPoints(lRow, 1) = Str2(0)(1) > > Next lRow Where did you dimension ChartPoints? Where did you get the following code? > > With MSChart2 > {init} > > .ColumnLabelCount = 1 * 2 > If UBound(ChartPoints, 1) > OldRowCount& Then > .RowCount = UBound(ChartPoints, 1) > End If > > For lRow = 1 To UBound(ChartPoints, 1) > .DataGrid.SetData lRow, 1 * 2 - 1, ChartPoints(lRow, 1), False > .DataGrid.SetData lRow, 1 * 2, ChartPoints(lRow, 2), False > Next > For lRow2 = lRow To OldRowCount& > .DataGrid.SetData lRow2, 1 * 2 - 1, 0, True > .DataGrid.SetData lRow2, 1 * 2, 0, True > Next > > > For lRow = OldRowCount& + 1 To .RowCount > > > For lRow2 = 1 To 1 - 1 > .DataGrid.SetData lRow, lRow2 * 2 - 1, 0, True > .DataGrid.SetData lRow, lRow2 * 2, 0, True > Next > > Next > > Seems to me you copied code from an example without really understanding what it was intended for. Helmut.
|
Pages: 1 Prev: Trouble reading .ini files on Win7 x64 Next: MSflexgrid truncation |