Compare char data in a cell {‘x’} to a character ‘x’
I want to find what’s in my table that’s in a category column 1
If you read through you will see what I tries and how I finally figured it out. I’m posing the question because I can’t be the only person that wanted to do this simple (but not clearly and simply documented)
Starting with a text file:
HEADER that is ignored
R 0 5
L 5 0
Set up how I want the table (not that MATLAB will pay any attention)
app.StimInputTable.Data = table(‘Size’,[4 3],…
‘VariableTypes’,{‘categorical’,’uint8′,’uint8′});
What did I get? Got what I asked for
K>> class (app.StimInputTable.Data.Var1)
ans =
‘categorical’
K>> class (app.StimInputTable.Data.Var2)
ans =
‘uint8’
K>> class (app.StimInputTable.Data.Var3)
ans =
‘uint8’
Read in the file
app.StimInputTable.Data = readtable(infile);
MATLAB changes all the types
K>> class (app.StimInputTable.Data.Var1)
ans =
‘cell’
K>> class (app.StimInputTable.Data.Var2)
ans =
‘double’
K>> class (app.StimInputTable.Data.Var3)
ans =
‘double’
I can redo the .. table(… VariableTypes), but then it tosses all my category data instead of converting
So here’s my table, I can deal with the cells
K>> app.StimInputTable.Data
ans =
2×3 table
Var1 Var2 Var3
_____ ____ ____
{‘L’} 5 0
{‘R’} 0 5
So the char is considered a cell, ok I’ve seen a warning about how chars (or char vectors) get put into cells, but my users are not going to type "R" 5 0 etc these files already exist.
K>> app.StimInputTable.Data(jj,1)
ans =
table
Var1
{‘L’}
Try to pull it out and I get another table, not the cell
OK so I pull it this way
K>> app.StimInputTable.Data{jj,1}
ans =
1×1 cell array
{‘L’}
Now I have a more simple 1×1 cell array, now to the character out of the cell or compare cell to cell
How about I compare it to a cell with the char in it
K>> {‘L’}
ans =
1×1 cell array
{‘L’}
That looks good, same types, lets try it
K>> app.StimInputTable.Data{jj,1} == {‘L’}
Operator ‘==’ is not supported for operands of type ‘cell’.
So that didn’t work, so I’ll try
K>> cl = app.StimInputTable.Data{jj,1}
cl =
1×1 cell array
{‘L’}
Get the char out of the cell
K>> cl{1,1}
ans =
‘L’
K>> cl{1,1}== ‘L’
ans =
logical
1
Finally!
cl = app.StimInputTable.Data{jj,1}
if cl{1,1}== ‘L’
…
end
Is this really what I have to do? It took way too long to figure out. I never could find Help on how to extract the single cell of data out of a table. I have R and L as catagories when I use them later but can’t read them in as such initing the table size and variabletypes gets ignored by the read()
Wait a moment
app.StimInputTable.Data{j,1} % This gives a cell as above, but
app.StimInputTable.Data.Var1{j} % is going to work? Where is that documented?
ans =
‘L’
So now
if app.StimInputTable.Data.Var1{j} == ‘L’
Apparently using .Var1{i} is not the same as indexing with {j}
This works but where will you find it documented? Good luckI want to find what’s in my table that’s in a category column 1
If you read through you will see what I tries and how I finally figured it out. I’m posing the question because I can’t be the only person that wanted to do this simple (but not clearly and simply documented)
Starting with a text file:
HEADER that is ignored
R 0 5
L 5 0
Set up how I want the table (not that MATLAB will pay any attention)
app.StimInputTable.Data = table(‘Size’,[4 3],…
‘VariableTypes’,{‘categorical’,’uint8′,’uint8′});
What did I get? Got what I asked for
K>> class (app.StimInputTable.Data.Var1)
ans =
‘categorical’
K>> class (app.StimInputTable.Data.Var2)
ans =
‘uint8’
K>> class (app.StimInputTable.Data.Var3)
ans =
‘uint8’
Read in the file
app.StimInputTable.Data = readtable(infile);
MATLAB changes all the types
K>> class (app.StimInputTable.Data.Var1)
ans =
‘cell’
K>> class (app.StimInputTable.Data.Var2)
ans =
‘double’
K>> class (app.StimInputTable.Data.Var3)
ans =
‘double’
I can redo the .. table(… VariableTypes), but then it tosses all my category data instead of converting
So here’s my table, I can deal with the cells
K>> app.StimInputTable.Data
ans =
2×3 table
Var1 Var2 Var3
_____ ____ ____
{‘L’} 5 0
{‘R’} 0 5
So the char is considered a cell, ok I’ve seen a warning about how chars (or char vectors) get put into cells, but my users are not going to type "R" 5 0 etc these files already exist.
K>> app.StimInputTable.Data(jj,1)
ans =
table
Var1
{‘L’}
Try to pull it out and I get another table, not the cell
OK so I pull it this way
K>> app.StimInputTable.Data{jj,1}
ans =
1×1 cell array
{‘L’}
Now I have a more simple 1×1 cell array, now to the character out of the cell or compare cell to cell
How about I compare it to a cell with the char in it
K>> {‘L’}
ans =
1×1 cell array
{‘L’}
That looks good, same types, lets try it
K>> app.StimInputTable.Data{jj,1} == {‘L’}
Operator ‘==’ is not supported for operands of type ‘cell’.
So that didn’t work, so I’ll try
K>> cl = app.StimInputTable.Data{jj,1}
cl =
1×1 cell array
{‘L’}
Get the char out of the cell
K>> cl{1,1}
ans =
‘L’
K>> cl{1,1}== ‘L’
ans =
logical
1
Finally!
cl = app.StimInputTable.Data{jj,1}
if cl{1,1}== ‘L’
…
end
Is this really what I have to do? It took way too long to figure out. I never could find Help on how to extract the single cell of data out of a table. I have R and L as catagories when I use them later but can’t read them in as such initing the table size and variabletypes gets ignored by the read()
Wait a moment
app.StimInputTable.Data{j,1} % This gives a cell as above, but
app.StimInputTable.Data.Var1{j} % is going to work? Where is that documented?
ans =
‘L’
So now
if app.StimInputTable.Data.Var1{j} == ‘L’
Apparently using .Var1{i} is not the same as indexing with {j}
This works but where will you find it documented? Good luck I want to find what’s in my table that’s in a category column 1
If you read through you will see what I tries and how I finally figured it out. I’m posing the question because I can’t be the only person that wanted to do this simple (but not clearly and simply documented)
Starting with a text file:
HEADER that is ignored
R 0 5
L 5 0
Set up how I want the table (not that MATLAB will pay any attention)
app.StimInputTable.Data = table(‘Size’,[4 3],…
‘VariableTypes’,{‘categorical’,’uint8′,’uint8′});
What did I get? Got what I asked for
K>> class (app.StimInputTable.Data.Var1)
ans =
‘categorical’
K>> class (app.StimInputTable.Data.Var2)
ans =
‘uint8’
K>> class (app.StimInputTable.Data.Var3)
ans =
‘uint8’
Read in the file
app.StimInputTable.Data = readtable(infile);
MATLAB changes all the types
K>> class (app.StimInputTable.Data.Var1)
ans =
‘cell’
K>> class (app.StimInputTable.Data.Var2)
ans =
‘double’
K>> class (app.StimInputTable.Data.Var3)
ans =
‘double’
I can redo the .. table(… VariableTypes), but then it tosses all my category data instead of converting
So here’s my table, I can deal with the cells
K>> app.StimInputTable.Data
ans =
2×3 table
Var1 Var2 Var3
_____ ____ ____
{‘L’} 5 0
{‘R’} 0 5
So the char is considered a cell, ok I’ve seen a warning about how chars (or char vectors) get put into cells, but my users are not going to type "R" 5 0 etc these files already exist.
K>> app.StimInputTable.Data(jj,1)
ans =
table
Var1
{‘L’}
Try to pull it out and I get another table, not the cell
OK so I pull it this way
K>> app.StimInputTable.Data{jj,1}
ans =
1×1 cell array
{‘L’}
Now I have a more simple 1×1 cell array, now to the character out of the cell or compare cell to cell
How about I compare it to a cell with the char in it
K>> {‘L’}
ans =
1×1 cell array
{‘L’}
That looks good, same types, lets try it
K>> app.StimInputTable.Data{jj,1} == {‘L’}
Operator ‘==’ is not supported for operands of type ‘cell’.
So that didn’t work, so I’ll try
K>> cl = app.StimInputTable.Data{jj,1}
cl =
1×1 cell array
{‘L’}
Get the char out of the cell
K>> cl{1,1}
ans =
‘L’
K>> cl{1,1}== ‘L’
ans =
logical
1
Finally!
cl = app.StimInputTable.Data{jj,1}
if cl{1,1}== ‘L’
…
end
Is this really what I have to do? It took way too long to figure out. I never could find Help on how to extract the single cell of data out of a table. I have R and L as catagories when I use them later but can’t read them in as such initing the table size and variabletypes gets ignored by the read()
Wait a moment
app.StimInputTable.Data{j,1} % This gives a cell as above, but
app.StimInputTable.Data.Var1{j} % is going to work? Where is that documented?
ans =
‘L’
So now
if app.StimInputTable.Data.Var1{j} == ‘L’
Apparently using .Var1{i} is not the same as indexing with {j}
This works but where will you find it documented? Good luck get data in a cell MATLAB Answers — New Questions