implementing custom plotting functions in MATLAB affecting performance in R2023b
I can't seem to get I am experiencing important performance optimization when using a custom plotting function in MATLAB R2023b. The function is designed to plot multiple datasets in a single figure, but as the number of datasets increases, the response time becomes unacceptably slow. Here is the core of my plotting function: ```matlab function customPlot(data) figure; hold on; for i = 1:length(data) plot(data{i}.x, data{i}.y, 'DisplayName', data{i}.name); end hold off; legend show; grid on; end ``` I’ve tried several optimizations, such as preallocating arrays and reducing the number of times I call `hold on`, but nothing seems to make a noticeable difference in performance. For example, when plotting just 5 datasets, it takes about 0.5 seconds, but with 20 datasets, it spikes to over 5 seconds. Additionally, I've noticed that using `plot` inside the loop, while convenient, may not be efficient. I attempted to concatenate all the data and plot once, but that resulted in incorrect legend entries. Here's what I attempted: ```matlab function optimizedPlot(data) figure; hold on; allX = [data{:}.x]; allY = [data{:}.y]; plot(allX, allY); legend({data.name}); hold off; grid on; end ``` This approach threw an behavior regarding mismatched dimensions: "behavior using vertcat. Dimensions of arrays being concatenated are not consistent." I am unsure how to overcome this while still maintaining the clarity of the legend entries. Any suggestions on optimizing the performance of the plotting function or correctly concatenating the datasets would be greatly appreciated.