Saving Loop Data into two different cell arrays
1 回表示 (過去 30 日間)
古いコメントを表示
Hi Everyone,
I am having problems with a bit of looping code which imports all the .csv files from a folder. The .csv files have two data columns and are numerical. I am trying to extract the columns from each file into two different cell arrays. One which stores all of the column one data from a .csv file and then puts it in a column and the same with column two with each file.
I am trying to end up with something like this.
D_Array Where D 1:4 are roughly 512x1
File(i) 1 2 3 4
D1 D2 D3 D4
At the moment as far as I can tell the loop is working however I am just capturing the last files data.
I am really struggling to implement the other answers on the forum as they are all working with one array and not multiple array outputs within a loop.
I hope this all make sense! I didn't like the look of the uploaded format below so attached the .m file.
Regards,
Chris
Code Below:
if true
clear all clc
%% Import Multiple Files...Assign ArrayData...Assign Depth delimiter = ','; if nargin<=2 startRow = 2; endRow = inf; else endRow = 2; end %% Format string for each line of text: % column1: double (%f) % column2: double (%f) formatSpec = '%f%f%[^\n\r]'; %% Open the folder dir. cd '/Users/Chris1/Documents/MATLAB/Terrain/TerrainSlices'; files=dir('*.csv'); Num = length(files(not([files.isdir]))) %% Open each csv k = 1;
for i=1:Num fid= fopen(files(i).name); data(k) = importdata(files(i).name) DA = textscan(fid, formatSpec, endRow(1)-startRow(1)+1, 'Delimiter', delimiter, 'HeaderLines', startRow(1)-1, 'ReturnOnError', false) for block=2:length(startRow) frewind(fid) DABlock = textscan(fid, formatSpec, endRow(block)-startRow(block)+1, 'Delimiter', delimiter, 'HeaderLines', startRow(block)-1, 'ReturnOnError', false) for col=1:length(DA) DA{col} = [DA{col};DABlock{col}] end end
%%Allocate imported data to column Array
% -------->>>>>>>>>>Having Problems here
for DA{1} = {:,i}
D_Array = D_Array + DA{1}
end
E_Array = DA{2}
% -------->>>>>>>>>>
%%Increment to next line
k = k + 1;
end
% Depth Array Data Scripted on {i} initial = 0 increase on k=i Number of % Depth files
kmax = max(k)-1; Depth_Array = [1:kmax]; %Move Back to working dir Matlab cd '/Users/Chris1/Documents/MATLAB/Terrain'; %Import if succesful if error say error importing
%uiwait(msgbox('Multiple Import Completed','Success','modal'));
%% end
0 件のコメント
回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Matrices and Arrays についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!