how i can fix this error?

i had this code and i don't understand what is rong. i have a text and i want to extract the words in it. and it gives me this error.

Answers (3)

As shown in your screenshot, fix these errors:
% Err 1
text_analiza = [rezultat_ascii_corpus; 32;32;32 ...]
%% Must be
text_analiza = [rezultat_ascii_corpus; 32;32;32];
% Err2
i==1;
j==1;
% must be
i=1;
j=1;
% it is not shown in your screenshot, but looks like
while
end % might be missing
i == 1;
would attempt to look for a variable named i and if it were found, then it would attempt to compare each element of i to 1, constructing a boolean array the same size as i as the result. The value is not being assigned to a variable so the resulting array would be assigned to the internal variable named ans . Then because of the semi-colon the result would be discarded (not displayed.)
If no variable named i were found, MATLAB would proceed to look for a function named i and if it found such a function, MATLAB would try to invoke the function with no parameters, and then try to compare the result to 1 and so on. MATLAB happens to have a built-in function named i which returns sqrt(-1) .
Likewise for the j == 1 line -- and MATLAB also happens to have a function named j which also returns sqrt(-1) .
Well... that is what would happen if you did not have a mistake earlier in the code. You have
text_analiza = [rezultat_ascii_corpus;32;23;32; ...];
In MATLAB, when you have the ... characters then everything from the ... to the result of the line is treated as a comment, and MATLAB expects the next line to be a continuation. So the ] on the line is just a comment, same as if you had done
text_analiza = [rezultat_ascii_corpus;32;23;32; ... %];
so you are in a situation where you have an active [ so you are building a list. And i==1 and j==1 happen to be valid expressions for contributing to a list being built. But while is a keyword that cannot exist inside an attempt to use [] to build a list, so MATLAB complains about the while
Your fundamental mistake is that you do not have a ] closing the [ that you started.

6 Comments

You still have the fundamental mistake that the ... makes the ] into a comment. effectively what you coded was
text_analiza = [rezultat_ascii_corpus; 32;32;32 %23 = End of Block semnifica finalul corpusului unui autor
with no ] there.
clear all
clc
close all
fopen('C:\Users\This Pc\Desktop\licenta\licenta\corpus');
text_analiza = [rezultat_ascii_corpus; 32;23;32]
Unrecognized function or variable 'rezultat_ascii_corpus'.
i == 1;
j == 1;
while (i<length(text_analiza))
cuvinte{j} = '';
while text_analiza(i)~=32 % cat timp nu am dat de caracterul spatiu, concatenam caracterele la celula care contine cuvantul respectiv
cuvinte{j} = [cuvinte{j},char(text_analiza(i))];
i = i+1;
end
j = j+1;
i = i+1; % cand dam de spatiu, sarim peste el ca sa nu ne blocam
end
[cuvinte_unice, ida, idb] = unique( cuvinte ) ;
counts = accumarray( idb, ones(size(idb)) ) ;
% cuvintele unice si numarul lor de ocurente
[ocurente_cuvinte_unice_sortate, indici_cuvinte_unice_sortate] = sort(counts,'descend');
frecvente_cuvinte_unice_sortate = ocurente_cuvinte_unice_sortate/length(cuvinte);
cuvinte_unice_sortate = cuvinte_unice(indici_cuvinte_unice_sortate);
cuvinte_numeric = nan(1,length(cuvinte)); %create a parallel vector with the same legth as the vector "cuvinte" and store in it
% the rank of the unique word corresponding to it; this will ease up the
% analysis later on, instead of working with cells, work with doubles
for i = 1:length(cuvinte_unice_sortate)
['PROGRES DE ',num2str(i/length(cuvinte_unice_sortate)*100),' %']
cuvinte_numeric(find(strcmp(cuvinte,cuvinte_unice_sortate(i)))) = i;
% for j = 1:length(cuvinte_unice_sortate)
% if strcmp(cuvinte(i),cuvinte_unice_sortate(j))
% cuvinte_numeric(i) = j;
% break;
% end
% end
end
% save comm_7_autori.mat
save('comm_9_autori_cuvinte.mat','cuvinte','cuvinte_numeric','cuvinte_unice',...
'cuvinte_unice_sortate','ocurente_cuvinte_unice_sortate','frecvente_cuvinte_unice_sortate');
% a=unique(cuvinte,'stable');
% b=cellfun(@(x) sum(ismember(cuvinte,x)),a,'un',0);
still the same error
You fopen() a file, but you do not store the resulting file identifier, and you do not use the file identifier to read anything; you also do not close the file identifier.
You do not initialize rezultat_ascii_corpus
The
i == 1;
j == 1;
are unlikely to be what you want. You almost certainly want assignment statements there, not comparison statements.
By the way: you may wish to consider using
cuvinte = regexp(text_analiza, ' +', 'split');
instead of your loops.

