app designer input from spreadsheet and output to spreadsheet
조회 수: 1 (최근 30일)
이전 댓글 표시
The code below works for one spreadsheet from an excel workbook. I need it to work for 5 or more sheets in the same workbook. I realize this will likely be a "for loop" in the "% Selection changed function: ButtonGroup". I'm unsure where to put the loop. I think it needs to occur before the "if statement" and should end before
app.QuestionTextArea.Value = 'Questionnaire Complete! Click <Next> to continue.'; %indicates the end of the first questionnaire
Specifically, if I wanted to add:
num_sheets = length(sheetnames('Questionnaires.xlsx')); %number of sheets in Questionnaires.xlsx
for sheetcount = 1:1:length(num_sheets) %sheet counter for number of sheets
before the [app.CurrentCount = app.CurrentCount + 1;], where would I reference the variable sheetcount within the code below? Should sheetcount be a "for loop"?
The app is also using dynamic radio buttons which are coded in the startupFcn(app) and after the 'else statement' in the "% Selection changed function: ButtonGroup". If you need more information, please let me know. Helpful answers are appreciated.
% Code that executes after component creation
function startupFcn(app)
% Specify initial values when the app starts up; initialize values; set defaults
get(groot,'Screensize');
drawnow;
app.UIFigure.WindowState = 'maximized';
%app.Questions = readtable('Questionnaires.xlsx', 'NumHeaderLines', 0);
app.Questions = readtable('Questionnaires.xlsx',"ReadVariableNames",false,"TextType","string");
app.NumberOfQuestions = height(app.Questions);
app.Instructions = readtable('Questionnaires.xlsx',"ReadVariableNames",false,"TextType","string","Range","A1");
app.NumberOfInstructions = height(app.Instructions);
app.Columns = 1:1:width(app.Instructions);
app.Columns = 3:2:width(app.Questions);
app.QuestionTextArea.Value = app.Questions{app.CurrentCount,2};
app.InstructionsTextArea.Value = app.Instructions{app.CurrentCount,1};
Position = [10 50 90 25];
for columnNumber = app.Columns
option = app.Questions{app.CurrentCount, columnNumber};
if(~strcmp(option,""))
r = uiradiobutton(app.ButtonGroup,'Text', option, "FontSize",26);
if (columnNumber ~= 2)
Position(1) = Position(1) + 100;
end
r.Position = Position;
end
end
Position(1) = Position(1) + 100;
app.DummyButton = uiradiobutton(app.ButtonGroup, 'Visible',"off", 'Value', 1, 'Position', Position, 'Text', 'Dummy', 'FontSize', 26);
app.filenm.Question = zeros(0);
app.filenm.Answer = zeros(0);
end
.....
% Selection changed function: ButtonGroup
function ButtonGroupSelectionChanged(app, event)
selectedButton = app.ButtonGroup.SelectedObject;
app.CurrentCount = app.CurrentCount + 1;
newRow = {app.QuestionTextArea.Value{1}, selectedButton.Text};
app.Answers = [app.Answers; newRow];
if(app.CurrentCount > app.NumberOfQuestions)
writetable(app.Answers, 'Answers.xls');
writetable(app.Answers, app.filenm);
app.QuestionTextArea.Value = 'Questionnaire Complete! Click <Next> to continue.'; %indicates the end of the questionnaire
app.ButtonGroup.Visible = 'off';
else
while size(app.ButtonGroup.Children, 1) > 1
button = app.ButtonGroup.Children(end);
if(~strcmp(button.Text,'Dummy'))
button.Parent = [];
else
button = app.ButtonGroup.Children(end - 1);
button.Parent = [];
end
end
app.QuestionTextArea.Value = app.Questions{app.CurrentCount,2};
Position = [10 50 90 25];
for columnNumber = app.Columns
option = app.Questions{app.CurrentCount, columnNumber};
if(~strcmp(option,""))
r = uiradiobutton(app.ButtonGroup,'Text', option,"FontSize",26);
if (columnNumber ~= 2)
Position(1) = Position(1) + 100;
end
r.Position = Position;
end
end
app.ButtonGroup.SelectedObject = app.DummyButton;
end
end
댓글 수: 2
Guillaume
2020년 4월 4일
Is this correct?
app.Questions = readtable('Questionnaires.xlsx', ..
app.NumberOfQuestions = height(app.Questions);
app.Instructions = readtable('Questionnaires.xlsx', ..
app.NumberOfInstructions = height(app.Instructions);
You're reading twice the same thing?
Then you have
app.Columns = 1:1:width(app.Instructions);
app.Columns = 3:2:width(app.Questions);
The first of the two is obviously pointless.
SImilarly, in your callback, you have two consecutive writetable of the same table.
As for your question: "I need it to work for 5 or more spreadsheets"
Which part is "it"? I think you need to describe a bit more. If I understood your code displays a question and a selection of answers as radio button. Each time, a question is answered it moves onto the next question until all questions have been answered. The list of question comes from one spreadsheet. What would the workflow be if there are several worksheet of questions?
채택된 답변
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Spreadsheets에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!