Ein Fehler zeigt an, dass ein Array leer ist, obwohl es nicht so ist

Diese Seite verwendet Cookies. Durch die Nutzung unserer Seite erklären Sie sich damit einverstanden, dass wir Cookies setzen. Weitere Informationen

  • Ein Fehler zeigt an, dass ein Array leer ist, obwohl es nicht so ist

    Dank Michael, ist der Error weg. aber mein Code ( in der Zeile: 51) zeichnet der Data fftw_result ohne aufzuhören . Wie kann ich die nur einmal zeichne?

    Ich bin mit dem App. fast fertig. Hier ist ein stuck meines Codes:

    Quellcode

    1. int SIZE =64;
    2. NSMutableArray *arrayFftwXRe= [NSMutableArray arrayWithCapacity:SIZE]; ...
    3. NSTimeInterval delta = 0.005;
    4. NSTimeInterval updateInterval = deviceMotionMin + (delta * sliderValue);
    5. NSMutableArray *sensorData= [NSMutableArray arrayWithCapacity:SIZE]; ...
    6. if ([mManager isDeviceMotionAvailable] == YES) {
    7. [mManager setDeviceMotionUpdateInterval:updateInterval];
    8. [mManager startDeviceMotionUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMDeviceMotion *deviceMotion, NSError *error) {
    9. // saving data
    10. NSString *strSensorData = [NSString stringWithFormat:@"%.6f %.6f %.6f %.6f ", deviceMotion.timestamp, deviceMotion.userAcceleration.x, deviceMotion.userAcceleration.y, deviceMotion.userAcceleration.z];
    11. NSString *strSensorDataX = [NSString stringWithFormat:@"%.6f %.6f ", deviceMotion.timestamp, deviceMotion.userAcceleration.x];
    12. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    13. NSString *docDir = [paths objectAtIndex: 0];
    14. NSString *docFile = [docDir stringByAppendingPathComponent: @"sensordata.txt"];
    15. NSString *docFileX = [docDir stringByAppendingPathComponent: @"dataFFTWX.txt"];
    16. if (![fileManager fileExistsAtPath:docDir]){
    17. [fileManager createFileAtPath:docFile
    18. contents:nil
    19. attributes:nil];
    20. [fileManager createFileAtPath:docFileX
    21. contents:nil
    22. attributes:nil];
    23. }
    24. [ sensorData.description writeToFile:docFile atomically:YES encoding:NSUTF8StringEncoding error:nil];
    25. /////////////////////////////////////// FFTW
    26. fftw_complex *data, *fft_result;
    27. fftw_plan plan_forward;
    28. int i;
    29. data = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * SIZE);
    30. fft_result = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * SIZE);
    31. plan_forward = fftw_plan_dft_1d(SIZE, data, fft_result,
    32. FFTW_FORWARD, FFTW_ESTIMATE);
    33. /////////////////////////////////////// Data-X-Y-Z /////////////////
    34. switch (weakSelf.segmentedControl.selectedSegmentIndex) {
    35. case kDeviceMotionGraphTypeUserAcceleration:
    36. [weakSelf setLabelValueX:deviceMotion.userAcceleration.x y:deviceMotion.userAcceleration.y z:deviceMotion.userAcceleration.z];
    37. // userAcceleration
    38. [[weakSelf.graphViews objectAtIndex:kDeviceMotionGraphTypeUserAcceleration] addX:deviceMotion.userAcceleration.x y:deviceMotion.userAcceleration.y z:deviceMotion.userAcceleration.z];
    39. break;
    40. /////////////////////////////////////// Data-X ////////////////////
    41. case kDeviceMotionGraphTypeFftwX:
    42. for( i = 0 ; i < SIZE ; i++ ) {
    43. data[i][0] = [sensorDataX[i] floatValue];
    44. data[i][1] = 0.0;
    45. }
    46. fftw_execute( plan_forward );
    47. for( i = 0 ; i < SIZE ; i++ ) {
    48. [arrayFftwXRe addObject:@(fft_result[i][0])];
    49. }
    50. [weakSelf setLabelValueX:fft_result[i][0] y:fft_result[i][1] z:deviceMotion.userAcceleration.z];
    51. [[weakSelf.graphViews objectAtIndex:kDeviceMotionGraphTypeFftwX] addX:fft_result[i][0] y:4 z:4];
    52. [ arrayFftwXRe.description writeToFile:docFileX atomically:YES encoding:NSUTF8StringEncoding error:nil];
    53. break;
    Alles anzeigen

    Jede Hilfe can mir wert sein. Sieht jemand dass ich was ändern soll dann bitte bescheid sagen.
    P.s.: meine Applikation soll die Beschleunigungsdaten zeichnen und durch FFTW filtern und noch mal zeichnen.
    Danke.
    josef

    Dieser Beitrag wurde bereits 10 mal editiert, zuletzt von josef907 ()

  • Michael schrieb:

    volker schrieb:

    Hm, wie und wo wird fft_result erzeugt? Da kracht es dann ja anscheinend beim reinschreiben ?!

    Nein, fft_result ist nicht das Problem, sonst hätte es schon früher gekracht.

    Ja hast du recht. fftw funktioniert. Ich habe alles durch NSlog gecheckt und alles funktioniert.
    ich habe den Array umgeschrieben:

    Quellcode

    1. ​ NSString *strArrayFftwXRe = [NSString stringWithFormat:@"%.6f ", fft_result[i][0]];
    2. NSLog(@"Data arrayFftwZRe [%d] : %@ ",i, arrayFftwZRe );
    3. [arrayFftwXRe addObject:strArrayFftwXRe];
    4. }
    5. [weakSelf setLabelValueX:fft_result[i][0] y:fft_result[i][1] z:deviceMotion.userAcceleration.z];
    6. [[weakSelf.graphViews objectAtIndex:kDeviceMotionGraphTypeFftwX] addX:fft_result[i][0] y:4 z:4];
    7. [ arrayFftwXRe.description writeToFile:docFileX atomically:YES encoding:NSUTF8StringEncoding error:nil]; ....

    Und es funktioniert. Aber jetzt habe ich ein andres Problem :) , wenn Ich es hier gleich beschreiben darf:
    Ich plote die Data X ,Y und Z. In der schleife ( line 41 bis 59) plote ich X. aber es hört nicht auf zu NSLog und ploten. Die schleife wiederholt sich. Wie kann ich es kontrollieren und einfach nur für
    das gegebene Array ( size=32) ploten? ?(
  • josef907 schrieb:

    Ja hast du recht. fftw funktioniert. Ich habe alles durch NSlog gecheckt und alles funktioniert.
    ich habe den Array umgeschrieben:

    Quellcode

    1. NSString *strArrayFftwXRe = [NSString stringWithFormat:@"%.6f ", fft_result[i][0]];
    2. NSLog(@"Data arrayFftwZRe [%d] : %@ ",i, arrayFftwZRe );
    3. [arrayFftwXRe addObject:strArrayFftwXRe];
    4. }

    Warum wandelst du Zahlen in Strings um, statt passendere Klassen wie NSNumber zu nutzen?

    Quellcode

    1. ​[arrayFftwXRe addObject:@(fft_result[i][0])];


    josef907 schrieb:

    Und es funktioniert. Aber jetzt habe ich ein andres Problem :) , wenn Ich es hier gleich beschreiben darf:
    Ich plote die Data X ,Y und Z. In der schleife ( line 41 bis 59) plote ich X. aber es hört nicht auf zu NSLog und ploten. Die schleife wiederholt sich. Wie kann ich es kontrollieren und einfach nur für
    das gegebene Array ( size=32) ploten? ?(

    Da steht ja auch noch alles mit NSLogs voll.
  • Michael schrieb:

    josef907 schrieb:

    Ja hast du recht. fftw funktioniert. Ich habe alles durch NSlog gecheckt und alles funktioniert.
    ich habe den Array umgeschrieben:

    Quellcode

    1. NSString *strArrayFftwXRe = [NSString stringWithFormat:@"%.6f ", fft_result[i][0]];
    2. NSLog(@"Data arrayFftwZRe [%d] : %@ ",i, arrayFftwZRe );
    3. [arrayFftwXRe addObject:strArrayFftwXRe];
    4. }

    Warum wandelst du Zahlen in Strings um, statt passendere Klassen wie NSNumber zu nutzen?

    Quellcode

    1. [arrayFftwXRe addObject:@(fft_result[i][0])];


    josef907 schrieb:

    Und es funktioniert. Aber jetzt habe ich ein andres Problem :) , wenn Ich es hier gleich beschreiben darf:
    Ich plote die Data X ,Y und Z. In der schleife ( line 41 bis 59) plote ich X. aber es hört nicht auf zu NSLog und ploten. Die schleife wiederholt sich. Wie kann ich es kontrollieren und einfach nur für
    das gegebene Array ( size=32) ploten? ?(

    Da steht ja auch noch alles mit NSLogs voll.


    Danke dir. ich hab schon alle geändert ( zu NSNumber).
    Ich nutze NSLogs um zu sehen bei jedem Schritt ob es richtig ist ( für ein Beginner ist gut :) ). Wenn Ich davon fertig bin , dann lösche die NSLog weg .
    und wegen die schleifen ? ob ich die Teilen kann?