Sign in to comment.

@Armina Petrean, keep in mind that posting the picture of the code is not helpful. Even if we suggest a solution, it is not guaranteed to work because the suggestion is not the best because of limited information.
Copy - pasting the code or attaching it via the paperclip button makes it easier for us to help you.
Secondly, the 7th line of your code, where you define the variable "text_analiza", is incomplete, as you have used the three dots, "...", called ellipses. When you use this, MATLAB expects the code to continue in the next line.
But instead of continuing the code, by defining a numeric array, the next line is different. Essentially, you have defined the while loop inside the variable, which is not allowed, and thus gives the error.
%Incomplete statement
y = [1 2 3 ...]
i==1;
j==1;
while i+j<5
Illegal use of reserved keyword "while".
i=i+j;
end
It's not clear as to how you want to define "text_analiza", so it is difficult to suggest anything.
Additionally, use single '=' sign if you want to assign value to any variable.

3 Comments

clear all
clc
close all
fopen('C:\Users\This Pc\Desktop\licenta\licenta\corpus');
text_analiza = [rezultat_ascii_corpus; 32;32;32];
%23 = End of Block semnifica finalul corpusului unui autor
i = 1;
j = 1;
while (i<length(text_analiza))
cuvinte{j} = '';
while text_analiza(i)~=32 % cat timp nu am dat de caracterul spatiu, concatenam caracterele la celula care contine cuvantul respectiv
cuvinte{j} = [cuvinte{j},char(text_analiza(i))];
i = i+1;
end
j = j+1;
i = i+1; % cand dam de spatiu, sarim peste el ca sa nu ne blocam
end
[cuvinte_unice, ida, idb] = unique( cuvinte ) ;
counts = accumarray( idb, ones(size(idb)) ) ;
% cuvintele unice si numarul lor de ocurente
[ocurente_cuvinte_unice_sortate, indici_cuvinte_unice_sortate] = sort(counts,'descend');
frecvente_cuvinte_unice_sortate = ocurente_cuvinte_unice_sortate/length(cuvinte);
cuvinte_unice_sortate = cuvinte_unice(indici_cuvinte_unice_sortate);
cuvinte_numeric = nan(1,length(cuvinte)); %create a parallel vector with the same legth as the vector "cuvinte" and store in it
% the rank of the unique word corresponding to it; this will ease up the
% analysis later on, instead of working with cells, work with doubles
for i = 1:length(cuvinte_unice_sortate)
['PROGRES DE ',num2str(i/length(cuvinte_unice_sortate)*100),' %']
cuvinte_numeric(find(strcmp(cuvinte,cuvinte_unice_sortate(i)))) = i;
% for j = 1:length(cuvinte_unice_sortate)
% if strcmp(cuvinte(i),cuvinte_unice_sortate(j))
% cuvinte_numeric(i) = j;
% break;
% end
% end
end
% save comm_7_autori.mat
save('comm_9_autori_cuvinte.mat','cuvinte','cuvinte_numeric','cuvinte_unice',...
'cuvinte_unice_sortate','ocurente_cuvinte_unice_sortate','frecvente_cuvinte_unice_sortate');
% a=unique(cuvinte,'stable');
% b=cellfun(@(x) sum(ismember(cuvinte,x)),a,'un',0);
for unique words= cuvinte unice
After making changes, does this code work?

Sign in to comment.

Categories

Find more on Get Started with MATLAB in Help Center and File Exchange

Products

Release

R2020a

Asked:

on 23 May 2023

Commented:

on 24 May 2023

Community Treasure Hunt

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

Start Hunting!