MATLAB Answers

Operation on variables whose name contain a certain string.

79 views (last 30 days)
Julien Gorenflot
Julien Gorenflot on 14 Sep 2017
Commented: Jan on 20 Sep 2017
Hi!
I've been trying a bunch of ways to treat some data, so I end up having a few variable called "treated_data_blablabla".
Let's say I want to do an operation on all of those variable (plot them, normalize them, etc). How can I just access them all, or loop through them?
I can get the list of them with
who treated_data_*
but that's only displayed. I managed to get the list in a variable with
test = who('-regexp','treated_data*');
But this is a matrix of cells, not even strings. So I still don't know how I access the variables whose names are in stored in test
  7 Comments
Jan
Jan on 20 Sep 2017
@Julien: I hope you see the chances to improve the programming style for your future projects. Many existing projects suffer from a bad design coming from the early phases of the development. A code starts as a short hack, a bunch of script, global variables, input and output through MAT files. Then the code grows and the programmer hesitates to replace the clumsy methods, because they are working already. And finally the software reaches a limit, where further modifications are getting exponentially harder due to the bad foundation.
The standard solution is a refactoring of the code: Use the collected experiences and rewrite the complete code from scratch. And in the next project, start to implement the parts such, that they are not effected by a modification of the underlying data structure.
But what is the best suggestion for you to your current problem? If this is the final phase of your project and after the presentation nobody will use the code anymore? Then a complete refactoring is a waste of time. The awkward who('-regexp','treated_data*') method will work. It is easy to access a matrix of cells. But I think Donald's suggestion is very useful and can be applied directly.
@Stephen: "Luckily that is your problem, not mine." Ooops?! You care very exhaustively about the problems of programmers, who fail at the limits of the dynamic creation of variables. Your contributions in this forum are much more useful and valuable than "this is your problem". What about: "Luckily your problem can be solved easily and I can show you how."?

Sign in to comment.

Answers (1)

OCDER
OCDER on 16 Sep 2017
Edited: OCDER on 16 Sep 2017
In case you're stuck with 1000's of treated_data_blahblah, here's an example showing how to work around this issue by correcting the variables before trying to operate on them.
%Poorly labeled variables
A_1 = rand(1);
A_2 = rand(2);
A_3 = rand(3);
A_4 = rand(4);
A_10 = rand(10);
%Convert poorly labeled variables to scalar structure first (and to cell later if you can)
Prefix = 'A_'; %Variable name before the changing portion (in your case, 'treated_data_')
save('temp_BadVarNames.mat', '-regexp', Prefix);
T = load('temp_BadVarNames.mat'); %Loads variables into scalar structure, T.A_1, T.A_2, etc.
delete('temp_BadVarNames.mat');
%OPTION1: Using scalar structures
%To operate on every data, use structfun or for loop. Ex:
A = structfun(@(x) x*10, T, 'UniformOutput', false); %Multiplies everything by 10
%OPTION2: Using cells
%If your variable name changes numerically, (A_1 A_2 instead of A_a A_b)
%then you can store values in a cell instead.
%Convert loaded variable into cell
CellIndex = cellfun(@(x) str2double(strrep(x, Prefix, '')), fieldnames(T));
A = cell(max(CellIndex), 1);
A(CellIndex) = struct2cell(T);
%To operate on every cell element in A, use cellfun or a for loop. Ex:
A = cellfun(@(x) x*10, A, 'UniformOutput', false); %Multiplies everything by 10
  1 Comment
OCDER
OCDER on 19 Sep 2017
Hmmmm, I thought this answer was great for your case...
"Only I haven't done it so far and I still need to handle my old data and sometimes, you need the result in one hour, so it's too late to reorganize everything..."
Which is why the above code automatically corrects your labeling scheme, in like, 0.0002 seconds. I already figured you had "1000's of treated_data_blahblah" according to Mind Reading Toolbox. :)
For your case, stick to Option1 : Scalar Structure, if variable name is more informative than index. Ex:
T.treated_data_monkey
T.treated_data_dog

Sign in to comment.

Tags

No tags entered yet.

Community Treasure Hunt

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

Start Hunting!