108 views (last 30 days)

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

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

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

Opportunities for recent engineering grads.

Apply Today
## 7 Comments

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/356673-operation-on-variables-whose-name-contain-a-certain-string#comment_484372

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/356673-operation-on-variables-whose-name-contain-a-certain-string#comment_484372

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/356673-operation-on-variables-whose-name-contain-a-certain-string#comment_484383

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/356673-operation-on-variables-whose-name-contain-a-certain-string#comment_484383

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/356673-operation-on-variables-whose-name-contain-a-certain-string#comment_484386

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/356673-operation-on-variables-whose-name-contain-a-certain-string#comment_484386

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/356673-operation-on-variables-whose-name-contain-a-certain-string#comment_484392

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/356673-operation-on-variables-whose-name-contain-a-certain-string#comment_484392

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/356673-operation-on-variables-whose-name-contain-a-certain-string#comment_485566

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/356673-operation-on-variables-whose-name-contain-a-certain-string#comment_485566

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/356673-operation-on-variables-whose-name-contain-a-certain-string#comment_485578

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/356673-operation-on-variables-whose-name-contain-a-certain-string#comment_485578

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/356673-operation-on-variables-whose-name-contain-a-certain-string#comment_485982

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/356673-operation-on-variables-whose-name-contain-a-certain-string#comment_485982

Sign in to comment.