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!