How to call variable (with index) using string?

75 次查看(过去 30 天)
Suppose I have many variables with arbitrary names in my workspace with all same size. I also have an identical logical index for update all those variables.
load('database.mat')
% For example this database has 1000 mixed-typed variables with all same size (20000x1)
variableNames = who;
validIndex = randi([0 1],20000,1);
for i=1:length(variableNames)
variableNames(i) = variableNames(i)(validIndex); % I know this line is wrong, so is there any other way to do this?
end
I want to update all those variables with same logical index, i tried who and eval command but still confused. Also if possible, I dont want to convert those variable into cell, because some of them need to be repeated in the code, so it might be harder to recognize without reading the variable name directly in the code. So any suggestion how do I do this?

采纳的回答

Cam Salzberger
Cam Salzberger 2020-1-17
编辑:Cam Salzberger 2020-1-17
Adam's suggestion really is the way to go about addressing the fundamental issue. In the meantime, if you want to get access to important data in those variables in the mat file, you can load them into a struct and access the fields with the variable names:
S = load('database.mat');
variableNames = fieldnames(S);
% Extract 10th entry, for example
outArr = zeros(numel(variableNames), 1);
for k = 1:numel(variableNames)
varVal = S.(variableNames{k});
outArr(k) = varVal(10);
end
-Cam
  1 个评论
Gifari Zulkarnaen
Gifari Zulkarnaen 2020-1-18
Thanks a lot, your answer give me insight, in addition to Adam Danz's explanation. The final solution that I want is:
S = load('database.mat');
variableNames = fieldnames(S);
for k = 1:numel(variableNames)
temp = S.(variableNames{k});
S.(variableNames{k}) = temp(validIndex); % validIndex is predefined logical index
end
In this way, all the variables can be updated without typing them all one by one, and I can still recognize some variables that I use in my code by reading directly its name (in struct).

请先登录,再进行评论。

更多回答(2 个)

Adam Danz
Adam Danz 2020-1-17
编辑:Adam Danz 2020-1-17
It sounds like at some point in your code you created a bunch of dynamically named variables. This opens a can of worms and makes it more difficult to work with your data. Here's an explanation why dyanamic variable naming should be avoided at all cost.
Instead, those variables should be stored all together in an array. You can store them in a matrix or a multidimensional array (if the variables are all numeric or logical), or in a cell array or a table. The table and cell array can store your variable names at the top. Otherwise you can create a 2nd variable that stores the variable names so you can identify the rows, columns, etc.
Once you have reorganized your data, your indexing question will have a very simple solution.
Even if you don't have access to the code that produced those variable or you cannot recreate them, your best bet is to restrcuture them according to the description above before moving forward.
If you have any problems, let us know which format you chose and I'd be glad to help further.
  3 个评论
Stephen23
Stephen23 2020-1-18
编辑:Stephen23 2020-1-18
"Btw, my variables are not dynamically named."
Actually they are: when calling load without an output variable (that structure) its contents are allocated to dynamically named variables in the calling workspace, either into variables that did not exist before load was called or overwriting existing variables without warning. It quacks like a duck and walks like a duck...
Gifari Zulkarnaen
Gifari Zulkarnaen 2020-1-18
I see. Perhaps I mistakenly understood the terminology. Thanks again.

请先登录,再进行评论。


Image Analyst
Image Analyst 2020-1-17
Ignoring the probably bad idea that these are variable names that you will refer to later and try to use (because I'm sure Stephen will chime in), I will just assume they are strings. It looks like you want to randomize the order of the string array or cell array of strings stored in a .mat file. To do that
storedStructure = load('database.mat')
% For example this database has 1000 mixed-typed variables with all same size (20000x1)
strings = storedStructure.variableNames;
% Get random indexes:
validIndex = randperm(length(strings));
% Re-order the string array according to the new, scrambled indexes:
strings = strings(validIndex);
If my interpretation is wrong, feel free to clarify.
  1 个评论
Gifari Zulkarnaen
Gifari Zulkarnaen 2020-1-18
Thanks for your response, but I dont want to randomize the order. I want to update the variables, from initally 20000x1 array, into around 12000x1 array from logical index, these 12000 are the only valid data after screening.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Data Type Identification 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by