How to efficiently access multiple value in a cell [ ACCESS MULTIPLE ROW FOR A SINGLE COLUMN]
Show older comments
Dear Matlab coder, How to access multiple row for a single column in a cell. Currently, I had to use this dumb way to access multiple row for the column 8 as shown below. May I know how to further improve the code? The excel file can be download from DataExcel
function [at] = MeanPVT (at)
for i = 1:length (at.Valindex)
A= (at.Valindex (:,i))';
m1 = (at.raw{A(1),8});
m2 = (at.raw{A(2),8});
m3 = (at.raw{A(3),8});
at.average (i) = (m1+m2+m3)/3;
end
end
On the same note , is it possible to make the code in the function INDEXINGVALUE more compact. Particularly, is there any way I can remove the usage of SWITCH. In the current framework, the state of the CONDITION_3 is depending to the Condition_TTDay. In other word, CONDITION_3 will have 2 or 3 state if the Condition_TTDay is either BL or (SS1 & SS2).
filename = 'DataExcel.xlsx';
[~,~,at.raw] = xlsread(filename,'Sheet1','','basic');
at.Condition_Cond = {'ACond' 'BCond'};
at.Condition_TTDay = {'BL' 'SS1' 'SS2' 'SS3'};
[at] = IndexingValue (at);
[at] = MeanPVT (at);
function [at] = IndexingValue (at)
c= 1;
for i=1:2
for j =1:4
at.condition_1 = at.Condition_Cond {i};
at.condition_2 = at.Condition_TTDay {j};
switch at.condition_2
case 'BL'
for k =1:2
at.condition_3 = k;
at.Valindex (:,c) = calMean (at);
c=c+1;
end
otherwise
for k =1:3
at.condition_3 = k;
at.Valindex (:,c) = calMean (at);
c=c+1;
end
end
end
end
end
function Valindex = calMean (at)
valid = find(cellfun('isclass', at.raw(:,2), 'char') & ...
cellfun('isclass', at.raw(:,3), 'char') & ...
cellfun('isclass', at.raw(:,7),'double'));
Valindex = ...
valid((strcmp(at.raw(valid,2), at.condition_1)) & ...
(strcmp(at.raw(valid,3), at.condition_2)) & ...
([at.raw{valid,7}].' == at.condition_3));
end
3 Comments
dpb
on 9 Jul 2017
Came back to look at your other problem a little more...left with a question:
In calMean the logical array Valindex is going to be for the elements of the raw array as selected by the preceding find operation, NOT for the full raw array. Are you sure this is what you want because you don't decimate raw by the elements not selected to which you then subsequently apply the Valindex vector in MeanPVT.
balandong
on 10 Jul 2017
Indeed, the index is ok; while length(valid) is shorter than raw valindex does, indeed, hold the locations relative to the full size so it is correct. I got an extra level of nesting in there while reading code first time and was thinking the shortening would've happened before the find operation but as written it doesn't...is ok, yes. Sorry for the sidetrack.
Accepted Answer
More Answers (0)
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!