Exporting multiple iterations of inputdlg user input into excel file
    8 visualizzazioni (ultimi 30 giorni)
  
       Mostra commenti meno recenti
    
I am going to include my whole code because I don't know where I went wrong. You can see on line 63 where I tried to use a for-loop to save multiple rows of user inputs as they are used, however the final exported excel file just has whichever the last input was. I have tried to use counters, but variable UserInput is considered a "double" and not a "cell" so I can't do that either. Let me know if you have any thoughts and thank you so much.
function user_input(hObject,varargin)
%This function runs a program which allows the user to input addresses into
%a spreadsheet which is saved in an excel file.
%The function uses input and question dialogue boxes, allows the user to
%correct mistakes through a GUI, and has a refresh button which allows the
%user to see the corrected information.
global f1 h4 UserInput h h2 h6 input_prompt dlg_title num_lines default_ans Answer h3 h5 %make these variables universal (else errors occur)
input_prompt={'First name:','Last name:', 'Address:','City:','State:','Zipcode:'}; % a list of input prompts which will be used
dlg_title = 'User Input'; %The title for the dialogue box
num_lines = 1; %The number of lines required for input
default_ans= {'Jane','Doe','100 Example St.','Exampletown','NJ','11567'}; %default answers for the dialogue box
options.Resize='on'; %allows the user to resize the window if necessary
UserInput=inputdlg(input_prompt,dlg_title,num_lines,default_ans,options); %initiates dialogue box and saves the answers in a column
UserInput= UserInput'; %changes the columns to rows
if nargin == 0  %if no argument in
    f1=figure(1); %set figure
    set(f1,'units','normalized','position',[.05 .3 .4 .5],...
        'menubar','none'); %sets position of GUI window
    h=uicontrol('units','normalized','style','text','string',UserInput,'fontsize',12,...
        'HorizontalAlignment','left','position',[.5 .2 .3 .5]); %presents the user input as text, adjusted left
    h2=uicontrol('units','normalized','style','text','string',input_prompt,'fontsize',12,...
        'fontweight','bold','HorizontalAlignment','right','position',[.1 .2 .3 .5]); %presents the categories as text, adjusted right
    h6=uicontrol('units','normalized','style','pushbutton','string','Refresh',...
        'position',[.75 .1 .2 .1],'callback',@user_input_refreshfctn); %a pushbutton that will refresh the screen
      pause (2); %pause so the user can review the inputs for errors
      Answer=questdlg('Is this the correct information?','Yes','No'); %present a question dialogue box
      switch Answer %switch function to cases for this question
          case 'Yes' %if yes
              disp('Your information will be saved.') %matlab command window will say this            
              return_new= questdlg('Would you like to do another entry?','Return',...
                  'Yes','No','Yes'); %Asks if user would like to do another entry (second No is the 'default' answer)
              switch return_new %switch to this question
                  case 'Yes' %if yes
                      user_input %rerun the whole function
                  case 'No' %if no new entry
                      close all %close all open windows
                      xlswrite('addressbook.xls',UserInput); %save the input to excel
              end
          case 'No' %if no this is not the correct information
              if nargin == 0 %if no argument in set up the figure (same as before)
                  f2=figure(2);
                  set(f2,'units','normalized','position',[.3 .3 .4 .5],... %create a new figure so user can specify which answer was incorrect
                      'menubar','none');
                  h3=uicontrol('style','text','string','What field would you like to edit?',...
                      'FontSize',12,'units','normalized','position',[.2 .6 .5 .2]); %text asks what field the user would like to edit
                  h4=uicontrol('style','popup','string','Choose One:|First name|Last name|Address|City|State|Zipcode',...
                      'FontSize',12,'units','normalized','position',[.2 .2 .6 .3],'callback',@user_input_editfctn); %makes a popup menu which allow the user to edit the function
                  h5=uicontrol('style','pushbutton','string','Done','units','normalized','position',[.75 .1 .2 .1],'callback',@user_input_extfctn); %creates a "done" button, to exit
              end
              for a=1:length(varargin) % <---- FOR LOOP NOT WORKING
              UserInput(a,:)=UserInput; %save as many lines as are put in by user into UserInput Cell
              xlswrite('addressbook.xlsx',UserInput); %saves after done
              end
      end
