i just want to check it why its not working can you try it because in my friend laptop it was working pls help
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
fid = fopen('MATLAB.txt', 'r');%Read the file
n = 0;
while ~feof(fid)
line1 = fgetl(fid);
line = string(line1);%Read line of text
s = split(line);
n = n + length(s);%Define length of words
end
fclose(fid);
%Calculate words
fprintf('Number of words: %d\n', n);
%==================================
n1=0;
for i=1:length(s)
%Conver string to characters
character=convertStringsToChars(s(i));
%Read each charcater length
if(length(character)==3)
n1=n1+1;
else
n1=0;
end
end
%Define letters
fprintf('Number of words contain only three letters: %d\n', n1);
%=========================================================
n1=0;
for i=1:length(s)
character=convertStringsToChars(s(i));
%%Check charcatwr consiste if letter A
if(character(1)=='A')
n1=n1+1;
else
n1=0;
end
end
fprintf('Number of words Starts with A letters: %d\n', n1);
%=================================================================
k1=0;
for i=1:length(s)
character=convertStringsToChars(s(i));
n1=0;
n2=0;
for k=1:length(character)
if(character(k)=='A')
n1=n1+1;
%%Read chaaracter E
elseif(character(k)=='E')
n2=n2+1;
else
k2=0;
end
end
if(n2==0 || n1==0)
k1=0;
else
k1=k1+1;
end
end
fprintf('Number of words contain A and E letters: %d\n', k1+1);
%%=================================================================
Cc = double(line1(:));
B = double(['A':'Z' 'a':'z']); % Create Bin Ranges
Hcts2 = histc(Cc,B);
CB = char(B);
for k1 = 1:4 % Output Table
idxrng = (1:13)+13*(k1-1);
fprintf(1,['\n\t' repmat(' —%c— ', 1, 13) '\n'], CB(idxrng))
fprintf(1,['\n\t' repmat('%3d ', 1, 13) '\n\n'], Hcts2(idxrng))
end
11 Commenti
Risposta accettata
Jan
il 12 Dic 2022
s = fileread('MATLAB.txt');
words = strsplit(s);
Do you see it? The code can be simplified massively.
n1=0; % Just a hint: why a "1" in n1? The simpler, the better. Use "n".
for i = 1:length(s)
% Convert string to characters % Avoid indirections. Use strlength()
% instead.
% character=convertStringsToChars(s(i));
% Simpler: c = char/s(i));
% Read each charcater length
if strlength(c)==3)
n1=n1+1;
% No! else
% No! n1=0; % Do not reset n1 inside the loop
end
end
Much cleaner without a loop:
% Number of words with 3 characters:
n = nnz(strlength(words) == 3);
Use startsWith() and contains() for the other parts.
It is hard to guess if the histogram part meets the requirements, because you did not post, what is asked for. So if you still have problems, explain them.
6 Commenti
Dongyue
il 15 Dic 2022
for the script below
if strlength(character)==3)
a right parenthese need to be removed. This line should be:
if strlength(character)==3
Jan
il 16 Dic 2022
Modificato: Jan
il 16 Dic 2022
@Saleh: As I have suggested already, replace this loop by this line:
n = nnz(strlength(s) == 3); % Your "s" is called "words" in my suggestion
Use the same pattern to find words starting with the character "A":
n = nnz(startsWith(s, "A"));
and
n = nnz(contains(s, ["A", "E"]));
% maybe: contains(s, ["a", "e"], 'IgnoreCase', true);
As mentioned before: Without a description of the purpose and without copy of the error message, I cannot suggest a modification of the histogram section.
One of your problems was a not matching parenthesis. As long as you are not able to fix such trivial typos by your own, I do not see a chance, that you can solve the coming homework questions in the future.
Più risposte (1)
Walter Roberson
il 12 Dic 2022
feof() only predicts end-of-file in some circumstances. You need to test ischar() of the results of fgetl()
while ~feof(fid)
line1 = fgetl(fid);
line = string(line1);%Read line of text
s = split(line);
n = n + length(s);%Define length of words
end
s is only going to hold the result for the last fgetl()
7 Commenti
Vedere anche
Categorie
Scopri di più su Characters and Strings in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!