필터 지우기
필터 지우기

how creat random text file

조회 수: 5 (최근 30일)
Aseel H
Aseel H 2013년 5월 25일
편집: Adam Danz 2020년 12월 16일
I need to creat text file have random charecters with spesific size
I need give it size to generate random text file
for example: i need file have 4 character text = "abca"
  댓글 수: 1
Jan
Jan 2013년 5월 25일
The question does not have a connection to the term "embedded" yet. Please clarify this.

댓글을 달려면 로그인하십시오.

채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2013년 5월 25일
s='a':'z'
n=4
out=s(randi(26,n,1))
  댓글 수: 2
Aseel H
Aseel H 2013년 5월 25일
thanks a lot it is OK, but additionally I need the resulte of out put in text file
by meanning : read the result from text file
Azzi Abdelmalek
Azzi Abdelmalek 2013년 5월 25일
Do you need to write a result to a text file?
Look at fprintf

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Adam Danz
Adam Danz 2019년 11월 1일
편집: Adam Danz 2020년 12월 16일
Another idea is to use randsample() to create random letters in the form of words separated by randomized spaces. This function allows you to set weights on the probability of each letter being drawn so you could use real letter frequencies for a laguage and then assign a frequency for spaces that controls the average word length.
Here's a demo using relative frequencies of English letters and an average words length of 5 letters.
% Desired avg word length (approximate)
wordLen = 5;
% Number of characters to produce
nchar = 100;
% Relative frequencies for each letter a:z
% https://en.wikipedia.org/wiki/Letter_frequency
freq = [ 8.167 1.492 2.782 4.253 12.702 2.228 2.015 6.094 6.966 ...
0.153 0.772 4.025 2.406 6.749 7.507 1.929 0.095 5.987 6.327 ...
9.056 2.758 0.978 2.36 0.15 1.974 0.074]./100 .*(1-1/wordLen);
% Re-weight the frequencies based on word length ^^^^^^^^^^^^^^^^
% To create a uniform frequency for each letter: freq = [1/wordLen,(1-1/wordLen)/26*ones(1,26)];
% The line below creates a character array of randomized lower case
% latin letters and spaces with a typical word length of 5 letters.
str = char(randsample([32,'a':'z'],nchar,true,[1/wordLen,freq]));
% [1] [2] [3]
% [1] 32 is the ascii value for [space]
% [2] 100 random characters will be generated
% [3] 1/5 to approximate a mean word length of 5 (for n letter words, use 1/n)
% Example output
disp(str)
a eeag rutetcaders odiodd wqeras dlwfwrtct hehwyaeina ushbr ao argcernpd h t dos ekksn dceeoeut h
% If desired, split the "words" into cell array of chars.
c = strsplit(str)
c = 1x16 cell array
{'a'} {'eeag'} {'rutetcaders'} {'odiodd'} {'wqeras'} {'dlwfwrtct'} {'hehwyaeina'} {'ushbr'} {'ao'} {'argcernpd'} {'h'} {'t'} {'dos'} {'ekksn'} {'dceeoeut'} {'h'}
% Let's see if the average word length is indeed what we set
% it to (5 in this demo)
slen = strlength(c);
mean(slen) % Yes, on average the words are 5 letters long
ans = 5.1250

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by