Search code examples
matlabcsvcell-array

Importing CSV into Matlab


Chemical composition of a certain material

Hi,

I am trying to import the below mentioned data in CSV format in matlab, which is [1000x10] in dimensions.

HCL;H2SO4;CH4; SULPHUR;CHLORINE;S2O3;SO2;NH3;CO2;O2 144 2 3 141 140 6 7 137 136 10 11 133

13 131 130 16 17 127 126 20 21 123 122 24

25 119 118 28 29 115 114 32 33 111 110 36

108 38 39 105 104 42 43 101 100 46 47 97

96 50 51 93 92 54 55 89 88 58 59 85

61 83 82 64 65 79 78 68 69 75 74 72

73 71 70 76 77 67 66 80 81 63 62 84

60 86 87 57 56 90 91 53 52 94 95 49

48 98 99 45 44 102 103 41 40 106 107 37

109 35 34 112 113 31 30 116 117 27 26 120

121 23 22 124 125 19 18 128 129 15 14 132

12 134 135 9 8 138 139 5 4 142 143 1

I am able to import this data through my code

fid = fopen(uigetfile('.csv'),'rt');
FileName = fopen(fid);
headers = fgets(fid);    %get first line
headers = textscan(headers,'%s','delimiter',';'); %read first line
format = repmat('%f',1,size(headers{1,1},1)); %count columns n makeformat  string
data = textscan(fid,format,'delimiter',';'); %read rest of the file
data = [data{:}];

I am getting data in matrix form in variable data [1000x10] and name of all the components like HCL, H2SO4 in a cell array named headers{1x1}.

Now I have two questions like the built in import feature in matlab you have flexibility to import data as separate column vectors, numeric matrix,cell array and table format. Is it possible to do as such through code, like i get column vectors with their name HCL with [1000x1] and H2sO4 with [1000x1] in my workspace after import and so on all the column vectors with their names with [1000x1]dimensions. if yes then help me please...?

If above mentioned is not possible then i can do alternatively that now I have names of column vectors in headers cell array, how I can extract those name and use those names as column vector names through code and I can assign data from data matrix [1000x10] to each column vector with their corresponding names.

like if i say

x = headers {1*1}{1*1}; i will get x = "HCL" 
x = genvarname(x); I will get x= x0x22HCL0x2 BUT 
I want that x get replaced with HCL.and then I assign 
HCL = data(:,1) and same like this other variables H2SO4,SULPHUR, CHLORINE.
You can say i try to implement the import feature of column vector through     my code.

Kindly help me to solve this issue. thanks


Solution

  • If you'd like, you can use the two data types to create a table in MatLab. I'm not terribly familiar with its use, but it seems to be well documented. I'm sure someone else can expand upon this.

    Edit:

    After re-reading your question, I think this is closer to what you are after.

    n=10;
    what='HCL';%change this to any of the strings you interested in
    numstr = repmat('%f',1,n);
    hdrstr = repmat('%s',1,n);
    
    headers = textscan(headers,hdrstr,'delimiter',';');
    headers = headers(1,:)
    data = cell2mat(textscan(fid,numstr,'delimiter',';'));
    
    datout = data(:,strcmp(headers,what));%datout will be 1000x1 HCL data
    

    Depending on what you want to do, you can loop through these appropriately