Selecting data based on string contents

3 views (last 30 days)
I am attempting to create a series of plots based on a variable set of inputs. Each plot would include multiple sets of data but the user may not necessarily have the same number of sets as I have for testing.
The data is set up in a manner similar to:
[Time Plot1-1 Plot1-2 Plot2-1 Plot2-2 Plot2-3;
0 1 2 3 4 5;
1 2 3 4 5 6;
2 3 4 5 6 7]
Using this sample, I would like to plot all the columns against time, but in two separate plots, Plot1 and Plot2.
Is there some kind of code I could use to look for similarities in the headers and have those sets plotted on the same plot?
Thanks

Accepted Answer

the cyclist
the cyclist on 29 Jul 2016
Edited: the cyclist on 29 Jul 2016

The exact commands will depend on how the variables are stored, but here is one example of how you could do this:

header = {'Time' 'Plot1-1' 'Plot1-2' 'Plot2-1' 'Plot2-2' 'Plot2-3'};
data = ...
[0 1 2 3 4 5;
 1 2 3 4 5 6;
 2 3 4 5 6 7];
timeCol  = strncmp(header,'Time',4);
plot1Col = strncmp(header,'Plot1',5);
plot2Col = strncmp(header,'Plot2',5);
figure
hold on
plot(data(:,timeCol),data(:,plot1Col),'r.-', ...
     data(:,timeCol),data(:,plot2Col),'b.-')

Here's the output ...

  3 Comments
Bob Thompson
Bob Thompson on 29 Jul 2016
Edited: Bob Thompson on 29 Jul 2016
Ok, so after some further investigation and test work I am wondering if I am able to work this into an Excel plot. My end goal would be to print the data from the sample matrix into an Excel sheet, and then create an Excel chart as described that corresponds to the data in Excel.
I can definitely see the strncmp command allowing me to select which columns I would like to plot, however, all of my research into Excel charts with MATLAB suggest that I create each series individually with SeriesCollection.
Would I be able to pair the strncmp results with a for loop to get the data to be selected?
for chartnum = 1:2;
if chart == 1;
for set = 1:n;
Excel.ActiveSheet.SeriesCollection(set).XValue = timecol;
Excel.ActiveSheet.SeriesCollection(set).Value = plot1col;
% Include other plot if statement information after this
Also, I noticed that all of the Plot1 lines had the same formatting. Would it be possible to set the formatting for each curve individually? Or is the plotting process considering them all the same curve?
Bob Thompson
Bob Thompson on 1 Aug 2016
After some trial and error, I have discovered a way to fulfill my objective. By placing all of my different plot comparisons in a cell array, I am able to run a for loop for each of the different cells and create a chart for each plot sets. In order to plot each individual series, a second for loop was created for checking all possible values within the strncmp result, and an if statement was paired with the for loop to remove all negative comparisons.
plotcol = cell(2,1)
plotcol(1) = {strncmp(header,'Plot1',5)};
plotcol(2) = {strncmp(header,'Plot2',5)};
% For loop for each plot
for i = 1:2;
ChartObjects.Add
% For loop for each series
for ii = 1:5;
% If statement for positive results
if plotcol{i}(ii) == 1;
SeriesCollection.NewSeries
elseif plotcol{i}(ii) == 0;
end
end
end

Sign in to comment.

More Answers (0)

Categories

Find more on Characters and Strings in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!