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 view (last 30 days)
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 Comments
Saleh
Saleh on 11 Dec 2022
Coding, sometimes called computer programming, is how we communicate with computers. Code tells a computer what actions to take, and writing code is like creating a set of instructions. By learning to write code, you can tell computers what to do or how to behave in a much faster way. You can use this skill to make websites and apps, process data, and do lots of other cool things. We all have hopes, dreams, and plans for the future. Whether you’re looking for a new opportunity, want to optimize your current job, or are simply searching for a new hobby, coding can help you get closer to your goals. And remember, anyone can learn how to code!
this paraghragh up is my MATLAB.txt
Saleh
Saleh on 12 Dec 2022
i already sure the contant in the file it starts from coding until to code !
if matlab don't accept this sign (!) i will remove it no problem

Sign in to comment.

Accepted Answer

Jan
Jan on 12 Dec 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 Comments
Dongyue
Dongyue on 15 Dec 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
Jan on 16 Dec 2022
Edited: Jan on 16 Dec 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.

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 12 Dec 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 Comments
Saleh
Saleh on 15 Dec 2022
hhhhhhhhhhhhhhhhhhhhhheeeeeeeeeeeeeeeeeeeeeeeeeelllllllllllllllllllllllllllllllpppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp me in all codes not only one code يارب صبرك
Jan
Jan on 16 Dec 2022
@Saleh: Stop this. Do not bump your question by posting comments, which do not contain additional information. This wastes the time of the readers and in consequence your time.

Sign in to comment.

Categories

Find more on Characters and Strings in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!