I made a table for neural network regression, regNet = trainNetwork(trData,'Percent',layers,options), and gor the following error Error using trainNetwork (line 165) Invalid training data table for regression. Predictors must be in the first column of the table, as a cell array of image paths or images. The Table, .txt file, I made has two variables, a string and a double, The first two rows looks like this. I thought they look exactly as in the tutorial . fileNames,Percent C:\Users\181620\SCF\iMEC\Data\regData\s7_x8y28_x8500y2800_1_1.png,68.6 C:\Users\181620\SCF\iMEC\Data\regData\s7_x8y28_x8500y2800_1_2.png,68.6
Kshitij Singh answered .
2025-11-20
The error arises because the format of your table does not match the requirements for regression tasks using trainNetwork. Specifically:
Ensure your table is formatted properly. If your table contains image paths and numeric regression targets, do the following:
Load the Table in MATLAB: If you read your .txt file into a table, ensure the fileNames column is recognized as a cell array of strings:
tbl = readtable('yourFile.txt');
tbl.fileNames = cellstr(tbl.fileNames); % Ensure the first column is a cell array
Check the Table: Verify that the table has the correct structure:
disp(tbl);
% It should look like:
% fileNames Percent
% {'C:\path\to\image1.webp'} 68.6
% {'C:\path\to\image2.webp'} 68.6
trainNetworkThe first column (fileNames) should contain image paths, and the second column (Percent) should contain numeric regression targets. Pass the table to trainNetwork as follows:
regNet = trainNetwork(tbl, 'Percent', layers, options);
Here:
'Percent' specifies the column containing the regression target.fileNames, is implicitly recognized as the input data.File Paths Not Found: Ensure that the file paths in fileNames are correct and accessible from your system. Use exist(path, 'file') to verify each path:
assert(all(cellfun(@(x) exist(x, 'file') == 2, tbl.fileNames)), 'Some file paths are invalid.');
Image Formats: Verify that all images have a consistent format (e.g., .webp, .jpg) and are readable by MATLAB.
Table Column Order: The predictors must be in the first column, and the response variable must be in the specified target column (in this case, 'Percent').
Table Data Types: The fileNames column must be a cell array of strings, and the Percent column must be numeric.
Here’s an example of creating and using a table for neural network regression:
% Example data
fileNames = {'C:\path\to\image1.webp'; 'C:\path\to\image2.webp'};
percent = [68.6; 72.1];
% Create table
tbl = table(fileNames, percent, 'VariableNames', {'fileNames', 'Percent'});
% Define a simple network
layers = [
imageInputLayer([32 32 3])
fullyConnectedLayer(1)
regressionLayer
];
options = trainingOptions('sgdm', 'MaxEpochs', 5, 'Plots', 'training-progress');
% Train network
regNet = trainNetwork(tbl, 'Percent', layers, options);
This will train the network using the table data. Ensure your table format matches this structure.