end
      % The edit function replaces the field with the incorrect value, as
      % specified by the user
      function user_input_editfctn(varargin)
      g=(get(h4,'value')-1); %get the field that is wrong
      if g==1
          wrong1=inputdlg('First name:','Correct first name',1); %show a new input dialogue box
          UserInput=[wrong1, UserInput(2:6)]; %will replace field user said was wrong and redisplay the information
          %below --- all the same code as above- different options
      elseif g==2
          wrong2=inputdlg('Last name:','Correct last name',1);
          UserInput=[UserInput(1),wrong2,UserInput(3:6)];
      elseif g==3
          wrong3=inputdlg('Address:','Correct address',1);
          UserInput=[UserInput(1:2),wrong3,UserInput(4:6)];
      elseif g==4
          wrong4=inputdlg('City:','Correct city',1);
          UserInput=[UserInput(1:3),wrong4,UserInput(5:6)];
      elseif g==5
          wrong5=inputdlg('State:','Correct state',1);
          UserInput=[UserInput(1:2),wrong5,UserInput(4:6)];
      elseif g==6
          wrong6=inputdlg('Zipcode:','Correct zipcode',1);
          UserInput=[UserInput(1:5),wrong6];
      end
      end
  end
% Exit function, so that user can say when they are done putting in the
% information
function user_input_extfctn(varargin)
global input_prompt dlg_title num_lines default_ans options %add global, otherwise they have error message
if nargin == 0 %if no argument in, define the following variables again (cannot re-read variables from the other function)
    input_prompt={'First name:','Last name:', 'Address:','City:','State:','Zipcode:'};
    dlg_title = 'User Input';
    num_lines = 1;
    default_ans= {'Jane','Doe','100 Example St.','Exampletown','NJ','11567'};
    options.Resize='on';
else %otherwise
    return_new= questdlg('Would you like to do another entry?','Return',...
        'Yes','No','Yes'); %asks user if they would like to do another entry
    switch return_new
        case 'Yes' %if yes, open a new figure
            user_input %run user input function again
        case 'No' %if no
            close all %close all the windows
            xlswrite('addressbook.xls',UserInput);  %save the info
    end
end
end
% REFRESH FUNCTION... allows user to see that their information has been
% corrected before moving forward
function  user_input_refreshfctn(varargin)
global UserInput input_prompt h h2 h6
if nargin == 0 %if no argument in
      UserInput=inputdlg(input_prompt,dlg_title,num_lines,default_ans,options); %defines how to input user data
      UserInput= UserInput'; %inverts columns/roles
      input_prompt={'First name:','Last name:', 'Address:','City:','State:','Zipcode:'}; %Input prompt info to show on screen
else %otherwise
    close figure 1 %close the figure with the old information
      f1=figure(1); %set figure
      set(f1,'units','normalized','position',[.05 .3 .4 .5],...
          'menubar','none'); %sets position of new figure
      h=uicontrol('units','normalized','style','text','string',UserInput,'fontsize',12,...
          'HorizontalAlignment','left','position',[.5 .2 .3 .5]); %adds text info based on user input, adjusts left
      h2=uicontrol('units','normalized','style','text','string',input_prompt,'fontsize',12,...
          'fontweight','bold','HorizontalAlignment','right','position',[.1 .2 .3 .5]); %adds text info for categories, adjust right
      h6=uicontrol('units','normalized','style','pushbutton','string','Refresh',...
          'position',[.75 .1 .2 .1],'callback',@user_input_refreshfctn); %adds refresh button, so you can see additional changes made
end
end
      % code
    end
0 Commenti
Risposte (1)
  Meade
      
 il 21 Apr 2016
        You're really close! Try this as an example:
N = 3;
loopData = cell(N,1);
for ii = 1:N    
    userDlg = inputdlg('Hit any key',sprintf('Dialogue (loop %i/%i)',ii,numel(N)),[1,50]);
    loopData{ii,1} = userDlg{:};    
end
xlswrite('NewData.xlsx',loopData)
0 Commenti
Vedere anche
Categorie
				Scopri di più su Interactive Control and Callbacks in Help Center e File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
