views:

545

answers:

4

Does anyone know of a method to display a popup date selection calendar in a MATLAB gui? I know the financial toolbox has a uicalendar function, but unfortunately I don't have that toolbox.

I have a hunch I'm going to have to use some Java or some other language for this one, which I know nothing about.

I'm looking for something similar to this: alt text

which would return a date string after the user selects the date.

+4  A: 

I'd start with the calendar() function which outputs a matrix containing the calendar for any month. I assume you could combine this with a user-clickable interface to retrieve a specific date?

The following code is really ugly, but could help you get started...

 WINDOW_WIDTH = 300;
 WINDOW_HEIGHT = 200;
f= figure('Position',[300 300 WINDOW_WIDTH WINDOW_HEIGHT]);

 NB_ROWS = 6;
 NB_COLS = 7;
 width = round(WINDOW_WIDTH/NB_COLS);
 height = round(WINDOW_HEIGHT/NB_ROWS);
 buttons = nan(NB_ROWS,NB_COLS);
 dates = calendar();

 for row = 1:NB_ROWS
    for col = 1:NB_COLS
       if dates(row,col) == 0
          mydate = '';
       else
          mydate = sprintf('%i', dates(row,col));
       end
       buttons(row,col) = uicontrol('Style', 'PushButton', ...
          'String', mydate, ...
          'Position', [(col-1)*width (NB_ROWS - row)*height width height]);
    end
 end
Kena
You can insert `dates(all(dates==0,2),:)=[];` after calendar() call to eliminate the row with all zeros.
yuk
And `[NB_ROWS,NB_COLS] = size(dates);`
yuk
Good points. I'm a bit busy today, but feel free to edit accordingly :)
Kena
@Kena Your solution is much prettier than using uitable, but I think uitable will be much less messy to deal with.
Doresoom
+3  A: 

I don't have much time for a more complete answer, unfortunately, but I'd try uitable to create a table and to define the CellSelectionCallback to get the date.

Here's a bit to get you started:

dates = calendar;
dates(~any(dates,2),:) = [];
fh = figure;
uh = uitable('parent',fh,'data',dates,'ColumnWidth',repmat({20},1,7),...
             'ColumnName',{'S','M','T','W','T','F','S'});
Jonas
+3  A: 

Here are two approaches that would give you a professional-looking calendar component in Matlab without too much programming work:

  1. Use a Java calendar component (for example, one of these or these). Once you download the relevant Java class or Jar-file, add it to your static Java classpath (use the edit('classpath.txt') command from the Matlab Command Prompt). Finally, use the built-in javacomponent function to place the component in your Matlab figure window.

  2. If you are using a Windows OS, you can embed any Active-X calendar control that is available. Use the built-in actxcontrolselect function to choose your favorite calendar control (for example, Microsoft Office's "Calendar Control 11.0" - MSCAL.Calendar.7 - which is automatically installed with Office 2003; or "Microsoft Date and Time Picker Control 6.0" - MSComCtl2.DTPicker.2, or ...). Then use the actxcontrol function to place the component in your Matlab figure window.

  3. Matlab has some pretty useful built-in calendar (date-selection) controls - I posted an article about them today

Yair Altman
@Yair Altman : Javacomponent seems to be undocumented. Being completely unfamiliar with using java in MATLAB, could explain the input parameters?
Doresoom
+2  A: 

The uigetdate function in the Mathworks File Exchange is also a nice solution:

http://www.mathworks.com/matlabcentral/fileexchange/8313-uigetdate

Fayssal El Moufatich