生成一致的datenums数组

时间:2015-11-29 01:29:15

标签: matlab

每当我需要绘制数据与时间的关系时,我会生成相应的 class TextTab extends JPanel { String[] textOptions = new String[]{"1 line of text","2 lines of text","3 lines of text","Chest Name","Script with Tail (1 Color)","Script with Tail (2 Color)"}; String[] numberOptions = new String[]{"1\"","2\"","3\"","4\"","6\"","8\"","10\""}; CustomJList<String> textList = new CustomJList<String>(textOptions); CustomJList<String> numberList = new CustomJList<String>(numberOptions); GridBagLayout ttGlay = new GridBagLayout(); GridBagConstraints gbc = new GridBagConstraints(); NimbusCellRenderer cr = new NimbusCellRenderer(); JPanel pnl = new JPanel(new GridBagLayout()); JScrollPane textNumOptionsPane = new JScrollPane(pnl); public TextTab() { ttGlay.rowHeights = new int[]{0,137}; textList.setBorder(new TitledBorder(new SoftBevelBorder(SoftBevelBorder.LOWERED),"Standard Text")); numberList.setBorder(new TitledBorder(new SoftBevelBorder(SoftBevelBorder.LOWERED),"Pre-Cut Numbers")); textList.setOpaque(false); textList.setCellRenderer(cr); numberList.setOpaque(false); numberList.setCellRenderer(cr); textList.setBackground(Color.WHITE); numberList.setBackground(Color.WHITE); textList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); numberList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); setLayout(ttGlay); gbc.weightx = 1; gbc.weighty = 1; gbc.gridx = 0; gbc.gridy = 0; gbc.anchor = gbc.NORTH; gbc.fill = gbc.BOTH; gbc.gridwidth = 1; gbc.gridheight = 1; gbc.insets = new Insets(10,10,0,0); add(textList,gbc); gbc.gridx = 1; gbc.gridy = 0; gbc.anchor = gbc.NORTH; gbc.fill = gbc.BOTH; gbc.gridwidth = 1; gbc.gridheight = 1; gbc.insets = new Insets(10,0,0,10); add(numberList,gbc); gbc.gridx = 0; gbc.gridy = 1; gbc.anchor = gbc.NORTH; gbc.fill = gbc.BOTH; gbc.gridwidth = 2; gbc.gridheight = 1; gbc.insets = new Insets(0,10,10,10); add(textNumOptionsPane,gbc); } class CustomJList<E> extends JList<E> { public CustomJList(){super();} public CustomJList(E[] listData){super(listData);} public CustomJList(ListModel<E> dataModel){super(dataModel);} public CustomJList(Vector<? extends E> listData){super(listData);} @Override public void paint(Graphics g) { Graphics2D g2 = (Graphics2D)g.create(); g2.setColor(getBackground()); if(!isOpaque())g2.fillRect(getInsets().left-4,getInsets().top-4,getWidth()-getInsets().left-getInsets().right+8,getHeight()-getInsets().top-getInsets().bottom+8); super.paint(g); } } class NimbusCellRenderer extends JLabel implements ListCellRenderer<Object> { public NimbusCellRenderer() { setOpaque(true); } public Component getListCellRendererComponent(JList<?> list,Object value,int index,boolean isSelected,boolean cellHasFocus) { setBorder(new EmptyBorder(0,5,0,0)); setText(value.toString()); Color background; Color foreground; if (isSelected&&list.hasFocus()) { background = new Color(57,105,138); foreground = Color.WHITE; } else { background = Color.WHITE; foreground = Color.BLACK; } if(list.hasFocus()&&isSelected) { setBorder(new CompoundBorder(new CompoundBorder(new LineBorder(new Color(115,164,209),1),new LineBorder(new Color(72,120,155),1)),new EmptyBorder(0,3,0,0))); } setBackground(background); setForeground(foreground); return this; } } } 数组,这样我就可以通过调用datenums来显示plot上的时间轴。

假设我需要所有的datenums,其间隔时间为datetickh_1之间的1分钟。这是我如何生成我的datenums数组h_2

vd

我学习了这种阅读this answer的技术。

另一方面,每当我想生成一个datenum时,我就会使用h_1 = [01 00]; % 01:00 AM h_2 = [17 00]; % 17:00 PM HH_1 = h_1(1); % Hour digits of h_1 MM_1 = h_1(2); % Minute digits of h_1 HH_2 = h_2(1); % Hour digits of h_2 MM_2 = h_2(2); % Minute digits of h_2 % Vector of 01:00 - 17:30 with 1 minute increments (24 hours a day, 1440 minutes a day) vd = HH_1/24+MM_1/1440:1/1440:HH_2/24+MM_2/1440; 函数:

datenum

由于14:20在01:00到17:30之间,因此它的datenum也应该是!遗憾的是,这不符合预期,因为分配给d = datenum('14:20','HH:MM'); 的号码与d中包含的值完全不同。我想我可能会遗漏一些东西,可能与设置参考日期或类似事件有关。

那么一致生成日期的合适方法是什么?

1 个答案:

答案 0 :(得分:1)

原因是datenum函数为您提供从0000年1月0日起的天数。 所以在打电话时

d = datenum('14:20','HH:MM');

你得到一个大约735965的数字,而你的vd数组中的数字介于0和1之间。为了确定1月0日0000和今天之间的日期你可以写

d = datenum('14:20','HH:MM') - datenum('00:00','HH:MM');

然后你的代码看起来像

h_1 = [01 00]; % 01:00 AM
h_2 = [17 00]; % 17:00 PM

HH_1 = h_1(1); % Hour digits of h_1
MM_1 = h_1(2); % Minute digits of h_1

HH_2 = h_2(1); % Hour digits of h_2
MM_2 = h_2(2); % Minute digits of h_2

vd = HH_1/24 + MM_1/1440   :  1/1440  :  HH_2/24+MM_2/1440;

d = datenum('14:20','HH:MM') - datenum('00:00','HH:MM');

display(d);
display(vd(801));

结果:

d = 0.5972
ans = 0.5972