How can I loop/index through a table inside a table to generate plots? I'd like to store the plots back into the table.

17 views (last 30 days)
I've built a table (Named: Book1) in MATLAB that is 83x5, 83 rows and 5 columns.
Each row in the 5th column has a table with dimension Nx3, N rows and 3 columns.
- I'd like to index/loop into each table in the 5th column and plot the 1st-column vs. 3rd-column
-During looping/indexing, I'd like to create a 6th column(Named: Plot) in the table (Named: Book1) that will store each genreated plot.
After looping//indexing, I'd like to view the plot by simply clicking Plot1,Plot2,Plot3,etc... An example of the completed output is below:
This is possible as I've seen it before, unsure how to approach / carry out the request.

Accepted Answer

ProblemSolver
ProblemSolver on 27 Jun 2023
I am not sure, if this is what you are exactly looking for:
To achieve your desired output, you can loop through each table in the 5th column of your main table, plot the 1st column against the 3rd column, and store the generated plot in a new column. Here's an example of how you can do it:
% Access the table in the 5th column of Book1
subTables = Book1(:, 5);
% Loop through each table
for i = 1:size(subTables, 1)
% Get the current table
currentTable = subTables{i};
% Extract the columns for plotting
x = currentTable(:, 1);
y = currentTable(:, 3);
% Plot the data
figure;
plot(x, y);
% Store the plot in the 6th column of Book1
Book1.Plot{i} = gcf;
end
Book1(:, 5) accesses the 5th column of the Book1 table, which contains the sub-tables. The loop iterates over each sub-table, extracts the columns for plotting, creates a plot, and stores it in the 6th column of Book1 using Book1.Plot{i}.
After running the code, you can access the generated plots by simply clicking on the corresponding plot in the Book1 table's 6th column.
  1 Comment
Rookie Programmer
Rookie Programmer on 28 Jun 2023
Edited: Rookie Programmer on 28 Jun 2023
I'm getting an error stating " Subscripting a table using linear indexing (one subscript) or multidimensional indexing (three or more subscripts) is not supported. Use a row subscript and a variable subscript."
What does this exactly mean? How would I correct this issue?

Sign in to comment.

More Answers (1)

Steven Lord
Steven Lord on 28 Jun 2023
After running the code, you can access the generated plots by simply clicking on the corresponding plot in the Book1 table's 6th column.
That's true as long as the figure window remains open. That code does not somehow "embed" the figure into the table array. Once the figure window gets closed, the handle becomes a handle to a deleted figure.
t = table((1:5).', gobjects(5, 1), 'VariableNames', ["data", "figures"])
t = 5×2 table
data figures ____ _______________________________________ 1 1×1 matlab.graphics.GraphicsPlaceholder 2 1×1 matlab.graphics.GraphicsPlaceholder 3 1×1 matlab.graphics.GraphicsPlaceholder 4 1×1 matlab.graphics.GraphicsPlaceholder 5 1×1 matlab.graphics.GraphicsPlaceholder
x = 1:10;
for r = 1:height(t)
t.figures(r) = figure;
ax = axes('Parent', t.figures(r));
plot(x, x.^r, 'Parent', ax)
end
t{3, 'figures'}
ans =
Figure (3) with properties: Number: 3 Name: '' Color: [1 1 1] Position: [671 558 577 433] Units: 'pixels' Show all properties
close all
t{3, 'figures'}
ans =
handle to deleted Figure
Also, creating and leaving open 83 separate figure windows is likely to make it difficult to find a specific one you want to look at, and could consume a good amount of memory especially if the data you're plotting is large. You haven't stated what values of N you're working with.
What is your end goal in creating and storing the figures in the table? We may be able to suggest a more robust and/or more efficient way to solve the underlying problem you're trying to solve without opening nearly 100 figures.

Community Treasure Hunt

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

Start Hunting!