matlab统计文件中密码出现的次数
时间: 2024-01-05 11:03:28 浏览: 68
可以使用MATLAB中的文本处理函数来统计文件中密码出现的次数。以下是一种可能的实现方式:
1. 使用`fileread`函数读取文件中的所有文本内容,存储在一个字符串变量中。
```
text = fileread('filename.txt');
```
2. 定义密码字符串变量,例如:
```
password = 'mypassword';
```
3. 使用`strfind`函数查找密码在文本中出现的位置,返回一个包含位置索引的向量。
```
indices = strfind(text, password);
```
4. 统计向量长度即可得到密码出现的次数。
```
count = length(indices);
```
完整代码示例:
```
text = fileread('filename.txt');
password = 'mypassword';
indices = strfind(text, password);
count = length(indices);
disp(['The password appears ', num2str(count), ' times.']);
```
相关问题
matlab做txt文件里的十个常用密码次数的bar图
您好!以下是使用MATLAB绘制txt文件中十个常用密码次数的bar图的代码示例:
```matlab
% 读取txt文件中的数据
data = importdata('passwords.txt');
% 统计各个密码出现次数
passwords = unique(data);
count = zeros(length(passwords), 1);
for i = 1:length(passwords)
count(i) = sum(strcmp(data, passwords(i)));
end
% 找到出现次数最多的前十个密码
[sortedCount, idx] = sort(count, 'descend');
top10Passwords = passwords(idx(1:10));
top10Count = sortedCount(1:10);
% 绘制bar图
bar(top10Count)
xticklabels(top10Passwords)
ylabel('次数')
title('十个常用密码次数统计')
```
其中,假设txt文件名为passwords.txt,文件中每行为一个密码,代码首先使用`importdata`函数读取文件中的数据,然后使用`unique`函数得到所有不同的密码,并使用循环统计各个密码出现的次数。接着,使用`sort`函数找到出现次数最多的前十个密码,并分别存储在`top10Passwords`和`top10Count`中。最后,使用`bar`函数绘制bar图,并使用`xticklabels`函数将x轴标签设置为十个常用密码,使用`ylabel`函数设置y轴标签,使用`title`函数设置图表标题。
passwords=fileread('pswd.txt'); passwords1=regexp(passwords,'[\s,.?:;!-]+','split'); passwords1=lower(passwords1); % 统计密码出现次数 m = containers.Map('KeyType','char','ValueType','any'); for i = 1:length(passwords1) if m.isKey(passwords1{i}) m(passwords1{i}) = m(passwords1{i}) + 1; else m(passwords1{i}) = 1; end end % 对密码出现次数进行排序 v=m.values'; k=m.keys'; kv=sortrows([k,v],2,'descend'); disp('前五十个常用密码及其出现次数:'); kv(1:10,:) % 绘制前十个常用密码的条形图 figure; bar(1:min(10, length(sortedCounts)), sortedCounts(1:min(10, length(sortedCounts)))); xticks(1:min(10, length(sortedCounts))); xticklabels(sortedPasswords(1:min(10, length(sortedCounts)))); xlabel('密码'); ylabel('出现次数'); title(sprintf('前十个常用密码(%s)', [datestr(now) '林浩晟 2022280310']));
这段代码可以从一个密码文件中读取密码,并统计每个密码出现的次数,然后对密码出现次数进行排序,并绘制前十个常用密码的条形图。其中,`pswd.txt`是你的密码文件名,可以根据需要进行修改。
需要注意的是,这段代码使用了MATLAB中的`containers.Map`容器来实现对密码出现次数的统计,因此需要确保你的MATLAB版本支持该容器类型。
另外,这段代码中绘制条形图的部分存在一些问题,需要将`sortedCounts`和`sortedPasswords`改为`kv(:,2)`和`kv(:,1)`,并在`xticklabels`函数中添加`'Rotation',45`参数以避免标签重叠。
完整代码示例:
```
passwords = fileread('pswd.txt');
passwords1 = regexp(passwords, '[\s,.?:;!-]+', 'split');
passwords1 = lower(passwords1);
% 统计密码出现次数
m = containers.Map('KeyType', 'char', 'ValueType', 'any');
for i = 1:length(passwords1)
if m.isKey(passwords1{i})
m(passwords1{i}) = m(passwords1{i}) + 1;
else
m(passwords1{i}) = 1;
end
end
% 对密码出现次数进行排序
v = m.values';
k = m.keys';
kv = sortrows([k, v], 2, 'descend');
% 显示前十个常用密码及其出现次数
disp('前十个常用密码及其出现次数:');
disp(kv(1:10,:));
% 绘制前十个常用密码的条形图
figure;
sortedCounts = cell2mat(kv(:,2));
sortedPasswords = kv(:,1);
bar(1:min(10, length(sortedCounts)), sortedCounts(1:min(10, length(sortedCounts))));
xticks(1:min(10, length(sortedCounts)));
xticklabels(sortedPasswords(1:min(10, length(sortedCounts))));
xtickangle(45);
xlabel('密码');
ylabel('出现次数');
title(sprintf('前十个常用密码(%s)', [datestr(now) '林浩晟 2022280310']));
```
阅读全文
相关推荐
















