从单元格数组中获取字符串作为Matlab工作区中变量的名称 [英] Take string from cell array for name of variable in matlab workspace

查看:836
本文介绍了从单元格数组中获取字符串作为Matlab工作区中变量的名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的实验中有大量的.csv文件(超过200个),以前我一直在分别阅读它们,并且在以后的数据处理步骤中,这是繁琐的工作.

I have a large amount of .csv files from my experiments (200+) and previously I have been reading them in seperately and also for later steps in my data handling this is tedious work.

co_15 = csvread('CO_15K.csv',5,0);
co_25 = csvread('CO_25K.csv',5,0);
co2_15 = csvread('CO2_15K.csv',5,0);
co2_80 = csvread('CO2_80K.csv',5,0);
h2o_15 = csvread('H2O_15K.csv',1,0);
etc.....

因此,我想在代码的开头创建一个像这样的单元格,然后创建一个for循环,该循环将自动读取它们.

So I want to make a cell at the beginning of my code looking like this and then a for loop that just reads them automatically.

input = {'co_15' 5;'co_25' 5;...
         'co2_15' 5; 'co2_80' 5;...
         'h2o_15' 1; 'h2o_140' 1;...
         'methanol_15' 5;'methanol_120' 5;'methanol_140' 5;...
         'ethanol_15' 5;'ethanol_80' 1;'ethanol_140' 5;...
         'co2_ethanol_15' 5 ;'co2_ethanol_80' 5;...
         'h2o_ethanol_15' 1 ;'h2o_ethanol_140' 1;...
         'methanol_ethanol_15' 5;'methanol_ethanol_120' 5;'methanol_ethanol_140' 5};

for n = 1:size(input,1)
    input{n,1} = csvread(strcat(input{n,1},'k.csv'),input{n,2},0);
end

此代码中的单元格为19行和2列,这些行是所有文件,并且这些列将包含用于处理数据的参数.现在我找不到解决方案的问题是,我的第一列是一个字符串名称,并且我希望该字符串名称成为csvread将数据写入其中的变量的名称,但是我现在设置它的方式只是覆盖了它包含csv数据的单元格第一列中的字符串.更清楚地说,我希望我的matlab工作区在包含csv文件数据的第一列中具有带有字符串名称的变量.我该如何解决?

The cell in this code is 19 rows and 2 columns, the rows are all the files and the columns will contain the parameters to handle the data. Now the problem I can't find a solution for is that my first column is a string name and I want that string name to be the name of the variable where csvread writes its data to but the way I set it up now it just overwrites the string in the first column of the cell with the csv data. To be extra clear I want my matlab workspace to have variables with string names in the first column containing the data of my csv files. How do I solve this?

推荐答案

您实际上并非 想要这样做. 甚至 Mathworks 不要这样做.如果您试图使用变量名来跟踪此类相关数据,那么总是有一个更好的数据结构来保存您的数据.

You don't actually want to do this. Even the Mathworks will tell you not to do this. If you are trying to use variable names to keep track of related data like this, there is always a better data structure to hold your data.

一种方法是拥有一个单元格数组

One way would be to have a cell array

data = cell(size(input(:,1)));
for n = 1:size(input,1)
    data{n} = csvread(strcat(input{n,1},'k.csv'),input{n,2},0);
end

另一个好的选择是使用struct.您可能只有一个struct,其动态字段名称与您的数据相对应.

Another good option is to use a struct. You could have a single struct with dynamic field names that correspond to your data.

data = struct();
for n = 1:size(input,1)
    data.(input{n,1}) = csvread(strcat(input{n,1},'k.csv'),input{n,2},0);
end

或者实际上创建一个结构数组,并在结构中同时保留名称​​和.

Or actually create an array of structs and hold both the name and the data within the struct.

for n = 1:size(input, 1)
    data(n).name = input{n,1};
    data(n).data =  csvread(strcat(input{n,1},'k.csv'),input{n,2},0);
end

如果您绝对坚持这样做(同样,强烈建议您不要使用 ),那么您可以使用

If you absolutly insist on doing this (again, it's is very much not recommended), then you could do it using eval:

for n = 1:size(input, 1)
    data = csvread(strcat(input{n,1},'k.csv'),input{n,2},0);
    eval([input{n, 1}, '= data;']);
end

这篇关于从单元格数组中获取字符串作为Matlab工作区中变量的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