0

I need to work with variables at each iteration of a loop in Matlab whose names depend on the loop index h (e.g. if h=1 I want to use data1 e to create other variables). Is there a way to do it? I cannot use cells, because the variables are very large matrices and I have memory problems using cells.

Example:

data1=[1,2,3];
data2=[4,5,6];
data3=[7,8,9];  %they are in the workspace

for h=1:3
    % A`h'=data`h'+6
    % save A`h'
end
6
  • How do you get memory problems with cells and not the other way? Do you store them locally and call them individually, or do you keep them in the MATLAB workspace? Anyhow, check out sprintf and fprintf. I could mention eval, but then I would get comments saying eval is evil! Commented Jan 15, 2015 at 9:39
  • I keep all variables in the Matlab workspace. I don't know why if I store all of them using cells I get memory problems and, instead, if I save them separately I don't. Commented Jan 15, 2015 at 9:41
  • @StewieGriffin eval is evil!!! this is why you get these comments! use struct with dynamic field names instead Commented Jan 15, 2015 at 9:53
  • @Shai, I have never answered a question eval, or used it myself! So I have never gotten those comments =) It was a dishonorary mention =P Commented Jan 15, 2015 at 9:56
  • @StewieGriffin good for you! Commented Jan 15, 2015 at 9:56

2 Answers 2

3

I think you should consider using structure with dynamic field names (see more details here).
For example

 for h=1:n
     dataName = sprintf('data%d', h); %// dynamic name
     resultName = sprintf('res%d', h); %// dynamic name
     base.(resName) = myFunction( base.(currentName) ); %// process data and save to result
 end

The nice thing about this approach (especially if you run into memory problems) is that save and load supports this approach:

 for h=1:n
     dataName = sprintf('data%d', h); %// dynamic name

     base = load( 'myHugeMatFile.mat', dataName ); %// loads only one variable from the file
     %// now the variable is a field in base 

     resultName = sprintf('res%d', h); %// dynamic name

     base.(resName) = myFunction( base.(currentName) ); %// process data and save to result

     save( 'myResultsFile.mat', '-struct', '-append', 'base' ); %// please verify this works - I'm not 100% certain here.
 end

Note how save and load can tread struct fields as different variables when needed.

Sign up to request clarification or add additional context in comments.

3 Comments

Not clear to me. Can you use the example I put in the question?
@user3285148 Do not create the workspace variables in a "flat" manner, but rather as fields of base struct. For example, instead of data1 = [1,2,3]; use base.data1=[1,2,3]; and so on. now you can access using h: dn = sprintf('data%d',h); base.(dn)
Can you please write what you would do using my example?
-1

As far as I understand, you either want to make a matrix:

 A = [1 2 3;
      4 5 6;
      7 8 9];

or a vector:

A =[1; 2; 3; 4; 5; 6; 7; 8; 9];

In the first case, just writing

A = [data1;data2;data3];

should do the trick. Otherwise, look into horzcat for a horizontal vector and vertcat for a vertical one:

A = horzcat(data1,data2,data3);
A = vertcat(data1',data2',data3');

4 Comments

? This is not the answer to my question
Then I did not understand your question. You should probably follow the dynamic field names solution then.. Anyway, this!
Btw, the first A is not a matrix! And it's better to use .' when transposing (notice the dot). And it doesn't look remotely close to solving OP's problem, but hey... points for trying.
Just a minor, minor comment more: A good thing about MATLAB is that the ; in the end of each row in the matrix is not necessary. This is to allow copy-paste of tables... (But they don't do any damage, so it's not a problem to have them there,,, it's just not something you have to do) =)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.