Oppure

Loading
31/03/11 20:21
Pierga
Ciao a tutti,
sto scrivendo un programma che mi faccia dei calcoli su due colonne di dati "Potenziale" (E) e "Corrente" (i). Lo strumento mi fornisce un file txt con dei dati di intestazione "Estart", "Eswitch", "Eend" etc. e successivamente parte con le due colonne di dati "E" ed "i", separati da virgola. Ne ho allegato uno.
Il codice funziona, ho solo due problemi che non riesco a sormontare:

1) Quando si apre la finestra di apertura file, se si preme il tasto Annulla mi appare l'errore "Run-time error '75'".

2) Alla fine il frutto dei calcoli appare nella textbox "Result". Pero' se il file txt di partenza e' troppo lungo non mi appaiono tutti i dati. Mi vengono tagliati. Inoltre, se il file fosse corto ma volessi avere tre colonne di dati, quelle iniziali "E" ed "i" e la colonna che deriva dal calcolo "curconv", lo stesso il programma mi taglia i dati. Sembra che la texbox possa contenere solo un certo un numero di caratteri, il che mi sembra strano.

La form e' composta da una Textbox "Result" ed un bottone.

Private Sub Command2_Click()

Result.Text = "" 'Cleaning textbox
CommonDialog1.Filter = "Text files|*.txt"
CommonDialog1.ShowOpen

Open CommonDialog1.FileName For Input As #1 'Apre il file in lettura

'Variables declaration
Dim Take As String
Dim scanrate As Double
Dim Estart As Double
Dim Eend As Double
Dim Eswitch As Double
Dim potential() As Double
Dim current() As Double
Dim ndata As Integer
ndata = 0
Dim counter1 As Integer
counter1 = 0
Dim time() As Double
Dim deltat As Double


'Reading cycle
Do Until EOF(1)
Line Input #1, Take 'N.B.: Takes the whole line in input, otherwise takes just the first
                    'characters before the first space!


If InStr(Take, "v (V/s):") > 0 Then
b = Split(Take, ": ") 'The line is divided in an array b(i) at each comma.
scanrate = b(1)
End If

If InStr(Take, "Estart (V):") > 0 
b = Split(Take, ": ") 'The line is divided in an array b(i) at each comma.
Estart = b(1)
End If

If InStr(Take, "Eswitch (V):") > 0 
b = Split(Take, ": ") 'The line is divided in an array b(i) at each comma.
Eswitch = b(1)
End If

If InStr(Take, "Eend (V):") > 0 
b = Split(Take, ": ") 'The line is divided in an array b(i) at each comma.
Eend = b(1)
End If

If InStr(Take, "number of E(V), I(A) couples:") > 0 Then 'For Echem Files
b = Split(Take, ": ") 'The line is divided in an array b(i) at each comma.
ndata = b(1)
ReDim potential(ndata) As Double
ReDim current(ndata) As Double
ReDim time(ndata) As Double
End If

If InStr(Take, ",") > 0 And InStr(Take, "e-") > 0 Then
b = Split(Take, ",") 'The line is divided in an array b(i) at each comma.
potential(counter1) = b(0)
current(counter1) = b(1)
counter1 = counter1 + 1
End If

Loop
Close #1 'Closes the file *.txt


'Tensformation of E scale to time scale
counter1 = 0
deltat = 0.001 / scanrate

For counter1 = 0 To ndata - 1
time(counter1) = counter1 * deltat
Next

'Calculation of convolute convolute current
Dim curconv() As Double
ReDim curconv(ndata) As Double
counter1 = 0 'equal to K
Dim counter2 As Double 'equal to J
counter2 = 1
Dim calc1 As Double
Dim calc2 As Double
calc1 = 0
calc2 = 0

For counter1 = 0 To ndata - 1
calc2 = 0
     For counter2 = 1 To counter1
     calc1 = ((deltat ^ 0.5) * current(counter2)) / ((counter1 - counter2 + 0.5) ^ 0.5)
     calc2 = calc2 + calc1
     Next counter2
calc2 = (3.14 ^ -0.5) * calc2
curconv(counter1) = calc2
Next counter1

'Write on text box the results
counter1 = 0
Result.Text = Result & "Estart (V): " & Estart & vbCrLf
Result.Text = Result & "Eswitch (V): " & Eswitch & vbCrLf
Result.Text = Result & "Eend (V): " & Eend & vbCrLf
Result.Text = Result & "v (V/s): " & scanrate & vbCrLf
Result.Text = Result & "Diffusion Coefficient (cm/s): " & Text1 & vbCrLf & vbCrLf
For counter1 = 0 To ndata - 1
Result.Text = Result & potential(counter1) & " , " & current(counter1) & " , " & curconv(counter1) & vbCrLf
Next counter1

End If
End Sub




Se qualcosa non e' chiaro, vi prego, fatemi sapere. Cercherò di essere più' esaustivo.
Vi avverto che sono un neofita e che conosco solo le basi della programmazione e tutto quello che ho scritto e frutto di innumerevoli ricerche in rete. Se potete, cercate di darmi delle indicazioni più' semplici possibile. Grazie mille a chiunque risponda.
aaa
31/03/11 20:41
lorenzo
Open CommonDialog1.FileName For Input As #1 'Apre il file in lettura


l'errore di runtime l'hai perché anche schiacciando il tasto di annulla, tu tenti di aprire il file prendendo il path scelto nella dialog. Se premi annulla quel path non è valido quindi tenti di aprire un file che non esiste



per il resto, adesso non ho molto tempo, però è ovvio che la textbox abbia un limite massimo di lunghezza, non crederai mica che sia infinita no?

aaa
01/04/11 7:04
poeo85
allora un mio consiglio...
per quanto concerne il commondialog ti consiglio di fare

ti metti il fileName dentro ad una variabile poi testi il risultato e se tutto va bene vai avanti.

per il resto visto che devi mettere su 3 colonne i tuoi dati, te li metti in una griglia???
aaa