I am trying to find the difference in two columns on my csv file to plot them and my current code isnt working.

5 次查看(过去 30 天)
I have a code that intakes all of my participants per there csv file and outputs there data by the objects they have seen however I cannot code the difference between the known midpoint of the object versus what they have answered. Am I overlooking something? I did change some of the info to keep more discreet sorry. Thank you to anyone who helps!
% Select CSV file and read data
[filename, path] = uigetfile('*.csv', 'Select The CSV to View Subject Data');
myCSV = readtable(fullfile(path, filename));
% Define the column indices for KeyPressCount and ResponseTime
Midpoint = 60;
Final_Answered_Midpoint = 76;
%Assigning condition names by category {row, column}
ConditionNames{1,1} = '1';
ConditionNames{2,1} = '2';
ConditionNames{3,1} = '3';
ConditionNames{4,1} = '4';
ConditionNames{5,1} = '5';
ConditionNames{6,1} = '6';
ConditionNames{7,1} = '7';
ConditionNames{8,1} = '8';
ConditionNames{9,1} = '9';
ConditionNames{10,1} = '10';
ConditionNames{11,1} = '11';
ConditionNames{12,1} = '12';
NbrCond = size(ConditionNames,1);
%Difference between midpoint and final slider position that was answered
difference = myCSV{:,76} - myCSV{:,60};
%Creating a loop that through each of the Categories (ConditionNames) in column 1 of myCSV and
%reports the row indices every time each category is found
for condi = 1:NbrCond
ConditionIndex(condi).RowIndices = [];
Indices = strfind(table2array(myCSV(:,1)), ConditionNames{condi,1}); %Strfind the current (condi) condition in the array
counter = 0;
for j = 1:size(Indices,1)
if ~isempty(Indices{j,1})
counter = counter + 1;
ConditionIndex(condi).RowIndices(counter) = j;
end
end
end
%Loops over each category (ConditionNames) in CSV to find data (ex:
%KeyPressCount) and stores it in AllData
for condi = 1:NbrCond
ConditionInstances = size(ConditionIndex(condi).RowIndices,2);
counter = 0;
for Condi_instance_i = 1:ConditionInstances
CurrentRowIndex = ConditionIndex(condi).RowIndices(Condi_instance_i);
counter = counter + 1;
%AllData(condi).Category(counter,1) = final_slider_position_pix-Midpoint;
AllData(condi).Category(counter,1) = myCSV{CurrentRowIndex, difference};
end
end
% Calculating Averages, Standard Deviations, and Standard Errors and
% storing them in AllData by Category (Row)
for condi = 1:NbrCond
AllData(condi).Mean = mean(AllData(condi).Category);
AllData(condi).StdDev = std(AllData(condi).Category);
AllData(condi).StdErr = AllData(condi).StdDev / sqrt(size(AllData(condi).Category, 1));
end
CategoryAvgs = cellfun(@(x) mean(x), {AllData(:).Category});
CategoryStdDevs = cellfun(@(x) std(x), {AllData(:).Category});
CategoryStdErrs = CategoryStdDevs ./ sqrt(cellfun(@numel, {AllData(:).Category}));
%Creating variable to store all of the means and standarderrors for
%plotting purposes
Means = [AllData.Mean];
StdErrs = [AllData.StdErr];
%Creating a figure to summarize results
figure;
bar([AllData.Mean]);
hold on;
errorbar(1:NbrCond, CategoryAvgs, CategoryStdErrs, '.k');
ylim([-10 25])
xlabel('Tools');
ylabel('Difference');
% Set x-axis labels (goes in order of AllData.Mean)
xticklabels({'1', '2', '3', '4', ...
'5', '6', '7', '8', ...
'9', '10', '11', '12'});
xtickangle(45)
hold off;
  2 个评论
dpb
dpb 2024-7-15
Just some comments on coding efficiency in MATLAB...
NbrCond = 12; % set the number (or read from somewhere else)
ConditionNames=string(1:NbrCond).' % create the name automagically by code, not manually
ConditionNames = 12x1 string array
"1" "2" "3" "4" "5" "6" "7" "8" "9" "10" "11" "12"
Making into a categorical array wouldn't be bad or just leaving them as numeric instead of strings would make comparison easier without needing the string matching at all.
% Select CSV file and read data
[filename, path] = uigetfile('*.csv', 'Select The CSV to View Subject Data');
myCSV = readtable(fullfile(path, filename));
% Define the column indices for KeyPressCount and ResponseTime
Midpoint = 60;
Final_Answered_Midpoint = 76;
...
%Difference between midpoint and final slider position that was answered
difference = myCSV{:,76} - myCSV{:,60};
You read as a table, the above could be just
difference=myCSV.Var76-myCSV.Var60;
assuming you didn't have a row of other variable names. Ideally, those columns would also be identified with usable and recognizable variable names.
%Creating a loop that through each of the Categories (ConditionNames) in column 1 of myCSV and
%reports the row indices every time each category is found
for condi = 1:NbrCond
ConditionIndex(condi).RowIndices = [];
Indices = strfind(table2array(myCSV(:,1)), ConditionNames{condi,1}); %Strfind the current (condi) condition in the array
...
Same sort of thing; you have a table, use table referencing of the desired variables instead of turning the whole thing into an array every pass through a loop.
If the conditions were numeric or categorical, the above could be more like
for condi = 1:NbrCond
Indices=matches(myCSV.Var1,ConditionNames(condi)); % for string comparison
...
It's not clear exactly what you're shooting for in the next; give a problem description of what the end result is to be and undoubtedly somebody will be along and deal with the logic.
More than likely, any statistics or calculations by such a subsetting can be done with varfun with grouping variables without explicit looping.

请先登录,再进行评论。

采纳的回答

Shishir Reddy
Shishir Reddy 2024-7-16
Hi Kathryn
As per my understanding, you are facing issues in your code while trying to find the difference between two columns in a csv file. This is because of the wrong syntax used in the following code snippet.
for condi = 1:NbrCond
ConditionInstances = size(ConditionIndex(condi).RowIndices,2);
counter = 0;
for Condi_instance_i = 1:ConditionInstances
CurrentRowIndex = ConditionIndex(condi).RowIndices(Condi_instance_i);
counter = counter + 1;
AllData(condi).Category(counter,1) = myCSV{CurrentRowIndex, difference};
end
end
The line - AllData(condi).Category(counter,1) = myCSV{CurrentRowIndex, difference}; is incorrect because difference is a vector of differences, not a column index.
The correct approach is to use the difference vector directly, indexing it with CurrentRowIndex.
AllData(condi).Category(counter,1) = difference(CurrentRowIndex);
This ensures that the Category field in AllData is populated with the correct difference values for each condition.
I hope this helps.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by