Logic statement error checking an array within a cell within a table
1 次查看(过去 30 天)
显示 更早的评论
Hi,
I am trying to write an if condition where if a variable name (in this case 'pressure') appears in an array that is stored in a cell of the table "podList", then the condition is true:
if 1 == ismember('pressure',settingsSet.podList(currentPod,VarNames).Properties.VariableNames)
I am getting the error: "Unrecognized table row name 'SPODD8'."
The variable "currentPod" changes in a higher loop and the first check is 'SPODD8'
See attached pictures for the structure of the table podList and VarNames.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1723926/image.png)
podList is currently a 19x4 but can change to more or fewer rows based on the input pods (which are research instruments)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1723931/image.png)
VarNames is currently a 1x36 cell but could aslo change based on the type of pod used. For my applications it is always either a 1x36 or a 1x42.
I also am not sure if I am calling out the row of VarNames correctly here as I haven't gotten that deep, so any help suggestions there would be appreciated!
Thank you in advance.
5 个评论
回答(3 个)
Steven Lord
2024-6-27
Let's break this line of code down into pieces. I've commented it out so I can run code later in this answer.
% if 1 == ismember('pressure',settingsSet.podList(currentPod,VarNames).Properties.VariableNames)
First section of code
The section settingsSet.podList refers to the table in the first picture in the post. That does look like a table array. So this section seems reasonable.
Second section of code, part 1
The section settingsSet.podList(currentPod,VarNames) will return a table consisting of the rows referred to by currentPod and the variables referred to by VarNames. I believe from the fact that you said currentPod is 'SPODD8' that you wanted this to retrieve the first row in settingsSet.podList, but that's not what it does. Sure, this looks like it should be the same as the "Index by Row Names" section on this documentation page, but the first variable in the picture is not a set of row names. Compare:
load patients
% Only need a few rows for demonstration purposes
LastName = LastName(1:5);
Age = Age(1:5);
noRowNames = table(LastName, Age)
lastNameRowNames = table(Age, RowNames=LastName)
The lastNameRowNames table has row names; I can index into it using 'Johnson', for example, to obtain row 2.
lastNameRowNames('Johnson', :)
But I can't index into noRowNames using 'Johnson' any more than I could index into it using the value 43 from the Age variable. LastName and Age are both variables in noRowNames; neither is a set of row names.
try
noRowNames('Johnson', :)
catch ME
fprintf("This call threw error:\n%s", ME.message)
end
I could use 'Johnson' with the LastName variable in noRowNames to index into it. It's a little more verbose, but it allows you to keep LastName as data in the table rather than metadata. The results look close to the results from the operation of indexing into lastNameRowNames with row name 'Johnson'.
noRowNames(matches(noRowNames.LastName, 'Johnson'), :)
Second section of code, part 2
In your code you refer to VarNames. But you haven't showed us the contents of that variable. Sure, your table does have a variable named VarNames, but your usage of it in that indexing expression doesn't refer to that variable. You'd need to refer to it as settingsSet.podList.VarNames to refer to that variable or settingsSet.podList.VarNames{1} to refer to the first cell in that variable from the table.
Third section of code
Okay, so if you obtain a sub-table from your array you then try to access one of its Properties. settingsSet.podList(currentPod,VarNames).Properties.VariableNames. But this isn't going to somehow refer to the VarNames variable in the table, if that's what you're expecting. Based on that picture and your statement that the table settingsSet.podList is a 19-by-4 table array, the list of VariableNames of the table is {'podName', 'VarNames', 'VarTypes', 'timezone'}. But that's not what you're trying to do, it sounds like.
Suggestion
If you want to process each row in this table, looking to see if the contents of the VarNames variable in that row contains the text 'pressure', consider using rowfun. Something along these lines ought to work, though because I don't have your actual data I can't test it. [Since it's not going to run I left it commented out.]
%{
underPressure = rowfun(@(x) ismember('pressure', x), ...
settingsSet.podList, ... % Process the rows of the settingsSet.podList table
InputVariables = {'VarNames'}, ... % Only pass the VarNames variable in
GroupingVariables = {'podName'}) % Group by values from podName
%}
The InputVariables parameter will tell MATLAB to pass only the VarNames variable from the row into the anonymous function, and the GroupingVariables parameter will tell MATLAB to group the results by the unique values in the podName variable.
0 个评论
Walter Roberson
2024-6-27
编辑:Walter Roberson
2024-6-27
if 1 == ismember('pressure',settingsSet.podList(currentPod,VarNames).Properties.VariableNames)
settingsSet.podList appears to be a table. If () indexing of it works then the result would be a table. You then ask for the VariableNames property of the table... which is going to be the same as VarNames (provided VarNames is indeed names and not numeric.) This seems like a bit of a waste: you could just use VarNames directly.
Comparing 1 == to ismember() is inefficient. ismember() in this form responds with either 0 (false) or 1 (true). You can just use the ismember() result directly in the test.
if ismember('pressure',VarNames)
But it looks to me as if what you want to test is
if ismember('pressure', settingsSet.podList.VarNames{currentPod})
0 个评论
Voss
2024-6-27
settingsSet.podList = load('podList.mat').T;
settingsSet.podList
Nrows = size(settingsSet.podList,1);
VarNamesContainsPressure = false(Nrows,1);
for row = 1:Nrows
% currentPod = settingsSet.podList.podName{row};
if ismember('pressure',settingsSet.podList.VarNames{row})
VarNamesContainsPressure(row) = true;
end
end
VarNamesContainsPressure
2 个评论
Voss
2024-6-27
编辑:Voss
2024-6-27
Yes. In this case the set of pods to check is specified in podsToCheck, and those are iterated over.
settingsSet.podList = load('podList.mat').T;
settingsSet.podList
podsToCheck = {'SPODD8','SPODE2'};
N = numel(podsToCheck);
VarNamesContainsPressure = false(N,1);
for ii = 1:N
currentPod = podsToCheck{ii};
rows = strcmp(settingsSet.podList.podName,currentPod);
if ismember('pressure',[settingsSet.podList.VarNames{rows}])
VarNamesContainsPressure(ii) = true;
end
end
VarNamesContainsPressure
VarNamesContainsPressure is true whenever 'pressure' appears in any element of VarNames in rows where podName is currentPod. In particular, the possibility that currentPod appears multiple times or zero times in podName is handled.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!