Need to output my data which is currently in a for loop to a table!
    4 visualizaciones (últimos 30 días)
  
       Mostrar comentarios más antiguos
    
    Olivia Milton-thompson
 el 13 de Oct. de 2015
  
    
    
    
    
    Comentada: Olivia Milton-thompson
 el 15 de Oct. de 2015
            I have a set of data which I had to read into matlab using a for loop, the code which works nicely is shown below:
 directory = 'C:\Users\....';
 listing = dir(directory);
 for i = 1:length(listing)
    if strcmp(listing(i).name, '.') || strcmp(listing(i).name, '..')
        continue
    end;
    file = listing(i).name;
    currfile = fopen(strcat(directory,listing(i).name));
    tline  = fgets(currfile);
    fclose(currfile);
end;
However, when you look at what "tline" outputs, it produces this as shown below:
 tline =
 Bolton,26.000,88.571,70.000,77.943,63.091,36.700,47.400,48.200
 tline =
 Thames Valley,33.000,94.286,68.667,78.657,61.455,30.500,51.600,37.300
 tline =
 London S Bank,28.000,81.429,84.000,76.514,64.909,28.000,51.600,43.700
This is fine, it's exactly what I want but I can't figure out how to then transform what's shown above into a nice table with 9 columns and headings. I am able to create a table taking out that data using this code shown below:
 T = table({'London S Bank'},[28.000],[81.429],[84.000],[76.514],[64.909],[28.000],[51.600],[43.700], 'VariableNames', {'UN','SS','RQ','SR','SF','ES','C','GH','GP'})
But I have 113 lines of data which needs to all be in the same table. Any ideas how I can do this straight from my loop without typing out all the data?
Thank you!
0 comentarios
Respuesta aceptada
  Peter Perkins
    
 el 13 de Oct. de 2015
        Olivia, all you have are strings, and presumably you want one column of strings and eight columns of numbers. TastyPastry's suggestion will work, but likely you would need use str2double somewhere in the process to convert strings like '26.000' to actual numbers.
Another suggestion would be to use textscan to turns each strings into one string and eight numbers, something along the lines of:
tline = cell(113,1);
for i = ...
   ...
   tline{i} = fgets(currfile);
end
C = textscan(strjoin(tline),'%s%f%f%f%f%f%f%f%f','delimiter',',');
T = table(C{:},'VariableNames',{'Location' 'X' 'Y' ...})
This is a somewhat advanced maneuver, but you might find it useful.
Más respuestas (1)
  TastyPastry
      
 el 13 de Oct. de 2015
        
      Editada: TastyPastry
      
 el 13 de Oct. de 2015
  
      You need to format your data into a cell array by using the comma as a delimiter. The function strsplit() ( documentation ) can do that. From what I'm seeing, each file has one line of data. You may want to use fgetl(), since it doesn't look like you need the newline character. You'll need to use strsplit() after you read in a line, then vertically concatenate the new line onto the end of your data cell array. You can also preallocate the cell array by setting the number of rows to the number of files you have in the directory, length(listing). This is a good habit, since it'll make your programs faster if you're working with large amounts of data.
Then, create the table using the columns of the cell array you created. The function table() has options which are listed in the documentation which will allow you to create headers and specify the data type in each column.
Ver también
Categorías
				Más información sobre Logical en Help Center y File Exchange.
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!