Adding data from csv file into separate columns

12 views (last 30 days)
Hi, I am trying to import data from several .csv files. I am successfully importing them but then I want to create a new array using data from the csv files. For instance, all csv files are data taken at the same location for different simulations. I would like to combine them so that my final data file has column 1: locations (which are the same for each), column 2: data from simulation 1, column 3: data from simulation 2, and so on...
So far, I have
name = 'lineX1_';
dataSaved = 'p_rgh_p_magU_';
which = 'mesh';
currentFolder = pwd;
dir = '/home/ariel/OpenFOAM/ariel-2.4.0/run/meshConvergenceJacobsen/postProcessing/'
disp(['Current Folder: ' currentFolder])
list = strtrim(ls(dir))
%mm = {'1';'2';'3';'4'};
mm = {'1';'4';};
data = [];
for jj = 1:length(mm)
data_current = load([dir name dataSaved which mm{jj} '.csv']);
x = data_current(:,1);
magU = data_current(:,6);
data = [x,magU];
end
This of course is rewriting the array each time with the new data rather than add the data to the next column. How would I do this?
Thanks in advance!
  1 Comment
Jan
Jan on 29 Sep 2016
Do not use "dir" and "which" as names of variables, because this shadows builtin functions. The dir command is more direct than parsing the string output of ls.

Sign in to comment.

Answers (1)

Jan
Jan on 29 Sep 2016
dataC = cell(1, length(mm));
for jj = 1:length(mm)
data_current = load([dir name dataSaved which mm{jj} '.csv']);
dataC{jj} = data_current(:,[1, 6]);
end
data = cat(2, data_current{:});
  1 Comment
Ariel Edesess
Ariel Edesess on 29 Sep 2016
Thanks very much, this is almost doing what I wanted but I'm getting the error:
Cell contents reference from a non-cell array object.
Error in meshConvergenceJacobsen (line 22)
data = cat(2,data_current{:});
It is reading in the data and making two separate arrays of two columns, but then not putting them together..

Sign in to comment.

Categories

Find more on Operators and Elementary Operations in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!