MATLAB: How to refer to the elements of array cell

cell arraysfprintfMATLAB

I have two .mat files that are linked with each other. The first file named event_list is 61×1 and has the names of event categories in photographs. For example event_list(1)=parade, event_list(2)=outdoor, etc
The second file is called file_list and is again 61×1 cell array. Each element of file_list is a cell array. For example file_list(1) is a 575×1 array with all the names of the photographs in the folder parade (event_list(1)).
I am trying to create a txt file from these .mat files that will list the folder name (eg parade, outdoor, etc) and then the photograph name in that folder.
For example, I need it to be like this:
parade
photo1_parade
parade
photo_2_parade
outdoor
photo1_outdoor
outdoor
photo2_outdoor
This is the code I have written so far:
fid=fopen('MyFile.txt','w');
%
for i=1:size(event_list)
fprintf( event_list{i,:})
fprintf(fid, [ event_list{i,:}, '\r\n']);
element=file_list{i,1}
fprintf(fid, [element, '\r\n']);
end
I am getting the following error:
Error using horzcat Dimensions of matrices being concatenated are not consistent.
Error in Untitled (line 11) fprintf(fid, [element, '\r\n']);
Thank you!

Best Answer

The first issue with your code is with
for i = 1 : size(event_list)
size(something) is always a vector with at least 2 elements. As per the documentation of colon, when you give it a vector, it only uses the first element of the vector. So, if your event_list is a column vector (size returns [n 1]) your code will work (by accident) but if it's a row vector (size returns [1 n]) then your loop will fail
for i = 1 : numel(event_list)
is guaranteed to work regardless of the shape of your vector.
The second issue may be, if size(event_list, 2) > 1 with
[event_list{i,:}, '\r\n'])
this will concatenate all the elements of event_list{i, :} into one string, then add at the end of that string the '\r\n'. To fix this, use strjoin which will insert the '\r\n' between each string:
strjoin(event_list(i, :), '\r\n')
However from your description, it sounds like event_list is a vector, in which case event_list{i, :} is either the whole event_list{:} (if it's a row vector) or just one element event_list{i} (if it's a column vector). Either way the notation you use does not make sense and is just confusing.
In any case, it looks like before each element of file_list you want to have the corresponding event_list, something completely different from what your code is attempting to do. This would work:
for event_number = 1 : numel(event_list) %and use meaningful variable names
event_string = strjoin(file_list{event_number}, ['\r\n', event_list{event_number}, '\r\n']);
if ~isempty(event_string) %even_string will be empty if file_list has no entry for that event
%if it's not empty add the leading event_list and the trailing \r\n that strjoin will not have added
event_string = [event_list{event_number}, '\r\n', event_string, '\r\n'];
end
fprintf(fid, event_string);
end