Make code faster for import and array creation
Ältere Kommentare anzeigen
Dear All,
Here is a bit of code that I wrote to import a bunch of XY data in CSV form, extract the first column from every file and store it as any array (mydata_x), then extract the second column from every file and store it as an array (mydata_y). Then I made a third array that will order all the files for plotting (mydata_z)
It works, but it's unbelievable slow. My question is, how can I make it faster?
%bring the data in from CSV format into a cell-array
csvFiles = dir('*.csv'); %works for the current directory only
numfiles = length(csvFiles); %try to keep only the data you want in the dir
mydata = cell(1,numfiles); %finds the total number of files in the dir
%save each XY data file into a cell of a cell-array called 'mydata'
for k = 1:numfiles
mydata{k} = importdata(csvFiles(k).name);
end
%Now I need to generate the X, Y, and Z data
for i=1:numfiles
mydata_X(:,i) = [mydata{i}(:,1)] % get data from the first column from all cells in mydata
end
for i=1:numfiles
mydata_Y(:,i) = [mydata{i}(:,2)] % get data from the second column from all cells in mydata
end
for i=1:numfiles
arrayOfZ = 1:numfiles
mydata_Z(i,:) = arrayOfZ(1,:) % here I assign 1 through 'numfiles' to offest all spectra
end
%from here I can mesh(mydata_X, mydata_Y, mydata_Z)
Any help would be greatly appreciated!!
Jenna
Akzeptierte Antwort
Weitere Antworten (0)
Kategorien
Mehr zu MATLAB finden Sie in Hilfe-Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!