├── Codes └── single_data_file_writer_comsol_v2.m ├── LICENSE └── README.md /Codes/single_data_file_writer_comsol_v2.m: -------------------------------------------------------------------------------- 1 | % Mohammad Asif Zaman 2 | % Converting multiple 2D table data from Comsol into a single csv file with 3 | % 3D data 4 | % Input files: csv files exported from comsol with XY plane data of a 5 | % function, fx(x,y,zo). Each file has data for a different zo file. 6 | % This will be repeated for other functions fy(x,y,zo) and fz(x,yo,zo) 7 | % 8 | % Input csv file data structure: 9 | % Column 1: x value (space coordinate) 10 | % Column 2: y value (space coordinate) 11 | % Column 4: fx value (function 1) 12 | % Column 5: fy value (function 2) 13 | % Column 6: fz value (function 3) 14 | % 15 | % 16 | % Output csv file data structure: 17 | % Column 1: x value (space coordinate) 18 | % Column 2: y value (space coordinate) 19 | % Column 3: z value (space coordinate) 20 | % Column 4: fx value (function 1) 21 | % Column 5: fy value (function 2) 22 | % Column 6: fz value (function 3) 23 | 24 | 25 | 26 | 27 | 28 | 29 | clear all; 30 | close all; 31 | clc; 32 | clf; 33 | 34 | fname_root = 'Data/ODEP/ro_0.5_zo_'; % Root name of the input csv files with directory location 35 | n_header_row = 5; % Number of header rows in the csv file that should be ignored. 36 | 37 | fname_out = 'Data/ODEP/CODEP_ro_0.5.csv'; % output csv file name 38 | 39 | zo_set = [1.5 1.8 2.3 2.8 3.3 3.8]; % Suffix of file names which also indicate z value of the function 40 | 41 | z_offset = 0; % z-offset value. Used for shifting z=0 reference 42 | 43 | 44 | 45 | % Defining variable arrays 46 | xdata = []; 47 | ydata = []; 48 | zdata = []; 49 | Fxdata = []; 50 | Fydata = []; 51 | Fzdata = []; 52 | 53 | 54 | % Sweeo through each file 55 | for m = 1:length(zo_set) 56 | zo = zo_set(m); % Extract z value from file name 57 | 58 | file_name = [fname_root, num2str(zo),'.csv']; % file name 59 | Mdata = csvread(file_name,n_header_row); % Ignore the header rows 60 | 61 | % order of the variables should be appropriately changed here 62 | xdata = [xdata; Mdata(:,1)]; 63 | ydata = [ydata; Mdata(:,2)]; 64 | zdata = [zdata; ones(length(Mdata(:,2)),1)*(zo-z_offset)]; 65 | Fxdata = [Fxdata; Mdata(:,4)]; 66 | Fydata = [Fydata; Mdata(:,5)]; 67 | Fzdata = [Fzdata; Mdata(:,6)]; 68 | end 69 | 70 | % Output variables 71 | Mdata_combined = [xdata ydata zdata Fxdata Fydata Fzdata]; 72 | 73 | % Write output csv file 74 | csvwrite(fname_out,Mdata_combined); 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Mohammad Asif Zaman 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # COMSOL-to-MATLAB-data-process 2 | MATLAB codes for combining, processing csv data files exported from COMSOL. 3 | 4 | # Files 5 | 6 | ## single_data_file_writer_comsol_v2.m 7 | Combines multiple csv files into a single csv file. The input csv files contains data of a 2D sweep (i.e. F(x,y,zo) is evaluated when x and y are varied and zo is kept constant). Each input csv file is evaluated for a different value of the third parameter (zo is different for each csv file). The output csv file combines the input files to create tabulated F(x,y,z) values where all three variables are swept. This csv file can be used to make a mat file. 8 | --------------------------------------------------------------------------------