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 次查看(过去 30 天)
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 个评论
Saleh
Saleh 2022-12-11
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 2022-12-12
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

请先登录,再进行评论。

采纳的回答

Jan
Jan 2022-12-12
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 个评论
Jan
Jan 2022-12-16
编辑:Jan 2022-12-16
@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.

请先登录,再进行评论。

更多回答(1 个)

Walter Roberson
Walter Roberson 2022-12-12
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 个评论
Saleh
Saleh 2022-12-15
hhhhhhhhhhhhhhhhhhhhhheeeeeeeeeeeeeeeeeeeeeeeeeelllllllllllllllllllllllllllllllpppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp me in all codes not only one code يارب صبرك
Jan
Jan 2022-12-16
@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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by