Load data from csv file (iphone SDk)

穿精又带淫゛_ 提交于 2019-12-05 05:20:26

问题


Does anyone know how to load data from csv file?

For the code example, CPTestAppScatterPlotController.m that I downloaded from core-plot Google website, a line graph can be plotted based on randomly generated initial data x, y.

 // Add some initial data
 SMutableArray *contentArray = [NSMutableArray arrayWithCapacity:100];
 NSUInteger i;
 for ( i = 0; i < 60; i++ ) {
     id x = [NSNumber numberWithFloat:1+i*0.05];
     id y = [NSNumber numberWithFloat:1.2*rand()/(float)RAND_MAX + 1.2];
     [contentArray addObject:[NSMutableDictionary 
                      dictionaryWithObjectsAndKeys:x, @"x", y, @"y", nil]];
}
self.dataForPlot = contentArray;

Then i modified the code,

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"ECG_Data" ofType:@"csv"];
NSString *myText = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil ];
NSScanner *scanner = [NSScanner scannerWithString:myText];
[scanner setCharactersToBeSkipped:[NSCharacterSet characterSetWithCharactersInString:@"\n, "]];
NSMutableArray *newPoints = [NSMutableArray array];           
float time, data;
while ( [scanner scanFloat:&time] && [scanner scanFloat:&data] ) {
    [newPoints addObject:
    [NSMutableDictionary dictionaryWithObjectsAndKeys:
            [NSNumber numberWithFloat:time], @"time",
            [NSNumber numberWithFloat:data], @"data",
             nil]];
}
self.dataForPlot = newPoints;

It seems that my code could not read the data from csv file. (there are two cols of the data in ECG_Data.csv, one for time and one for data) Can anyone give me some suggestion??? Thanks!!!!


回答1:


http://cocoawithlove.com/2009/11/writing-parser-using-nsscanner-csv.html

Here is a good place to start for creating a CSV parser. It's complete with sample code and user comments.



来源:https://stackoverflow.com/questions/2426374/load-data-from-csv-file-iphone-sdk

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!