某种材料的化学成分
您好,
我正在尝试在matlab中以CSV格式导入下面提到的数据,尺寸为[1000x10]。
HCL; H 2 SO 4; CH 4;硫;氯; 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
我可以通过我的代码
导入这些数据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{:}];
我在变量数据[1000x10]中以矩阵形式获取数据,并在名为headers {1x1}的单元数组中获取所有组件的名称,如HCL,H2SO4。
现在我有两个问题,比如matlab中的内置导入功能,你可以灵活地将数据导入为单独的列向量,数值矩阵,单元格数组和表格格式。是否有可能通过代码执行此操作,例如我在导入后在我的工作空间中获取名称为HCL [1000x1]且H2sO4为[1000x1]的列向量,以及所有具有[1000x1]维度名称的列向量。 如果是,请帮助我......?
如果上面提到不可能,那么我可以选择现在我在头单元格数组中有列向量的名称,如何提取这些名称并通过代码将这些名称用作列向量名称,我可以从数据中分配数据矩阵[1000x10]到每个列向量及其对应的名称。
就像我说一样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.
请帮我解决这个问题。谢谢
答案 0 :(得分:1)
您是否尝试过内置的readtable功能?
您可以使用指定的列标题访问表的每一列。
答案 1 :(得分:0)
我知道这不是你要求的,但我会转换为结构:
x=cell2struct(num2cell(data),headers,2)
原因很简单,例如选择具有单个变量的第三行是不可能的。使用结构只需使用x(3)
如果在某些时候你需要你最初要求的矢量而你不能使用条纹,请使用[x.HCL]
答案 2 :(得分:0)
如果您愿意,可以使用这两种数据类型在MatLab中创建表格。我对它的使用并不十分熟悉,但似乎有很好的记录。我相信其他人可以扩展这个。
编辑:
重新阅读你的问题之后,我认为这更接近你所追求的目标。
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
根据您的目的,您可以适当地循环使用