Matlab脚本可以';t根据另一列的值正确计算分类变量

Matlab脚本可以';t根据另一列的值正确计算分类变量,matlab,count,Matlab,Count,我使用这个脚本来计算每个类别(“第1列”下的“go”、“nogo”)的真实分段数(第3列“分段良好”),但它从不返回实际数目:( 如果能再给我一双眼睛看看,我将不胜感激!谢谢 (此.txt文件由EGI netstation的.log文件转换而来,如果有必要的话) 示例.txt文件: 简要说明和前言: 不太确定此任务的所有实现要求,但这里有一种读取.txt/.log文件的方法。它使用函数textscan()将文件作为单元格数组扫描到MATLAB中,每个数据项的格式为%s%d%s(字符串、整数、字符

我使用这个脚本来计算每个类别(“第1列”下的“go”、“nogo”)的真实分段数(第3列“分段良好”),但它从不返回实际数目:(

如果能再给我一双眼睛看看,我将不胜感激!谢谢

(此.txt文件由EGI netstation的.log文件转换而来,如果有必要的话)

示例.txt文件:

简要说明和前言: 不太确定此任务的所有实现要求,但这里有一种读取
.txt
/
.log
文件的方法。它使用函数
textscan()
将文件作为单元格数组扫描到MATLAB中,每个数据项的格式为
%s%d%s
(字符串、整数、字符串、字符串)

类别:字符串→ <代码>%s
段号:整数→ <代码>%d
段好:字符串→ <代码>%s
眼动:字符串→ <代码>%s

在以下脚本中将此数据作为表示为
data
的单元格数组读取后,我们可以将此数组拆分为列。现在,我们可以使用
contains()检查哪些索引/行具有“Category”
go
nogo
函数。contains函数将包含两个参数。第一个参数是正在搜索的字符串/字符串数组,第二个参数是要搜索的字符串。
contains()
函数将为其可以找到要搜索的字符串的所有索引返回true“1”:

示例:

Result = contains(["Apple", "Pear", "Grape", "Apple"],"Apple");
将返回

Result = [1 0 1 0];
在评估与
go
nogo
相对应的索引后,我们可以使用这些值对第三列进行矩阵索引。矩阵索引允许我们使用逻辑数组获取与条件/相对应的所有索引。在这种情况下,我们的逻辑数组/条件是与
nogo
go相关的索引。将
contains(,“true”)
应用于这些新的子集将允许我们找到每个类别
go
nogo
中出现true的位置。最后使用函数
nnz()
(非零数)将允许您找到有多少次
contains()
返回true


脚本:
扩展名:在目录中的文件之间循环
使用MATLAB R2019b运行

精确计算,您要计算哪些输出参数?您是否只想在第3列中显示true的次数?对于
sample.txt
,您能给出预期的输出吗?是的!我想在第3列中为每个类别显示true的次数。感谢您的解决方案!一旦我能使用matlab,我会尝试一下。不,没问题。希望一切都很好。:)代码工作正常!非常感谢。但我很难将它与上面发布的原始脚本相适应,这样它就可以循环遍历文件夹中的所有文件,并在表中返回结果。不用担心,如果这将需要你一些时间,但任何帮助将不胜感激@索菲很高兴它奏效了。:)我添加了在文件夹中循环文件的部分。在本例中,我创建了
.txt
文件,但您也可以合并
.log
文件。脚本将位于我的答案中的
扩展部分
部分。
Result = contains(["Apple", "Pear", "Grape", "Apple"],"Apple");
Result = [1 0 1 0];
clear;
clc;

%Reading in the data as a cell array%
fileID = fopen('sample.log', 'r');

Header = string(fgetl(fileID));
Data = textscan(fileID,'%s %d %s %s');

fclose(fileID);

%Splitting the data into specific columns%
Category = Data(:,1);
Category = string(Category{1,1});
Segment_Number = Data(:,2); 
Segment_Number = string(Segment_Number{1,1});
Segment_Good = Data(:,3);
Segment_Good = string(Segment_Good{1,1});
Eye_Movements = Data(:,4);
Eye_Movements = string(Eye_Movements{1,1});


%Finding the indices corresponding to "go" and "nogo"%
No_Go_Indices = contains(Category,"nogo");
Go_Indices = ~No_Go_Indices;

%Finding how many true cases in column 3 (Segment_Good) corresponding to "go" and "nogo"
Go_True_Cases = nnz(contains(Segment_Good(Go_Indices),"true"));
No_Go_True_Cases = nnz(contains(Segment_Good(No_Go_Indices),"true"));


%Counting the number of times true occurs in each column%
Column_3_True_Count = nnz(contains(Segment_Good,"true"));
Column_4_True_Count = nnz(contains(Eye_Movements,"true"));

%Printing the results to the command window%
fprintf("Category: go -> %d true\n",Go_True_Cases);
fprintf("Category: nogo -> %d true\n\n",No_Go_True_Cases);

fprintf("Total true cases: %d\n", Column_3_True_Count);
clear;
clc;

%Full or relative directory path%

%Adding directory with the files to the be accessible%
Directory_Path = "Files"; 
addpath(Directory_Path);

%Reading the filenames within the directory and the number of files%
Text_Files = dir(Directory_Path+'/*.txt');
Number_Of_Files = length(Text_Files);

%Creating arrays that will hold the results%
All_No_Go_True_Cases = zeros(Number_Of_Files,1);
All_Go_True_Cases = zeros(Number_Of_Files,1);

%Looping through the files and running the function that will grab the
%results%
for File_Index = 1: Number_Of_Files

File_Name = string(Text_Files(File_Index).name);


[No_Go_True_Cases,Go_True_Cases] = Get_Data(File_Name);
All_No_Go_True_Cases(File_Index) = No_Go_True_Cases;
All_Go_True_Cases(File_Index) = Go_True_Cases;

end

All_No_Go_True_Cases
All_Go_True_Cases


%Local function definition%
function [No_Go_True_Cases,Go_True_Cases] = Get_Data(File_Name)
%Reading in the data as a cell array%
fileID = fopen(File_Name, 'r');

Header = string(fgetl(fileID));
Data = textscan(fileID,'%s %d %s %s');

fclose(fileID);

%Splitting the data into specific columns%
Category = Data(:,1);
Category = string(Category{1,1});
Segment_Number = Data(:,2); 
Segment_Number = string(Segment_Number{1,1});
Segment_Good = Data(:,3);
Segment_Good = string(Segment_Good{1,1});
Eye_Movements = Data(:,4);
Eye_Movements = string(Eye_Movements{1,1});


%Finding the indices corresponding to "go" and "nogo"%
No_Go_Indices = contains(Category,"nogo");
Go_Indices = ~No_Go_Indices;

%Finding how many true cases in column 3 (Segment_Good) corresponding to "go" and "nogo"
Go_True_Cases = nnz(contains(Segment_Good(Go_Indices),"true"));
No_Go_True_Cases = nnz(contains(Segment_Good(No_Go_Indices),"true"));


%Counting the number of times true occurs in each column%
Column_3_True_Count = nnz(contains(Segment_Good,"true"));
Column_4_True_Count = nnz(contains(Eye_Movements,"true"));

%Printing the results to the command window%
fprintf("Category: go -> %d true\n",Go_True_Cases);
fprintf("Category: nogo -> %d true\n",No_Go_True_Cases);
fprintf("Total true cases: %d\n\n", Column_3_True_Count);
end