Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

How can I plot multiple data sets from different files all on the same plot?

Asked by Sarah on 18 Jul 2013

I'm trying to write a script that does the following: Reads in data from a file _aoi_aware to see which aoi is in use (i.e. which data it needs to plot)

Then prompts the user to open a file based on the determined aoi

Then plots the data in the aoi file, which is in the form of coordinates.

The only problem with this is that the original file that contains which aoi is in use can contain multiple aoi's. I will need all of these plotted. So ultimately, I would like the user to be prompted to load the aoi_aware file, then be prompted to select data from aoi_1, if that is open, and then have the script plot that. But if aoi_2 appears later in the aoi_aware file,then that needs to be plotted as well. So I want the script to check for all aoi's in use and plot all of them on the same plot. Here's the code I have so far, and it keeps looping on aoi_4 and producing errors. I'm not sure how to correct the code I've written to achieve the desired result. All help is appreciated. let me know if I can clarify the question.

[fileName3 pathName3]= uigetfile('~/*_aoi_aware_out_Z.txt','Select the appropriate test file under *_aoi_aware_out_Z.txt');
filepath3=fullfile(pathName3,fileName3);
%% Read columns of data according to format string.
[TimeHdr CameraID_1 CameraID_2 CameraID_3 CameraId_4 CameraID_5 CameraID_6 CameraID_7 CameraID_8 Track_aoi Track_camera]  = textread(filepath3, repmat('%s ', 1,11),1);
[Timecell Cam1 Cam2 Cam3 Cam4 Cam5 Cam6 Cam7 Cam8 AOI Cam]=textread(filepath3, '%s %f %f %f %f %f %f %f %f %f %f', 'headerlines', 1);
UTtimeCharArray = char(Timecell);
years = str2num(UTtimeCharArray(:,1:4));
months = str2num(UTtimeCharArray(:,6:7));
days = str2num(UTtimeCharArray(:,9:10));
hours = str2num(UTtimeCharArray(:,12:13));
minutes = str2num(UTtimeCharArray(:,15:16));
seconds = str2num(UTtimeCharArray(:,18:21));
times = datenum(years,months,days,hours,minutes,seconds);
%%Check to see which AOI it's tracking on each min.
for i=1:length(times)
    if AOI(i)==1
          filepath_aoi1=fullfile(pathname_aoi_1,filename_aoi_1);
          [TimeHdr CameraID1 Col1 Row1 Quality1 Width1 RA1 Dec1 Error_est1]  = textread(filepath_aoi1, repmat('%s ', 1,9),1);
          [Timecell cam1 col1 row1 qual1 wid1 ra1 dec1 error1]=textread(filepath_aoi1, '%s %f %f %f %f %f %f %f %f ', 'headerlines', 1);
          %%Plot
          figure(1)
          plot1=plot(col1, row1, 'c');
          grid on
          legend(plot1, 'row','column','Location','Best');
          datetick('x','HH:MM:SS', 'keeplimits')
          title(['TA Gyroscopic Drift' UTtimeCharArray(1,12:19) ' --> ' UTtimeCharArray(end,12:19) ' UT '],'FontSize',10)
          %xlim([min(times), max(times)])
          xlabel('column');
          ylabel('row');
          hold on
      elseif AOI(i)==2
          [filename_aoi_2 pathname_aoi_2]= uigetfile('~/*_AOI_2_out_Z.txt','Select the appropriate test file under **_AOI_2_out_Z.txt');
          filepath_aoi2=fullfile(pathname_aoi_2,filename_aoi_2);
          [TimeHdr CameraID2 Col2 Row2 Quality2 Width2 RA2 Dec2 Error_est2]  = textread(filepath_aoi2, repmat('%s ', 1,9),1);
          [Timecell cam2 col2 row2 qual2 wid2 ra2 dec2 error2]=textread(filepath_aoi2, '%s %f %f %f %f %f %f %f %f ', 'headerlines', 1);
          %%Plot
          figure(1)
          plot1=plot(col2, row2,'k');
          grid on
          %legend(plot1, 'column','row','Location','Best');
          datetick('x','HH:MM:SS', 'keeplimits')
          title(['TA Gyroscopic Drift' UTtimeCharArray(1,12:19) ' --> ' UTtimeCharArray(end,12:19) ' UT '],'FontSize',10)
          %xlim([min(times), max(times)])
          xlabel('rows');
          ylabel('columns');
          hold on
      elseif AOI(i)==3
          [filename_aoi_3 pathname_aoi_3]= uigetfile('~/*_AOI_3_out_Z.txt','Select the appropriate test file under **_AOI_3_out_Z.txt');
          filepath_aoi3=fullfile(pathname_aoi_3,filename_aoi_3);
          [TimeHdr CameraID3 Col3 Row3 Quality3 Width3 RA3 Dec3 Error_est3]  = textread(filepath_aoi3, repmat('%s ', 1,9),1);
          [Timecell cam3 col3 row3 qual3 wid3 ra3 dec3 error3]=textread(filepath_aoi3, '%s %f %f %f %f %f %f %f %f ', 'headerlines', 1);
          %%Plot
          figure(1)
          plot1=plot(col3, row3,'g');
          grid on
          %legend(plot1, 'column','row','Location','Best');
          datetick('x','HH:MM:SS', 'keeplimits')
          title(['TA Gyroscopic Drift' UTtimeCharArray(1,12:19) ' --> ' UTtimeCharArray(end,12:19) ' UT '],'FontSize',10)
          %xlim([min(times), max(times)])
          xlabel('column');
          ylabel('row');
          hold on
      else AOI(i)==4
          [filename_aoi_4 pathname_aoi_4]= uigetfile('~/*_AOI_4_out_Z.txt','Select the appropriate test file under *_AOI_4_out_Z.txt');
          filepath_aoi4=fullfile(pathname_aoi_4,filename_aoi_4);
          [TimeHdr CameraID4 Col4 Row4 Quality4 Width4 RA4 Dec4 Error_est4]  = textread(filepath_aoi4, repmat('%s ', 1,9),1);
          [Timecell cam4 col4 row4 qual4 wid4 ra4 dec4 error4]=textread(filepath_aoi4, '%s %f %f %f %f %f %f %f %f ', 'headerlines', 1);
          %%Plot
          figure(1)
          plot1=plot(col4, row4, 'b');
          grid on
          %legend(plot1, 'row vs. col','Location','Best');
          datetick('x','HH:MM:SS', 'keeplimits')
          title(['TA Gyroscopic Drift' UTtimeCharArray(1,12:19) ' --> ' UTtimeCharArray(end,12:19) ' UT '],'FontSize',10)
          %xlim([min(times), max(times)])
          xlabel('column');
          ylabel('row');
          hold on       
      end 
      i=i+1;
  end

