2

I want to retrieve bibtex data (for building a bibliography) by sending a DOI (Digital Object Identifier) to http://www.crossref.org from within matlab.

The crossref API suggests something like this:

curl -LH "Accept: text/bibliography; style=bibtex" http://dx.doi.org/10.1038/nrd842

based on this source.

Another example from here suggests the following in ruby:

open("http://dx.doi.org/10.1038/nrd842","Accept" => "text/bibliography; style=bibtex"){|f| f.each {|line| print line}}

Although I've heard ruby rocks I want to do this in matlab and have no clue how to translate the ruby message or interpret the crossref command.

The following is what I have so far to send a doi to crossref and retrieve data in xml (in variable retdat), but not bibtex, format:

clear
clc

doi = '10.1038/nrd842';

URL_PATTERN = 'http://dx.doi.org/%s';
fetchurl = sprintf(URL_PATTERN,doi);

numinputs = 1;

www = java.net.URL(fetchurl);

is = www.openStream;

%Read stream of data
isr = java.io.InputStreamReader(is);
br = java.io.BufferedReader(isr);

%Parse return data
retdat = [];
next_line = toCharArray(br.readLine)';  %First line contains headings, determine length

%Loop through data

while ischar(next_line)
  retdat = [retdat, 13, next_line];
  tmp = br.readLine;
  try
    next_line = toCharArray(tmp)';
    if strcmp(next_line,'M END')
      next_line = [];
      break
    end
  catch
    break;
  end
end


%Cleanup java objects
br.close; 
isr.close;
is.close;

Help translating the ruby statement to something matlab can send using a script such as that posted to establish the communication with crossref would be greatly appreciated.

Edit:

Additional constraints include backward compatibility of the code (back at least to R14) :>(. Also, no use of ruby, since that solves the problem but is not a "matlab" solution, see here for how to invoke ruby from matlab via system('ruby script.rb').

Community
  • 1
  • 1
Buck Thorn
  • 5,024
  • 2
  • 17
  • 27
  • Why the raw Java instead of [`urlread`](http://www.mathworks.com/help/matlab/ref/urlread.html)? – horchler Jul 29 '13 at 19:44
  • That's historical: the code was borrowed from another app. – Buck Thorn Jul 29 '13 at 19:46
  • For setting the request header and other details, [see this](http://undocumentedmatlab.com/blog/expanding-urlreads-capabilities/). – horchler Jul 29 '13 at 19:48
  • @horchler: thanks for the link, it would lead to a solution on a recent version of matlab but `urlread2` is not happy in R14. I am more likely to have success with octave at this point. – Buck Thorn Jul 30 '13 at 07:01
  • This appears to be a java programming question at this point. It might be possible to port some of the code from a java program which achieves what I want to do. – Buck Thorn Jul 30 '13 at 13:54
  • Ouch, R14 is quite old. If you're using that, you should indicate that you're using a 2004 version of Matlab in any future StackOverflow questions. It will save people a lot of trouble. [Many things that we now take for granted won't work](http://www.dynare.org/DynareWiki/MatlabVersionsCompatibility). – horchler Jul 30 '13 at 15:06
  • @horchler yeah, I feel like I am missing out. I have at times found myself translating bsxfun statements, no fun. However age is relative and R14 is still great. I should have included a comment sooner regarding this impairment however. – Buck Thorn Jul 30 '13 at 15:34
  • Have you seen [bsxfun substitute](http://www.mathworks.com/matlabcentral/fileexchange/23005-bsxfun-substitute)? – horchler Jul 30 '13 at 15:42
  • @horchler Thanks, that is a great workaround! – Buck Thorn Jul 30 '13 at 18:28

2 Answers2

1

You can easily edit urlread for what you need. I won't post my modified urlread function code due to copyright.

In urlread, (mine is at C:\Program Files\MATLAB\R2012a\toolbox\matlab\iofun\urlread.m), as the least elegant solution:

Right before "% Read the data from the connection." I added:

urlConnection.setRequestProperty('Accept','text/bibliography; style=bibtex');
0

The answer from user2034006 lays the path to a solution. The following script works when urlread is modified:

URL_PATTERN = 'http://dx.doi.org/%s';
doi = '10.1038/nrd842';
fetchurl = sprintf(URL_PATTERN,doi); 
method = 'post';
params= {};
[string,status] = urlread(fetchurl,method,params);

The modification in urlread is not identical to the suggestion of user2034006. Things worked when the line

urlConnection.setRequestProperty('Content-Type','application/x-www-form-urlencoded');

in urlread was replaced with

urlConnection.setRequestProperty('Accept','text/bibliography; style=bibtex');
Buck Thorn
  • 5,024
  • 2
  • 17
  • 27