How to create new matrix, named after elements in a column vector, from a multidimensional array

1 view (last 30 days)
I have a multidimensional array (6x6x5274 cell, named 'Ghat'), and 5274x1 cell (named 'date'), and I want to create new create separate matrices - 5274 6x6 matricies - named after the dimensions in the date vector. The date vector follows the pattern of network_yyyymmdd.
When I run the code currently, it runs to the end and creates the new variables, however, each variable is a 1x1 double which contains a 0.
This is the code, the original 'data.mat' contains 'date' and 'Ghat':
I know it isn't best practice to use eval to name variables dynamically, but I just need to run this code once in order to separate the matrices
clear
%Import data
load('data.mat')
%Convert date to colunm vector in yyyymmdd format
date = date';
date = datetime(date);
date = date + calyears(2000);
date = yyyymmdd(date);
date = num2str(date);
date1 = cell(length(date), 1);
date1(:) = {'network_'};
date2 = strcat(date1,date);
date = date2;
clear date1 date2
Ghat = num2cell(Ghat);
for k=1:1:length(date)
eval([date{k} '= Ghat{:,:,k};'])
end
  1 Comment
Stephen23
Stephen23 on 25 Mar 2020
Edited: Stephen23 on 25 Mar 2020
"I know it isn't best practice to use eval to name variables dynamically"
Correct.
"but I just need to run this code once in order to separate the matrices"
Wrong.
According to your comment below, your aim is to export to Excel. There is absolutely NO reason why you "need" to dynamically define any variables just to export data (or subsets of data) to Excel files. You just decided to force yourself into writing code this inefficeint, buggy, complex, obfuscated way (it is not because of MATLAB).
I recommend that instead you simply follow the examples in the MATLAB documentation:

Sign in to comment.

Accepted Answer

Ameer Hamza
Ameer Hamza on 25 Mar 2020
Edited: Ameer Hamza on 25 Mar 2020
I am not sure why you want to create the variables like this dynamically, maybe a table or a timetable is a better solution. However, I suspect the following will fix your code
eval([date{k} '= [Ghat{:,:,k}];'])
Something like this should work without eval()
%Import data
load('data.mat')
%Convert date to colunm vector in yyyymmdd format
date = date';
date = datetime(date);
date = date + calyears(2000);
date = yyyymmdd(date);
date = num2str(date);
for i=1:numel(date)
name = [date{i}, '.xlsx'];
writematrix(Ghat(:,:,i), name);
end
  7 Comments
Stephen23
Stephen23 on 25 Mar 2020
Edited: Stephen23 on 25 Mar 2020
"What is the point of saying my method is complex and inefficient if you aren't going to suggest how to change it?"
In my last comment I explained how you can do this in a simpler and more efficient way: directly calling the exporting function on subsets of the array (no dynamically named variables are required). The point of that is to show you how to write simpler, neater, more efficient code.
"I asked for help using the method I chose, if you aren't going to help me fix that method than replying is pointless and futile."
In fact this is such a common issue on forums and help-desks that it even has a name:

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!