Table Variable Name-dot indexing

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 Kommentare

Walter Roberson
Walter Roberson am 14 Feb. 2021
Which MATLAB release are you using?
Could you confirm that class(Table1) is table ?
I'm using R2020a release.
Good point Walter, Table 1 is cell.
Here is the code i wrote to create Table 1. Is there any way to create table instead?
for i = 1:Nev
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
%Last three columns
Table1{i,(j-1)*3+4} = CoverageA(i,1);
Table1{i,(j-1)*3+5} = CoverageD(i,1);
Table1{i,(j-1)*3+6} = TotalLoss(i,1);
end
cell2table(Table1, 'VariableNames', VarName(1,:))
Mos_bad
Mos_bad am 14 Feb. 2021
Thanks, table creation problem solved! Now back to my initial question, is there a more efficient way to define the variables' names than I did?
compose() and use "" delimited format
[compose("Location %d-Building loss", 1:Nbu),
compose(...)
compose(...)]
and reshape to a row vector.
Mos_bad
Mos_bad am 14 Feb. 2021
Bearbeitet: Walter Roberson am 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)
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.

Melden Sie sich an, um zu kommentieren.

Antworten (0)

Kategorien

Gefragt:

am 14 Feb. 2021

Kommentiert:

am 15 Feb. 2021

Community Treasure Hunt

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

Start Hunting!

Translated by