Take string from cell array for name of variable in matlab workspace

。_饼干妹妹 提交于 2019-12-02 11:38:45

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

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!