0

I have 40 variables. The 40 variables names are in a cell array (40 x 1).

Each variable will have a matrix. The matrix is of type of double and will be size 5000 x 150. It will also have a vector of size 1 x 150 & one last vector 1 x 4.

Initially I was going to dynamically name each struct with its variable name in the cell array. So would look like something like below (assuming variable name is ABC),

ABC.dataMatrix
ABC.dataVec
ABC.summaryData

All the variables would be saved to a directory.

However I've read using eval isn't a very good idea so guessing my idea isn't the best way to go about this problem. What would be the best way to go about this problem?

2
  • 4
    Why not using array of struct? Like ABC(i).dataMatrix. It is not clear why you would have to use eval. Commented Feb 16, 2016 at 12:25
  • 4
    why not use dynamic field names? Commented Feb 16, 2016 at 12:49

1 Answer 1

0

You can either use struct arrays with dynamic field names, as @Shai and @RobertStettler suggest.

However, another option is a table. This might be more appealing if you want to see your data in one big matrix, and you can give each table row the name of your variables too! Note that the rows in a table would then be what you call your variables, but MATLAB calls the table columns its variables.

Also note that using a table can be more difficult than using struct or cell arrays, but if you know how to use them, you can handle a table too.

An example:

% create dummy data
rowNames = {'a';'b';'c'};
M = {ones(3); zeros(3); nan(3)}; % a cell, one element per item in rowNames
V = [1 2; 3 4; 5 6]; % a matrix of vectors, each row containing a vector for every item in rowNames

% create a table
T = table(M,V,'RowNames',rowNames); % this is where your variable names could go

Now, to access data you could use (some examples):

  • T(2,:) or T('b',:), return a table for all data on the 'b' row.
  • T(:,2) or T(:,'V'), return a table of variable V for all rows.
  • T.V or T{:,2} or T{:,'V'} or T.(2), return matrix V for all rows. This syntax is similar to accessing a (dynamic) field name of a struct.
  • T{3,1} or T{'c',1} or T.M('c'), return cell M for row 'c'. This syntax is similar to accessing a cell, but with more advanced possibilities, i.e. the ability to access the table via row or variable names.
  • T{3,1}{:} or T{'c',1}{:} or T.M{'c'}, return cell contents M for row 'c'.

And even more complex: T('a',:).M{:} is a complex way of accessing the cell content of M for row 'a', which can be done with T{1,1}{:} or T.M{'a'} or T{'a','M'}{:} or T.M{1} as well.

In your case you would en up with a 40x3 table, with every row what you call a variable and the first column the matrices (in cell arrays), and the last two columns the vectors (as well in cell arrays or as a 40xm double, m being the length of your vector).

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

Comments

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.