Selecting data based on string contents

조회 수: 3 (최근 30일)
Bob Thompson
Bob Thompson 2016년 7월 29일
댓글: Bob Thompson 2016년 8월 1일
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

채택된 답변

the cyclist
the cyclist 2016년 7월 29일
편집: the cyclist 2016년 7월 29일

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
Bob Thompson
Bob Thompson 2016년 7월 29일
편집: Bob Thompson 2016년 7월 29일
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 2016년 8월 1일
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

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by