cannot load csv file

Hi , I am new to matlab and I face this error each time I try to load my csv file using the dbload command in iris TOOLBOX.
d=dbload('data.csv')
d =
struct with no fields.
how can I solve this problem?

Answers (1)

Image Analyst
Image Analyst on 9 May 2025
Edited: Image Analyst on 9 May 2025

0 votes

Instead try using the functions: readtable, readmatrix, readcell, or csvread
If you have any more questions, then attach your data and code to read it in with the paperclip icon after you read this:

5 Comments

And what if I have to use the dbload function ? And each time I upload my data it always shows ''struct no field''

If you must use dbload() but dbload() consistently returns no fields for your input files, then all you can do is detect the "no fields" case and do something to handle it smoothly (such as giving a warning message and going on to the next file.)
d = dbload('data.csv');
if ~isstruct(d) || isempty(fieldnames(d))
%handle the case where d has no fields
else
%do real work
end
dbload() does whatever dbload() does. There is always the possibility of editting dbload() to do something different than it currently does; it is not clear that would be acceptable.
If you were to attach a sample data file, we could examine it to try to determine why dbload() is saying "no fields"
ok this is my file
Try this:
d = importdata('data.csv')
d = struct with fields:
data: [52x7 double] textdata: {53x9 cell}
numericalData = d.data;
% Get 7 column names. Adjust column indexes as needed.
columnNames = d.textdata(1, 2:end-1)
columnNames = 1x7 cell array
{'GDP'} {'CPI_U'} {'S'} {'RS'} {'GDP_RW'} {'CPI_RW'} {'RS_RW'}
% Convert to a table if you want to associate the variables with the columns.
t = array2table(numericalData, VariableNames = columnNames)
t = 52x7 table
GDP CPI_U S RS GDP_RW CPI_RW RS_RW ____ _____ ______ ___ __________ ______ _____ 3712 51.39 2599.2 12 2.3562e+06 87.28 NaN 3643 51.44 2491.9 12 2.4117e+06 88.48 NaN 3712 51.46 2603.4 12 2.3907e+06 88.54 NaN 4908 53.48 2630.2 12 2.48e+06 89.65 NaN 4150 55.46 2631.1 12 2.4021e+06 90.21 NaN 3727 55.55 2498.7 12 2.455e+06 91.69 NaN 4016 57.06 2366.7 12 2.4095e+06 91.94 NaN 5153 59.02 2589.1 12 2.4346e+06 91.7 NaN 3932 61.04 2606.5 10 2.2797e+06 91.07 NaN 3322 61.04 2735.6 10 2.3297e+06 91.84 2.5 3974 61.58 2899.2 9.5 2.3265e+06 91.59 1.5 5140 63.76 2815.8 9.5 2.407e+06 92.09 1 4074 65.93 2843.7 9.5 2.3097e+06 92.1 1 3571 66.67 2766.2 9.5 2.3867e+06 93.32 1 3737 67.75 2749.8 9.5 2.3796e+06 93.17 1 5087 69.95 2867.7 9.5 2.4641e+06 93.95 1
My speculation is that dbload() is having problems because the first field is not quoted with "" but still contains characters ('Q')
If so then possibly the 'preprocess' option of dbload() would be useful. 'proprocess' takes a function handle, with the function accepting a character vector and returning a character vector. You could put in a function handle to a function that puts " quotes around the first portion of text, such as
d = dbload('data.csv', 'preprocess', @PrePro)
function S = PrePro(S)
S = regexprep(S, '^(\w+)', '"$1"', 'lineanchors');
end

Sign in to comment.

Asked:

on 9 May 2025

Commented:

on 9 May 2025

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!