Hi Jim
in order to answer the kind of questions you are facing you need a structure you can easily select elements according to questions. One way of building such structure is with the class table
file_name='ratings1.txt'
fileID=fopen(file_name)
str1={}
tline=fgetl(fileID)
str1=[str1 ; tline]
while ischar(tline)
tline = fgetl(fileID);
str1=[str1 ; tline]
end
fclose(fileID);
[sz1 sz2]=size(str1)
since from the sample supplied there only one '(', one ')' and one ':' in each line let's assume that no film name will have any of these characters:
film_name={};year={};country={};rating={};
for k=1:sz1-1
L1=str1{k}
split1=regexp(L1,'(')
split2=regexp(L1,')')
split3=regexp(L1,':')
L1_name=L1([1:split1-1])
L1_year=L1([split1+1:split2-1])
L1_country=L1([split2+2:split3-1])
L1_rating=L1([split3+1:end])
film_name=[film_name;L1_name]
year=[year;L1_year]
country=[country;L1_country]
rating=[rating;L1_rating]
end
T=table(film_name,year,country,rating)
Now you how to answer questions like the ones you have mentioned in the question:
because sure the film name length vary, you need strcmp() or isequal()
One way is to build yourself functions to search the structure, like
function pointer=cell_comp(cell1,name1)
pointer=0
[s1 s2]=size(cell1)
for k=1:s1
if isequal(cell1{k},name1)
pointer=[pointer k]
end
end
if sum(pointer)>0
pointer(1)=[]
end
end
to the question "what is the film rating for movie A in country B"
A='Ôritsu uchûgun Oneamisu no tsubasa'
film_A_entries=cell_comp(T.film_name,A)
B='Iceland'
country_B_entries=cell_comp(T.country,B)
intersect(film_A_entries,country_B_entries)
ans = Empty matrix: 1-by-0
there is no no such film A in country B
for
A='Êtes-vous fiancée à un marin grec ou à un pilote de ligne?'
B='Finland'
film_A_entries=cell_comp(T.film_name,A)
country_B_entries=cell_comp(T.country,B)
the answer now is
T.rating(intersect(film_A_entries,country_B_entries))
=
'S'
To the question: what is the film rating for movie A in countries B and C
A='Être et avoir'
B='Iceland'
C='Taiwan'
film_A_entries=cell_comp(T.film_name,A)
country_B_entries=cell_comp(T.country,B)
country_C_entries=cell_comp(T.country,C)
T.rating([country_B_entries country_c_entries]) =
'L'
'GP'
but in for this question
A='Ôritsu uchûgun Oneamisu no tsubasa'
B='USA'
C='Singapore'
film_A_entries=cell_comp(T.film_name,A)
country_B_entries=cell_comp(T.country,B)
country_C_entries=cell_comp(T.country,C)
T.rating([country_B_entries country_C_entries])=
'Unrated'
'PG'
wrong because film A is not listed in country C
The basic answer would be
intersect(intersect(film_A_entries,country_B_entries),country_C_entries)=
Empty matrix: 1-by-0
because the condition queried in the question does not exist.
If you want elaborate you could do instead the following:
T.rating(intersect(film_A_entries,country_B_entries)) =
'Unrated'
T.rating(intersect(film_A_entries,country_C_entries)) =
Empty cell array: 0-by-1
Please understand this is just one basic way to address the main line of your question.
If you find this answer of any help solving your question, please click on the thumbs-up vote link,
thanks in advance
John