1

I have written a script in MATLAB, where I am retrieving rows and columns from a table based on the WHERE clause. So far i manage to retrieve the data from the database table.

The problem is that i would like to allow the user to have the option of running another search to retrieve another set of data.

This is my code so far that i have and the script is called 'searchpdb'.

pdbSearch = input('Enter your PDB Code: ', 's')
curs = fetch(exec(conn, ['SELECT * FROM cath_2_wo_dup WHERE pdbcode = ' '''' pdbSearch '''']));
fprintf('Results Successful! \n');
results = curs.Data % prints the data out

% ----------------------
% User option to search again
% -----------------------

goAgain = input('Would you like to search for another pdb?', 's');
% if (goAgain = 'Yes')
if strcmp(goAgain, 'Yes')
    searchpdb(); %runs this script again.
elseif strcmp(goAgain, 'No')
    fprintf('\nBye!\n');

end

I have tried using 'questdlg', but it does not show the results of the data in the table after i have given the option to the user to run again.

I am doing this the wrong way, or is there another efficient way of doing it? Should the option of running the script again be in another script ?

Jeiman
  • 1,121
  • 9
  • 27
  • 50

2 Answers2

3

Well, it will work, but I would recommend using a while clause instead of calling the script recursively:

goAgain = true;
while goAgain
    pdbSearch = input('Enter your PDB Code: ', 's');
    curs = fetch(exec(conn, ['SELECT * FROM cath_2_wo_dup WHERE pdbcode = ' '''' pdbSearch '''']));
    fprintf('Results Successful! \n');
    results = curs.Data % prints the data out

    % ----------------------
    % User option to search again
    % -----------------------

    res = input('Would you like to search for another pdb?', 's');
    goAgain = isequal(upper(res),'YES');
end

It just becomes clearer for the reader of your code. Just by taking a look at the first lines of this new code, one can guess that:

  1. There is a loop - something happens multiple times.
  2. There is a stop condition called goAgain.
Andrey Rubshtein
  • 20,795
  • 11
  • 69
  • 104
  • Thank you for the tip on using a `while` loop. However, i would like to allow the user to type in either `'Yes' or 'No'` to search again. So far, it's just looping back again. Is there a way i can include letters to be called out so that if the user types in `'Yes'`, it allows them to search again, if is a `'No'`, it cancels the script from running. – Jeiman Feb 21 '12 at 22:32
  • 1
    @jeiman90, it looks like it is working. (At least on my computer). Can you explain what goes wrong? – Andrey Rubshtein Feb 21 '12 at 22:51
  • When i inititally type in my first pdbcode code to search in the Database, it returns the table(which is good), but for when it asks me whether i would like to run it again and I type in 'no', it still runs the while loop, asking me to type in another pdb code (when i don't need to). – Jeiman Feb 21 '12 at 23:10
  • Also, another query, How do i allow the data that has been retrieved first to be displayed in the workspace, before giving the option to the user of searching again? – Jeiman Feb 21 '12 at 23:33
  • It works. Thank you. Back to my other question that i asked. How do i allow the data that has been retrieved first to be displayed in the workspace, before giving the option to the user of searching again? Although it shows the results in the command window, i would like to see my results that i have queried in before searching again. It doesn't put the variables in the workspace after the first query has been made. Do you get what i mean by this ? – Jeiman Feb 22 '12 at 00:32
  • @jeiman90, I think you should ask another question on this one. – Andrey Rubshtein Feb 22 '12 at 13:11
1

What is the problem, exactly? The following test script (called scriptrecurse.m) works as expected (note that you don't need the parentheses () to call a script).

X = randn(3);
disp('X = ')
disp(X)

x = input('Go again? ','s');

if strcmpi(x,'y')
    scriptrecurse
else
    fprintf('Bye!\n')
end

For example:

>> scriptrecurse
X = 
    1.1808    0.4716   -1.4529
    0.1729    2.0474   -0.6343
    0.1747   -0.6437   -1.1136

Go again? y
X = 
   -0.8910   -0.2479    0.0851
    1.7106    2.0659    0.6639
   -0.5174   -0.4350    0.0301

Go again? n
Bye!
Chris Taylor
  • 46,912
  • 15
  • 110
  • 154