필터 지우기
필터 지우기

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.

조회 수: 30 (최근 30일)
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.

채택된 답변

ProblemSolver
ProblemSolver 2023년 6월 27일
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
Rookie Programmer
Rookie Programmer 2023년 6월 28일
편집: Rookie Programmer 2023년 6월 28일
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?

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Steven Lord
Steven Lord 2023년 6월 28일
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.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by