Search code examples
rubymatlabbibtexdoi

Retrieve bibtex data from crossref by sending DOI from matlab: translation from ruby


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').


Solution

  • 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');