필터 지우기
필터 지우기

Table Variable Name-dot indexing

조회 수: 8 (최근 30일)
Mos_bad
Mos_bad 2021년 2월 14일
댓글: Walter Roberson 2021년 2월 15일
I need to creat a table with the following variable names :
A1 B1 C1 A2 B2 C2 A3 B3 C3 , ... An Bn Cn X Y Z
A1 B1 C1 are Table's variable names that their values come from seprate arrays A, B, and C. Also, the last three column of the table, X, Y, AND Z, have nothing to do with aformentioned arrays. Any thoughts how to define variable names as above?
Here are what I wrote. Besides getting an error "Unable to perform assignment because dot indexing is not supported for variables of this type" , I believe there is definately a way more efficient way to do that.
% Assume A, B, and C as Location %d–Building Loss, Location %d–Time Element Loss, and Location %d–Total Loss
for n=1:Nbu
VarName{1 , (n-1)*3+1} = sprintf('Location %d–Building Loss', n);
VarName{1 , (n-1)*3+2} = sprintf('Location %d–Time Element Loss', n);
VarName{1 , (n-1)*3+3} = sprintf('Location %d–Total Loss', n);
end
% Assume Building Loss, Time Element Loss, and Total Loss(Building + Time Element Loss) as X,Y, and Z; VARIABLESS' NAMES OF THE LAST THREE COLUMNS
VarName{1 , Nbu*3+1} = sprintf('Building Loss', n);
VarName{1 , Nbu*3+2} = sprintf('Time Element Loss', n);
VarName{1 , Nbu*3+3} = sprintf('Total Loss(Building + Time Element Loss)', n);
Table1.Properties.VariableNames = VarName(1,:);
  댓글 수: 7
Mos_bad
Mos_bad 2021년 2월 14일
편집: Walter Roberson 2021년 2월 15일
Thanks, That worked and "compose" reduced several line of codes.
Last question, Any thoughts on rewriting the nested for loop in my previous comment?
(here is the direct link)
Walter Roberson
Walter Roberson 2021년 2월 15일
for j=1:Nbu
Table1{i,(j-1)*3+1} = BuildingLoss{i}(:,j) ;
Table1{i,(j-1)*3+2} = BuildingTimeElementLoss{i}(:,j);
Table1{i,(j-1)*3+3} = BuildTotLoss{i}(:,j) ;
end
could be:
Table1(i,:,1) = num2cell(BuildingLoss{i}, 1);
Table1(i,:,2) = num2cell(BuildingTimeElementLoss{i}, 1);
Table1(i,:,3) = num2cell(BuildingTotLoss{i}, 1);
Table1(i,:,4) = {CoverageA(i,1)}; %same value for each j
Table1(i,:,5) = {CoverageD(i,1)}; %same value for each j
Table1(i,:,6) = {TotalLoss(i,1)}; %same value for each j
and reshape afterwards to collapse the second and third dimension together. Or better yet, don't.

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by