Access to a table, why matlabs run with code: T = T(:,[varnames(others) 'SelfAsses​sedHealthS​tatus']); , but later can not run with this one: T(:, ['Systolic' 'Diastolic']) = [];

1 view (last 30 days)
I have the code below in which 'patients.dat is a sample file in Matlab.
clear all
clc
T = readtable('patients.dat');
T(:, ['Systolic' 'Diastolic']) = [];
varnames = T.Properties.VariableNames;
others = ~strcmp('SelfAssessedHealthStatus', varnames);
T = T(:, [varnames(others) 'SelfAssessedHealthStatus']);
T(1:5,6:7)
When I run this code. Matlab reported an error message:
Error using magic_square (line 5)
Unrecognized variable name 'SystolicDiastolic'.
When I remove line: T(:, ['Systolic' 'Diastolic']) = []; , matlab works well.
My problem is that: the two lines: T = T(:, [varnames(others) 'SelfAssessedHealthStatus']); and T(:, ['Systolic' 'Diastolic']) = []; Have the same syntax, but why matlab reported an error message at the latter (T(:, ['Systolic' 'Diastolic']) = [];)
Can somebody help me explain this phenomenon? many thanks and best regards!

Accepted Answer

Stephen23
Stephen23 on 8 Feb 2016
Edited: Stephen23 on 8 Feb 2016
The problem is that you misunderstand the meaning of square brackets []. Many beginners think that square brackets construct a list, but in fact they are a concatenation operator. So when you write this:
['Systolic' 'Diastolic']
you concatenate the two strings together into one string:
>> ['Systolic' 'Diastolic']
ans =
SystolicDiastolic
It does not construct a list of those strings! In fact the error message message shows this quite clearly too:
Unrecognized variable name 'SystolicDiastolic'.
You see, actually reading the error message is a good habit, because it contains useful information to understand what is happening. Here it showed you one string that you are using as a variable name, not two strings like you think you are using.
The solution, like it always is, is to read the documentation:
"When indexing into a table with parentheses or curly braces, you can specify vars as a colon, numeric indices, logical expressions, a single variable name, or a cell array of row names." There is a bug in the documentation because that should read "a cell array of variable names.", and this is easy to figure out from the context.
A cell array of variable names will resolve your problem:
T(:,{'Systolic','Diastolic'})
  3 Comments
Stephen23
Stephen23 on 8 Feb 2016
Edited: Stephen23 on 8 Feb 2016
Because the varnames are in a cell array MATLAB places the trailing string into a cell before concatenating them, like this:
>> [{'A','B'},'C']
ans =
'A' 'B' 'C'
MATLAB does an implicit type conversion from string to cell (of a string), thus the output is a cell array of strings and this is correct for using to index tables with.

Sign in to comment.

More Answers (0)

Categories

Find more on Cell Arrays in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!