Table Variable Name-dot indexing

6 views (last 30 days)
Mos_bad
Mos_bad on 14 Feb 2021
Commented: Walter Roberson on 15 Feb 2021
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 Comments
Mos_bad
Mos_bad on 14 Feb 2021
Edited: Walter Roberson on 15 Feb 2021
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 on 15 Feb 2021
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.

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!