cannot load csv file
Mostrar comentarios más antiguos
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?
Respuestas (1)
Image Analyst
el 9 de Mayo de 2025
Editada: Image Analyst
el 9 de Mayo de 2025
0 votos
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 comentarios
Tahina Riantsoa
el 9 de Mayo de 2025
Walter Roberson
el 9 de Mayo de 2025
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"
Tahina Riantsoa
el 9 de Mayo de 2025
Try this:
d = importdata('data.csv')
numericalData = d.data;
% Get 7 column names. Adjust column indexes as needed.
columnNames = d.textdata(1, 2:end-1)
% Convert to a table if you want to associate the variables with the columns.
t = array2table(numericalData, VariableNames = columnNames)
Walter Roberson
el 9 de Mayo de 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
Categorías
Más información sobre Text Files en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!