Is there an easy way to plot many different arrays of varying sizes without manually specifying each one

2 views (last 30 days)
I have a save file that contains 2 cell arrays that have 100 elements each. I MUST use these 2 cell arrays, there is no way around it. lets call them T and R.
length(T{:,m}) = length(R{:,m}) ~= length(T(:,n~=m))~=length(R{:,n~=m)).
I want to make a function where I can input any number of m-values and it will automatically plot each T{:,m} and R{:,m} into one graph without manually having to put if/then statements with plotting commands like:
plot(T{:,m1},R{:,m1},T{:,m2},R{:,m2})
to account for every possible number of m-values inputted.
I know of cellfun(@plot,A), but I do not know if it is capable of plotting arrays from two separate cells against each other.

Answers (1)

Stephen23
Stephen23 on 6 Aug 2016
Edited: Stephen23 on 6 Aug 2016
To create one plot with multiple lines, you should join all of the data into one array, padded with NaN's to make them all of the same length (this may be easiest to do in a loop, or you could use Jos's excellent FEX submission padcat). Then plot will simply plot each column as one line, and it ignore the NaN's. Here is a simple example:
>> X = 0:0.1:2; % x values
>> M = NaN(numel(X),4); % preallocate array
>> M(1:5,1) = X(1:5).^2; % 1st column
>> M(:,2) = sin(X); % 2nd column
>> M(4:9,3) = sqrt(X(4:9)); % 3rd column
>> M(1:8,4) = X(1:8)-1; % 4th column
>> plot(X(:),M)
plots this:

Products

Community Treasure Hunt

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

Start Hunting!