How to plot specific rows from table

19 views (last 30 days)
John
John on 6 Jun 2025
Commented: John on 9 Jun 2025
I am trying to plot data based on the specimen id, I have two types of speciments. 0.12C and 0.07C Steel, as seen in the photo here is the table.
I would like to plot the data with different markers for each Plate ID, but i can't seem to figure out how to plot them. I have tried calling out the rows and varible when plotting but it won't work.
Here is how i load the data:
Table4 = readtable('Grain Size.xlsx','sheet','KIC Master','VariableNamingRule','preserve');
Here is how I am trying to plot:
plot(ax2,Table4(1:7,"Ferrite Grain Size-1/2 (in.)"),Table4(1:7,"Provisional To °F"),'Color','black','LineStyle','none','Marker','x','MarkerEdgeColor','black','MarkerFaceColor','black');
plot(ax2,Table4(8:11,"Ferrite Grain Size-1/2 (in.)"),Table4(8:11,"Provisional To °F"),'Color','black','LineStyle','none','Marker','S','MarkerEdgeColor','black','MarkerFaceColor','black');
I get this error:
Error using plot
Invalid subscript for Y. A table variable subscript must be a numeric array containing real positive integers, a logical array, a character vector, a string array, a cell
array of character vectors, or a pattern scalar used to match variable names.Error using plot
Invalid subscript for Y. A table variable subscript must be a numeric array containing real positive integers, a logical array, a character vector, a string array, a cell
array of character vectors, or a pattern scalar used to match variable names.
What am I missing here? The only way i have gotten this kind of thing to work in the past is by creating seaperate tables for each specimen type then plotting each table individually. I would like to at least keep them in the same table and be able to call our which rows I want to plot.
Thank You!

Accepted Answer

Stephen23
Stephen23 on 6 Jun 2025
Edited: Stephen23 on 7 Jun 2025
Simple Solution:
Instead of trying to plot two tables, it would be much better to plot the table content. It just requires changing four characters to accesss the table content:
plot(ax2,Table4{1:7,"Ferrite Grain Size-1/2 (in.)"},Table4{1:7,"Provisional To °F"},...)
% ^ ^ ^ ^
Table4 = array2table(rand(13,2), VariableNames=["Provisional To °F","Ferrite Grain Size-1/2 (in.)"]);
plot(Table4{1:7,"Ferrite Grain Size-1/2 (in.)"},Table4{1:7,"Provisional To °F"})
Explanation:
Container arrays are arrays that can contain other arrays, just like a box can hold other things (including other boxes). MATLAB has several types of container arrays, e.g. cell arrays, structure arrays, string arrays, tables, timetables, etc. All container arrays have two basic ways to index them:
  1. indexing that returns a subset of an array (i.e. returns an array of the same type), or
  2. indexing that returns the content of a container array.
The MATLAB indexing that corresponds to both those two ways:
  1. parentheses () returns a subset of an array (actually this applies to all array types), or
  2. curly-braces {} or dot indexing returns the content of a container array.
so if you index into a table array using parentheses then you will get another table array. If you index into a table using curly braces then you will get the content of that table. And when you plot data, it only really makes sense to plot the content of a table (because you are trying to plot numeric data, not a data container).
Note that numeric arrays, logical arrays, character arrays, etc. cannot contain other arrays (i.e. are not container arrays) and therefore do not support dot indexing or curly-brace indexing.
Container arrays are just boxes to put things into and just like a box we can either pass the entire box or take something out of it and pass that. Just as in spoken langauge "please give me that box" is different from "please take an apple out of that box and give me the apple" so MATLAB also distinguishes between these operations.
  3 Comments
Stephen23
Stephen23 on 7 Jun 2025
Edited: Stephen23 on 7 Jun 2025
"When to use TableX.(Varible) or Table(Rows,Cols)... "
Use whatever works best.
But your original problem is that you did not distinguish between the container array and its content. That is what I tried to help you understand.
John
John on 9 Jun 2025

Alright reading the above comment after your edit it makes a lot more sense what I was doing wrong. I think I'm being to understand how (),{}, and dot index works. I could have use curly brackets in my original code to call the content I wanted. Thank you for this extra explanation!

Sign in to comment.

More Answers (0)

Tags

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!