I started with the manual code below to plot five lines of data:
% code
xAxis = 1:numel(plotLine1);myPlot = plot(xAxis,plotLine1(:)','k',... xAxis,plotLine2(:)','b',... xAxis,plotLine3(:)','c',... xAxis,plotLine4(:)','m',... xAxis,plotLine5(:)','g','LineWidth', 0.5)
But I want to select which data to plot based on a function output. The function populates the data into plotLine(n) and also returns a "logical" 1×5 cell array (1 for "yes", 0 for "no"), which is used to select, from plotData, which lines of data will be plotted. The logical array can contain any possible combination of the five data sets. Since each data has its own color defined, plotLine1 will always contains the data Ch01, plotLine2 the Ch06 and so on. Then I use plotData with the "logical" array to find out which data will be plotted.
% codeplotData(1,1) = {'xAxis,plotLine1(:),''k'''};plotData(1,2) = {'xAxis,plotLine2(:),''b'''};plotData(1,3) = {'xAxis,plotLine3(:),''c'''};plotData(1,4) = {'xAxis,plotLine4(:),''g'''};plotData(1,5) = {'xAxis,plotLine5(:),''m'''};ri = 1;for r=1:5 a=logicArray(1,r); bitTest = str2num(a{:}); switch bitTest case 1 plotContents(1,ri) = {plotData(1,r)}; ri = ri + 1; endend
plotData only holds the references to the data (plotLine = 18000×40 double, that is loaded with the necessary data in the function).
I have tried concatenating plotContents:
% code toPlot = ''; for r = 1:numel(plotContents) if r < numel(plotContents) toPlot = strcat(toPlot,plotContents{1,r},','); else toPlot = strcat(toPlot,plotContents{1,r}); end end finalPlot = toPlot{:}; myPlot = plot(finalPlot,'LineWidth', 0.5)
But it gives the error: "Error using plot Invalid first data argument.
Error in PlotViewer_function (line 234) myPlot = plot(finalPlot,'LineWidth', 0.5)"
I have also tried:
% code hold on cellfun(@(x) plot(x), plotContents);
Error code: "Error using plot Invalid first data argument
Error in PlotViewer_function>@(x)plot(x) (line 233) cellfun(@(x) plot(x), plotContents);"
I have also tried:
% code finalPlot = strcat(toPlot{:},',''LineWidth'', 0.5'); myPlot = plot(finalPlot)
where finalPlot is a 1×85 char containing (in one example): xAxis,plotLine1(:),'k',xAxis,plotLine3(:),'c',xAxis,plotLine5(:),'m','LineWidth', 0.5
The error it gives: "Error using plot Invalid first data argument.
Error in PlotViewer_function (line 236) myPlot = plot(finalPlot) "
How can I plot the selected data using plotLine(n) and the logical array?
Best Answer