function oformat = GUI_dialogimport(fname,vformat)
global datimport;
oformat=[];
%% Dialog Import
auxpos=get(gcf,'Position');
xsizedialog=auxpos(3)*0.4;
ysizedialog=auxpos(4)*0.8;
xposdialog=auxpos(1) + (auxpos(3)*0.6)/2;
yposdialog=auxpos(2) + (auxpos(4)*0.2)/2;
dialogimport=dialog('units','normalized','position',[xposdialog yposdialog xsizedialog ysizedialog],'numbertitle','off',...
    'resize','on','menubar','none','name',fname,'Visible','on');
strinstructions=sprintf('What is the format of %s and similar files? \n \n The files you are importing are ASCII-formatted, but the meaning of each column is not clear. We assume that all your files are formatted equally, so in the table below we show you the data recovered from %s file as an example. \n Please, indicate which columns correponds to Onset, Offset (or Duration) and Pitch. Please, remember that ONLY NUMERIC information is accepted, so please skip the first lines if they do not correspond to the numeric data information you want to import.',fname,fname);
uicontrol('Parent',dialogimport,'Style', 'text',...
    'String', strinstructions,...
    'Units','normalized','Position', [0.02 0.78 0.96 0.2],'HorizontalAlignment','left');
setappdata(dialogimport,'vformat',vformat);
% TODO cellSelect
%memo=textread(fname,'%s','delimiter','\n');
%textformat=uicontrol('Parent',dialogimport,'style','text','units','normalized',...
%    'position',[.02 .12 .45 .6],'hor','left','String',memo,'BackgroundColor',[1,1,1]);
%uicontrol('Parent',dialogimport,'Style', 'text',...
%           'String', 'FILENAME',...
%           'Units','normalized','Position', [0.02 0.72 0.45 0.03]);
datimport.M=readtextdata(fname,0);
datimport.tabledata = uitable('Parent',dialogimport,'Units','normalized','Position',...
    [0.01 0.12 0.98 0.4], 'Data', datimport.M,...
    'RowName',[],...
    'ColumnName', 'numbered',...
    'ColumnFormat', [],...
    'ColumnEditable', []);
panelformat=uipanel('Parent',dialogimport,'units','normalized','position',[0.01 0.55 0.98 0.22],'title','Select format');
%'String', 'onset(s)|onset(ms)|onset(100ns)|onset(samples at 44.1Khz)|onset(samples at 32Khz)|onset(samples at 22.5Khz)|onset(samples at 16Khz)|onset(samples at 11.25Khz)|onset(samples at 8Khz)|',...
onsetpopup=uicontrol('Parent',panelformat,'Style', 'popup',...
    'String', 'onset(s)',...
    'Units','normalized','Position', [0.01 0.6 0.3 0.1]);%,...
%'Callback', @setmap);
uicontrol('Parent',panelformat,'Style', 'text',...
    'String', 'corresponds to column number',...
    'Units','normalized','Position', [0.32 0.58 0.48 0.1]);
onsetcolumn=uicontrol('Parent',panelformat,'Style', 'popup',...
    'String', strrep(num2str(1:size(datimport.M,2)),'  ','|'),...
    'Units','normalized','Position', [0.8 0.6 0.15 0.1]);

offsetpopup=uicontrol('Parent',panelformat,'Style', 'popup',...
    'String', 'offset(s)|duration(s)',...
    'Units','normalized','Position', [0.01 0.35 0.3 0.1]);%,...
%'Callback', @setmap);
uicontrol('Parent',panelformat,'Style', 'text',...
    'String', 'corresponds to column number',...
    'Units','normalized','Position', [0.32 0.33 0.48 0.1]);
offsetcolumn=uicontrol('Parent',panelformat,'Style', 'popup',...
    'String', strrep(num2str(1:size(datimport.M,2)),'  ','|'),...
    'Units','normalized','Position', [0.8 0.35 0.15 0.1],'Value',2);

pitchpopup=uicontrol('Parent',panelformat,'Style', 'popup',...
    'String', 'pitch (MIDI number)',...
    'Units','normalized','Position', [0.01 0.13 0.3 0.1]);%,...
%'Callback', @setmap);
uicontrol('Parent',panelformat,'Style', 'text',...
    'String', 'corresponds to column number',...
    'Units','normalized','Position', [0.32 0.1 0.48 0.1]);
pitchcolumn=uicontrol('Parent',panelformat,'Style', 'popup',...
    'String', strrep(num2str(1:size(datimport.M,2)),'  ','|'),...
    'Units','normalized','Position', [0.8 0.13 0.15 0.1],'Value',3);

uicontrol('Parent',panelformat,'Style', 'text',...
    'String', 'Skip the ',...
    'Units','normalized','Position', [0.23 0.8 0.1 0.1],'HorizontalAlignment','left');
uicontrol('Parent',panelformat,'Style', 'text',...
    'String', 'first lines',...
    'Units','normalized','Position', [0.53 0.8 0.3 0.1],'HorizontalAlignment','left');
firstlinesedit=uicontrol('Parent',panelformat,'Style', 'edit',...
    'String', '0',...
    'Units','normalized','Position', [0.4 0.77 0.1 0.15],'Callback',{@changeSkiplines,fname});
uicontrol('Parent',dialogimport,'Style', 'pushbutton',...
    'String', 'Ok',...
    'Units','normalized','Position', [0.83 0.03 0.15 0.07],'Callback',@importCallback);
if ~isempty(vformat)
    set(onsetpopup,'Value',vformat(1));
    set(onsetcolumn,'Value',vformat(2));
    set(offsetpopup,'Value',vformat(3));
    set(offsetcolumn,'Value',vformat(4));
    set(pitchpopup,'Value',vformat(5));
    set(pitchcolumn,'Value',vformat(6));
    set(firstlinesedit,'String',num2str(vformat(7)));
end
if size(datimport.M,2)<20
    uiwait(dialogimport);
else
    errordlg('Too many columns. Format error');
    close(dialogimport);
end
if ishandle(dialogimport)
    vformat(1)=get(onsetpopup,'Value');
    vformat(2)=get(onsetcolumn,'Value');
    vformat(3)=get(offsetpopup,'Value');
    vformat(4)=get(offsetcolumn,'Value');
    vformat(5)=get(pitchpopup,'Value');
    vformat(6)=get(pitchcolumn,'Value');
    vformat(7)=str2num(get(firstlinesedit,'String'));
    oformat=vformat;
    close(dialogimport);
end
end

function importCallback(hObject,eventData)
%setappdata(get(hObject,'Parent'),'vformat',1234);
uiresume(get(hObject,'Parent'));
end
function changeSkiplines(hObject,eventData,fname)
global datimport;
datimport.M=readtextdata(fname,str2num(get(hObject,'String')));
set(datimport.tabledata,'Data',datimport.M);
end