cannot load csv file

4 views (last 30 days)
Tahina Riantsoa
Tahina Riantsoa on 9 May 2025
Commented: Walter Roberson on 9 May 2025
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
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
Image Analyst
Image Analyst on 9 May 2025
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
Walter Roberson
Walter Roberson on 9 May 2025
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.

Community Treasure Hunt

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

Start Hunting!