MATLAB中如何无循环绘制不同长度的元胞数组?
Hey there! I totally get the frustration of slow loops when plotting thousands of curves—MATLAB's loop overhead can really add up. Luckily, there's a clean, vectorized way to achieve exactly what you want, no loops required.
The Solution: Use Cell Array Expansion with {:}
Instead of looping through each cell pair, you can leverage MATLAB's ability to expand cell arrays into individual input arguments for the plot function. Here's the one-liner that replaces your entire loop:
plot(MAT_ArrayT{:}, MAT_ArrayY{:})
How It Works
When you use the {:} syntax on a cell array like MAT_ArrayT, MATLAB expands it into a comma-separated list of its contents. So for your 6×1 cell arrays, this is equivalent to calling:
plot(MAT_ArrayT{1}, MAT_ArrayY{1}, MAT_ArrayT{2}, MAT_ArrayY{2}, ..., MAT_ArrayT{6}, MAT_ArrayY{6})
This does exactly what your loop does, but it's handled entirely by MATLAB's optimized internal functions—no loop overhead means way faster execution, especially when scaling to thousands of curves.
Bonus: Customizing Curve Styles
If you want to apply consistent styling to all curves (like a solid blue line), just add the style specifier at the end:
plot(MAT_ArrayT{:}, MAT_ArrayY{:}, '-b')
If you need unique styles for each curve, you can predefine a list of styles and expand it similarly, but for most cases where you just need to plot all curves efficiently, the basic one-liner is perfect.
内容的提问来源于stack exchange,提问作者Ben

