Issue with if else elseif and while
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
frankenberry
el 24 de Abr. de 2020
Comentada: Geoff Hayes
el 25 de Abr. de 2020
My appdesigner code begins by writing the first set of answers to file. The first set of answers start on tab press (i.e., on tab press = instruction and question loads; client selects answers, client presses submit). The first set of answers are written when the SubmitAnswers button is pressed. The 'if loop' then presents the remaining questions and writes these to file. I'm having an issue with the "if statement". When I use <=, the app presents all the remaining questions but I get a "row index exceeds table dimensions' error which makes sense because the row index would be 9. However, the app should not process this and should instead go to the elseif statement but the app won't go to the "elseif statement" and process the questions from the next sheet. I've also tried < but then the app only does up to question 7 and misses the last question. I tried "WriteMode Append" but this command appends all answers and I only want the client's answers for one set of questionnaires in the app. If anyone can be of assistance, I would appreciate it. Someone told me that if I read the table in and write it out again, the rows would append. This also is not working in my code below.
% Button pushed function: SubmitAnswersButton
function SubmitAnswersButtonPushed(app, event)
readtable("Answers.xlsx","ReadVariableNames",false,"Sheet","NEQA");
writetable(app.Answers, 'Answers.xlsx', 'Sheet', "NEQA"); %, 'WriteMode', 'Append'); % write the values to the table Answers.xlsx as a backup - should be all questions from all sheets to one page
writetable(app.Answers, app.filenm, 'Sheet', "NEQA"); %, 'WriteMode', 'Append'); % write the answers to the client's excel file;
% collects answers when "Submit" button pushed and writes question and answers to file
if app.NEQCurrentCount < app.NumberOfNEQQuestions
app.NEQCurrentCount = app.NEQCurrentCount + 1; % NEQ question counter
app.NEQsheet_counter = 1; % first sheet in Questionnaires_NEQ.xlsx
app.Answers = table;
%load next question
app.NEQColumns = 3:2:width(app.NEQQuestions);
app.NEQInstructions = readtable('Questionnaires_NEQ.xlsx',"ReadVariableNames",false,"TextType","string","Range","A1"); %height will not work unless readtable comes first; % Succeeds because the sheet counter is a scalar
app.NumberOfNEQInstructions = height(app.NEQInstructions); % total number of instructions; just in case future projects require more rows for instructions;
app.NEQInstructionsTextArea.Value = app.NEQInstructions{1,1}; % writes the instruction in the front panel box from the row count and column 1
app.NEQQuestions = readtable('Questionnaires_NEQ.xlsx',"ReadVariableNames",false,"TextType","string","Sheet", app.NEQsheet_counter); % Succeeds because the sheet counter is a scalar
app.NEQQuestionsTextArea.Value = app.NEQQuestions{app.NEQCurrentCount,2}; % ROW INDEX ERROR OCCURS HERE
selectedButton1.Text = app.ButtonGroup_NEQF.SelectedObject.Text;
selectedButton2.Text = app.ButtonGroup_NEQT.SelectedObject.Text;
selectedButton3.Text = app.ButtonGroup_NEQE.SelectedObject.Text;
newRow = {app.NEQQuestionsTextArea.Value{1}, [selectedButton1.Text], [selectedButton2.Text], [selectedButton3.Text]};
app.Answers = [app.Answers; newRow]; % inserts new row in answer 'file'
readtable("Answers.xlsx","ReadVariableNames",false,"Sheet","NEQA")
writetable(app.Answers, 'Answers.xlsx', 'Sheet', "NEQA");%, 'WriteMode', 'Append'); % write the values to the table Answers.xlsx as a backup - should be all questions from all sheets to one page
writetable(app.Answers, app.filenm, 'Sheet', "NEQA"); % 'WriteMode', 'Append'); % write the answers to the client's excel file; writes the question in the front panel box from the row count and column 2
app.DummyButton1.Value = 1;
app.DummyButton2.Value = 1;
app.DummyButton3.Value = 1;
% Presents Questions for Sheet 2
elseif (app.NEQCurrentCount > app.NumberOfNEQQuestions) % if the question counter is less than the number of questions
app.NEQsheet_counter = app.NEQsheet_counter + 1;
more code
end
end
0 comentarios
Respuesta aceptada
Geoff Hayes
el 24 de Abr. de 2020
frakenberry - the problem might be the first line in the body of the if statement
if app.NEQCurrentCount < app.NumberOfNEQQuestions
app.NEQCurrentCount = app.NEQCurrentCount + 1; % NEQ question counter
where app.NEQCurrentCount is immediately incremented by one. Can this be the last statement in this body?
if app.NEQCurrentCount <= app.NumberOfNEQQuestions
app.NEQsheet_counter = 1; % first sheet in Questionnaires_NEQ.xlsx
app.Answers = table;
%etc.
app.NEQCurrentCount = app.NEQCurrentCount + 1; % NEQ question counter
elseif (app.NEQCurrentCount > app.NumberOfNEQQuestions)
app.NEQsheet_counter = app.NEQsheet_counter + 1;
% etc.
end
I guess it all depends upon what NEQCurrentCount has been initialized to - zero or one? And do we ever need to reset this counter (like when we change sheets)?
I'm also wondering about
app.NEQsheet_counter = 1; % first sheet in Questionnaires_NEQ.xlsx
. Doesn't this automatic reset to one conflict with the code in the body of the elseif?
app.NEQsheet_counter = app.NEQsheet_counter + 1;
Finally, you mention The 'if loop' then presents the remaining questions and writes these to file. This isn't a "loop". Do you want to be using a loop (with for or while)?
8 comentarios
frankenberry
el 25 de Abr. de 2020
Editada: frankenberry
el 25 de Abr. de 2020
Geoff Hayes
el 25 de Abr. de 2020
if you want to use a for loop, then you could do something like
for k = 1:app.NumberOfNEQQuestions
app.NEQCurrentCount = k; % NEQ question counter
% some code
end
once that is complete then you would move on to the next sheet (and so you would need to indicate somehow that the sheet number has changed. As for the code in the for loop it would be similar to what you have above but you probably wouldn't have to continually read from the Excel file - you would just do that once, and then add each question, and then update the file.
Más respuestas (0)
Ver también
Categorías
Más información sobre Dialog Boxes en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!