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
"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:
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Calendar에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!