I keep getting an error code on the last part of this code to convert some MNIST files to .mat form, does anyone have any ideas of what might be wrong.
5 views (last 30 days)
Show older comments
Here is the error
>> convert_mnist_to_mat
Error: File: convert_mnist_to_mat.m Line: 58 Column: 1
This statement is not inside any function.
(It follows the END that terminates the definition of the function
"read_mnist_labels".)
Here is the code.
% Function to convert MNIST dataset files into .mat format
function convert_mnist_to_mat(mnist_dir, output_file)
% Check if the directory exists
if ~isfolder(mnist_dir)
error('MNIST directory does not exist.');
end
% Read training images
train_images = read_mnist_images(fullfile(mnist_dir, 'train-images-idx3-ubyte'));
% Read training labels
train_labels = read_mnist_labels(fullfile(mnist_dir, 'train-labels-idx1-ubyte'));
% Read test images
test_images = read_mnist_images(fullfile(mnist_dir, 't10k-images-idx3-ubyte'));
% Read test labels
test_labels = read_mnist_labels(fullfile(mnist_dir, 't10k-labels-idx1-ubyte'));
% Save data into .mat file
save(output_file, 'train_images', 'train_labels', 'test_images', 'test_labels');
disp('MNIST dataset converted successfully.');
end
% Function to read MNIST image data
function images = read_mnist_images(filename)
fid = fopen(filename, 'rb');
assert(fid ~= -1, ['Could not open ', filename, '']);
magic_number = fread(fid, 1, 'int32', 0, 'ieee-be');
assert(magic_number == 2051, ['Invalid magic number in ', filename]);
num_images = fread(fid, 1, 'int32', 0, 'ieee-be');
num_rows = fread(fid, 1, 'int32', 0, 'ieee-be');
num_cols = fread(fid, 1, 'int32', 0, 'ieee-be');
images = fread(fid, [num_rows, num_cols, num_images], 'uint8');
fclose(fid);
images = permute(images, [2, 1, 3]);
end
% Function to read MNIST label data
function labels = read_mnist_labels(filename)
fid = fopen(filename, 'rb');
assert(fid ~= -1, ['Could not open ', filename, '']);
magic_number = fread(fid, 1, 'int32', 0, 'ieee-be');
assert(magic_number == 2049, ['Invalid magic number in ', filename]);
num_labels = fread(fid, 1, 'int32', 0, 'ieee-be');
labels = fread(fid, [num_labels, 1], 'uint8');
fclose(fid);
end
% Usage example
mnist_dir = 'C:\Users\gabep\Desktop\MNIST';
output_file = 'mnist_dataset.mat';
convert_mnist_to_mat(mnist_dir, output_file);
0 Comments
Accepted Answer
Cris LaPierre
on 29 Mar 2024
Edited: Cris LaPierre
on 29 Mar 2024
You have written a function, but the final 3 lines of code are not inside a function. Try moving the final 3 lines of code to the top to create a script with local functions. You can read more about this here.
% Usage example
mnist_dir = 'C:\Users\gabep\Desktop\MNIST';
output_file = 'mnist_dataset.mat';
convert_mnist_to_mat(mnist_dir, output_file);
% Function to convert MNIST dataset files into .mat format
function convert_mnist_to_mat(mnist_dir, output_file)
% Check if the directory exists
if ~isfolder(mnist_dir)
error('MNIST directory does not exist.');
end
% Read training images
train_images = read_mnist_images(fullfile(mnist_dir, 'train-images-idx3-ubyte'));
% Read training labels
train_labels = read_mnist_labels(fullfile(mnist_dir, 'train-labels-idx1-ubyte'));
% Read test images
test_images = read_mnist_images(fullfile(mnist_dir, 't10k-images-idx3-ubyte'));
% Read test labels
test_labels = read_mnist_labels(fullfile(mnist_dir, 't10k-labels-idx1-ubyte'));
% Save data into .mat file
save(output_file, 'train_images', 'train_labels', 'test_images', 'test_labels');
disp('MNIST dataset converted successfully.');
end
% Function to read MNIST image data
function images = read_mnist_images(filename)
fid = fopen(filename, 'rb');
assert(fid ~= -1, ['Could not open ', filename, '']);
magic_number = fread(fid, 1, 'int32', 0, 'ieee-be');
assert(magic_number == 2051, ['Invalid magic number in ', filename]);
num_images = fread(fid, 1, 'int32', 0, 'ieee-be');
num_rows = fread(fid, 1, 'int32', 0, 'ieee-be');
num_cols = fread(fid, 1, 'int32', 0, 'ieee-be');
images = fread(fid, [num_rows, num_cols, num_images], 'uint8');
fclose(fid);
images = permute(images, [2, 1, 3]);
end
% Function to read MNIST label data
function labels = read_mnist_labels(filename)
fid = fopen(filename, 'rb');
assert(fid ~= -1, ['Could not open ', filename, '']);
magic_number = fread(fid, 1, 'int32', 0, 'ieee-be');
assert(magic_number == 2049, ['Invalid magic number in ', filename]);
num_labels = fread(fid, 1, 'int32', 0, 'ieee-be');
labels = fread(fid, [num_labels, 1], 'uint8');
fclose(fid);
end
0 Comments
More Answers (0)
See Also
Categories
Find more on Import, Export, and Conversion in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!