How to save responses in a table

조회 수: 6 (최근 30일)
Ivana Smith
Ivana Smith 2022년 4월 15일
편집: BhaTTa 2025년 3월 19일
Hello!
I have a set of 5 images named image_1.png, image_2.png, etc. in my downloads and I ask for people to respond A or B to a question, and I want these responses saved in a table with 5 rows for each image, and each column being the response to that image. Is there any way to do this? I want to be able to transform it into a scatter plot later.
Thank you!

답변 (1개)

BhaTTa
BhaTTa 2025년 3월 19일
편집: BhaTTa 2025년 3월 19일
Hey @Ivana Smith, You can achieve this by creating a table in MATLAB to store the responses for each image. Each row of the table will correspond to an image, and each column will represent a different respondent's answer, you can simulate collecting responses by manually populating the table or by creating a function that prompts for input.
Note: I have used bar plot instead of scatter plot as i find bar plot to be more appropriate, you can change it based on your requirement
I have created the dummy data below:
% Number of images and respondents
numImages = 5;
numRespondents = 3; % You can adjust this based on your needs
% Initialize an empty cell array
responses = cell(numImages, numRespondents);
% Create a table with appropriate row and column names
imageNames = arrayfun(@(x) sprintf('image_%d.png', x), 1:numImages, 'UniformOutput', false);
respondentNames = arrayfun(@(x) sprintf('Respondent_%d', x), 1:numRespondents, 'UniformOutput', false);
% Create the table
responseTable = array2table(responses, 'RowNames', imageNames, 'VariableNames', respondentNames);
% Simulate collecting responses
responseTable{1, 1} = {'A'};
responseTable{2, 1} = {'B'};
responseTable{3, 1} = {'A'};
responseTable{4, 1} = {'B'};
responseTable{5, 1} = {'A'};
responseTable{1, 2} = {'B'};
responseTable{2, 2} = {'A'};
responseTable{3, 2} = {'B'};
responseTable{4, 2} = {'A'};
responseTable{5, 2} = {'B'};
responseTable{1, 3} = {'A'};
responseTable{2, 3} = {'B'};
responseTable{3, 3} = {'A'};
responseTable{4, 3} = {'B'};
responseTable{5, 3} = {'A'};
To transform the responses into a format suitable for a scatter plot, you need to convert the categorical responses ('A' and 'B') into numerical values.
% Convert responses to numerical values
countA = sum(strcmp(responseTable{:,:}, 'A'), 2);
countB = sum(strcmp(responseTable{:,:}, 'B'), 2);
% Create a bar plot
figure;
bar(1:numImages, [countA, countB], 'stacked');
xlabel('Image Number');
ylabel('Number of Responses');
title('Responses per Image');
legend({'A', 'B'});
xticks(1:numImages);
xticklabels(imageNames);
grid on;

카테고리

Help CenterFile Exchange에서 Tables에 대해 자세히 알아보기

태그

제품


릴리스

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by