0 Comments

Sarah

Tags

Products

No products are associated with this question.

1 Answer

Answer by dpb on 19 Jul 2013
Accepted answer
...
for i=1:length(times)
  if AOI(i)==1
    ...
  elseif AOI(i)==2
    ...
  elseif AOI(i)==3
    ...
  else AOI(i)==4
    ...

The problem about continuing on case 4 is there's no 'if' on the 'else' so the AOI(i)==4 is meaningless--I'm somewhat surprised Matlab didn't flag it but didn't look at the code in the editor to see if it did give any warning.

If there are only four possible AOI values 1-4, then fixing that to also be elseif will stop that; if there are additional AOI values possible then unless you want them ignored you need to deal with them somewhere. Can't answer that as there's not info on that provided.

If there's a range of values after 3 that all have identical processing then it's likely the SWITCH construction might make writing the logic simpler than the nested IF

2 Comments

Sarah on 19 Jul 2013

Thank you. That did help in preventing the program to jump straight to AOI(i)==4. I have another question, that I'm hoping you (or someone else) can answer. Although this helped with getting the program to operate in the correct order, it's doing something else now. It continuously prompts the user to obtain the AOI_#_out_Z.txt file (where # is the current AOI). I would like it to just continuously plot the current AOI(i) and continue plotting until AOI(i) changes value, say changes from 3 to 4. Then when it changes value, the program should prompt the user to open up the AOI_4_out_Z.txt file and plot all those values on the same plot. If the AOI(i) value changes again, say back to 3, then it should plot on the same plot after prompting the user to open the AOI_3_out_Z.txt file and continue from the plot of when it was equal to 3 before. When it switches from 3 to 4, then the graph on 3 should go down to 0. I have tried inserting while loop to ask the program to continuously plot while AOi(i)==3, but it seems to get stuck in an infinite loop. I'm going to keep working on this, but would appreciate help. I'll include a segment of the code, which includes the inserted while loop.

 elseif AOI(i)==3
        uigetfile('~/*_AOI_3_out_Z.txt','Select the appropriate test file under **_AOI_3_out_Z.txt');
        filepath_aoi3=fullfile(pathname_aoi_3,filename_aoi_3);
        [TimeHdr CameraID3 Col3 Row3 Quality3 Width3 RA3 Dec3 Error_est3]  = textread(filepath_aoi3, repmat('%s ', 1,9),1);
        [Timecell cam3 col3 row3 qual3 wid3 ra3 dec3 error3]=textread(filepath_aoi3, '%s %f %f %f %f %f %f %f %f ', 'headerlines', 1);
        %%Plot
        while AOI(i)==3
        figure(1)
        plot1=plot(col3, row3,'g');
        grid on
        title(['Row vs. Column Plot' UTtimeCharArray(1,12:19) ' --> ' UTtimeCharArray(end,12:19) ' UT '],'FontSize',11)
        xlabel('column');
        ylabel('row');
        hold on
        end 
dpb on 19 Jul 2013
    ... 
elseif AOI(i)==3
  uigetfile('~/*_AOI_3_out_Z.txt', ...
  ...
  while AOI(i)==3
    ...
  end
  ...

Of course it's an infinite loop...you're already in a logic branch where AOI(i)==3 and there's nothing in the added loop that ever changes AOI(i) or even increments i to look at a different element of AOI inside this loop.

And, even if you did increment 'i' there, that's in all likelihood not what you would want to do; perhaps a second index to process all of that particular set would work but need more info than have on how the data are structured to be able to say unequivocally.

Looks to me like need to back away from the keyboard for a while and draft out a workflow on paper that processes the data as stored in the sequence that you want...then once you have a clear picture of what is needed to be done you'll have a far better chance of implementing it.

In that regard I'm confused about why the user has to be prombpted to reopen a set of files--isn't there a corresponding set of files that is to be processed from one group/test/whatever that can simply be referenced knowing a case number or base testname or somesuch? If not it would seem desirable to create such a convention or, at worst, have another file that contains the list of needed files for each case and that can simply be read for the needed files one that file is selected.

dpb

Contact us