MATLAB: Reading image and pulling, RGB and pixel number from GIF format image

gifimage analysisimage processingImage Processing Toolbox

Hello,
I am working on a code to pull the RGB values from a gif frame by frame, then load them into a file in a specific format to load into a LED lighting array. I cannot seem to get it to label the LED numbers correctly, or how I want them numbered. When running the following code through matlab it seems to label the LED numbers in a different order. Here is the code I am using.
sourceimage = '/Users/tapotter/anime/leda11.gif'; %Pull file to load
numframes = numel(imfinfo(sourceimage)); %Pull the number of frames
fid = fopen('FastLEDAnimation.txt', 'a+');
for frame = 1:numframes %Go through each frame of the gif
fprintf('Frame: %d Complete', frame); %Post to the console when each frame is complete
[img, map] = imread(sourceimage, frame); % Open the image and create the map
rgbimg = im2uint8(ind2rgb(img, map)); %turn the index into unit8 rgb format
crgb = reshape(permute(rgbimg, [3, 1, 2]), 3, []); %makes a 3xnumpixels array
framedelay = imfinfo(sourceimage); %Pulls the frame delay from imfinfo
fdelay = framedelay.DelayTime;
pixcrgb = [1:size(crgb, 2); crgb]; %Sets up the readout to include what pixel its on(not working as I thought)
fprintf(fid, 'leds[%d].setRGB(%d, %d, %d);\n', crgb);
fprintf(fid, '\n FastLED.delay(%d)\n', fdelay);
end
fclose(fid);
Here is a sample of the output I am getting,
leds[0].setRGB(0, 0, 0);
leds[0].setRGB(0, 18, 20);
leds[20].setRGB(8, 8, 9);
leds[20].setRGB(34, 36, 20);
leds[34].setRGB(36, 20, 40);
leds[47].setRGB(33, 65, 70);
leds[22].setRGB(52, 54, 33);
leds[65].setRGB(70, 12, 70);
leds[80].setRGB(33, 65, 70);
It should be reading
leds[0].setRGB(0, 0, 0);
leds[1].setRGB(0, 18, 20);
leds[2].setRGB(8, 8, 9);
leds[3].setRGB(34, 36, 20);
When I look at the data that is generated for the "pixcrgb" data I can see it is correct, at least number going through the number of pixels in the image. I am attaching a screen shot of the data I am seeing through matlab. Does anyone know how I might get the required number from pixcrgb? Example at the top it is labeling the 1, 2, 3, 4, 5, followed by the RGB values. Next just to make sure I understand how the gif or image is being read, is it in this format?
// 0 > 1 > 2 > 3 > 4
// |
// .----<----<----<----'
// |
// 5 > 6 > 7 > 8 > 9
// |
// .----<----<----<----'
// |
// 10 > 11 > 12 > 13 > 14
// |
// .----<----<----<----'
// |
// 15 > 16 > 17 > 18 > 19

Best Answer

Change
fprintf(fid, 'leds[%d].setRGB(%d, %d, %d);\n', crgb);
to:
fprintf(fid, 'leds[%d].setRGB(%d, %d, %d);\n', pixcrgb);
Otherwise pixcrgb = [1:size(crgb, 2); crgb]; is not considered anywhere.