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

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 件のコメント

Stephen23
Stephen23 2020 年 3 月 25 日
編集済み: Stephen23 2020 年 3 月 25 日
"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:

サインインしてコメントする。

 採用された回答

Ameer Hamza
Ameer Hamza 2020 年 3 月 25 日
編集済み: Ameer Hamza 2020 年 3 月 25 日
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 件のコメント

Thanks for the answer. I want to eventually export each separate matrix to a excel final based on its name, which is why I want to create a separate variable for each matrix.
Also, the fix you supplies creates a 1x36 double, is there a way to keep it in matrix format as a 6x6 double?
You can rehape the matrix to keep it in 6*6 form
eval([date{k} '= reshape([Ghat{:,:,k}],6,[]);']);
Stephen23
Stephen23 2020 年 3 月 25 日
編集済み: Stephen23 2020 年 3 月 25 日
"I want to eventually export each separate matrix to a excel final based on its name, which is why I want to create a separate variable for each matrix."
There is absolutely no need to create separate arrays for that.
If you want to export them, then you do NOT "need" to magically create different arrays, just use basic indexing in the loop and call whatever data exporting function you want (e.g. writematrix or xlswrite).
Your current approach is just pointlessly complex and inefficient.
What is the point of saying my method is complex and inefficient if you aren't going to suggest how to change it? 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.
Ameer Hamza
Ameer Hamza 2020 年 3 月 25 日
As you mentioned in your other question, the end goal was to save each slice of the 3D matrix in a separate file. I have updated the answer to show you a way to avoid eval altogether.
Stephen23
Stephen23 2020 年 3 月 25 日
編集済み: Stephen23 2020 年 3 月 25 日
"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:
Thanks Ameer, very helpful!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

質問済み:

2020 年 3 月 25 日

編集済み:

2020 年 3 月 25 日

Community Treasure Hunt

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

Start Hunting!

Translated by