MATLAB Answers

How to call variable (with index) using string?

55 views (last 30 days)
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?

  0 Comments

Sign in to comment.

Accepted Answer

Cam Salzberger
Cam Salzberger on 17 Jan 2020
Edited: Cam Salzberger on 17 Jan 2020
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 Comment

Gifari Zulkarnaen
Gifari Zulkarnaen on 18 Jan 2020
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).

Sign in to comment.

More Answers (2)

Adam Danz
Adam Danz on 17 Jan 2020
Edited: Adam Danz on 17 Jan 2020
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 Comments

Gifari Zulkarnaen
Gifari Zulkarnaen on 18 Jan 2020
Thank you for your explanation. Hope I can accept two answers. Btw, my variables are not dynamically named.
Stephen Cobeldick
Stephen Cobeldick on 18 Jan 2020
"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 on 18 Jan 2020
I see. Perhaps I mistakenly understood the terminology. Thanks again.

Sign in to comment.


Image Analyst
Image Analyst on 17 Jan 2020
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 Comment

Gifari Zulkarnaen
Gifari Zulkarnaen on 18 Jan 2020
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.

Sign in to comment.