CodexBloom - Programming Q&A Platform

How to implement guide with dynamic plotting of multiple datasets in matlab using 'hold on'

👀 Views: 0 đŸ’Ŧ Answers: 1 📅 Created: 2025-06-14
matlab plotting cell-arrays

I'm migrating some code and Quick question that's been bugging me - I'm working on a MATLAB script to visualize multiple datasets on the same figure window, but I'm working with unexpected behavior when trying to use `hold on` for dynamic plotting... My datasets are stored in a cell array, where each cell contains a 1D array of data. The goal is to plot each dataset in a loop and maintain all previous plots in the figure. After setting up the figure, I'm using the following code snippet: ```matlab figure; for i = 1:length(dataCellArray) plot(dataCellArray{i}); hold on; end ``` However, I notice that only the last dataset is displayed in the figure, and the previous datasets seem to disappear. I've tried placing `hold on` after the `figure` command, but this doesn't solve the question. I also experimented with `legend` to check if the plots are being overwritten, but the legend only shows the last dataset. I've verified my versions and I'm using MATLAB R2023b. Is there something specific I might be missing about the `hold on` functionality or the way I'm handling the cell array? Any insights would be greatly appreciated, especially relating to the handling of multiple plot calls within a loop. Thanks! What's the best practice here? I'm working in a CentOS environment. Am I approaching this the right way? I'm coming from a different tech stack and learning Matlab. Thanks for your help in advance!