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.
19 次查看(过去 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.
0 个评论
采纳的回答
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 个)
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"])
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'}
close all
t{3, 'figures'}
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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Whos 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!