Error in formatting csv files imported from python
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hi,
I am trying to format two csv files imported from Python. The following set of commands work fine for one csv file but raises error with another csv file. The two csv files are of the same type, the only difference are the data. I am confused as to why one is working while the other doesn't work.
rawdata = readcell('medcoordsdof.csv'); % Use readcell
data = cellfun(@(x) strtrim(erase(x,{'[' ']'})),rawdata(2,:),'uni',0); % Remove the characters such as '[' & ']'
datacell = cellfun(@(x) str2double(strsplit(x,' ')),data,'uni',0);
The above code works fine with the attached medcoordsdof.csv file.
However, the same lines of code raises error for the adcoordsdof.csv file. For example,
rawdata = readcell('adcoordsdof.csv'); % Use readcell
data = cellfun(@(x) strtrim(erase(x,{'[' ']'})),rawdata(2,:),'uni',0); % Remove the characters such as '[' & ']'
datacell = cellfun(@(x) str2double(strsplit(x,' ')),data,'uni',0);
raises the error:
Error using erase (line 40)
First argument must be text.
Error in Bfield>@(x)strtrim(erase(x,{'[',']'})) (line 4)
data = cellfun(@(x) strtrim(erase(x,{'[' ']'})),rawdata(2,:),'uni',0); % Remove the characters such as '[' & ']'
Error in Bfield (line 4)
data = cellfun(@(x) strtrim(erase(x,{'[' ']'})),rawdata(2,:),'uni',0); % Remove the characters such as '[' & ']'
Any help or suggestion would be extremely useful and will be greatly appreciated.
Thank you.
0 Kommentare
Antworten (2)
Shlok
am 15 Okt. 2024
Hi Avishek,
This error is being caused due to incorrect delimiter detection.
When you provide only the filenames to the “readcell” function, MATLAB attempts to automatically detect the import options, including delimiters, data formats, and whitespace handling. You can inspect the detected import options using the “detectImportOptions” function. This function creates an import options object, which allows you to define how to read data from files, such as CSV or Excel.
Let us observe what import options are detected for both files:
detectImportOptions("medcoordsdof.csv")
detectImportOptions("adcoordsdof.csv")
For “medcoordsdof.csv”, a comma (,) is detected as the delimiter, while for “adcoordsdof.csv”, both tab and space (\t and ' ') are detected. As a result, the two files are parsed differently, causing the error.
To resolve this, you should manually specify the import options, particularly the delimiters. In this case, explicitly setting the delimiter to a comma will fix the problem. Here is the updated code:
rawdata = readcell('medcoordsdof.csv', Delimiter=','); % Use readcell
data = cellfun(@(x) strtrim(erase(x,{'[' ']'})),rawdata(2,:),'uni',0); % Remove the characters such as '[' & ']'
datacell = cellfun(@(x) str2double(strsplit(x,' ')),data,'uni',0)
rawdata = readcell('adcoordsdof.csv', Delimiter=','); % Use readcell
data = cellfun(@(x) strtrim(erase(x,{'[' ']'})),rawdata(2,:),'uni',0); % Remove the characters such as '[' & ']'
datacell = cellfun(@(x) str2double(strsplit(x,' ')),data,'uni',0)
For more details on the “readcell” and “detectImportOptions” functions, refer to the following MATLAB documentation links:
0 Kommentare
Stephen23
am 15 Okt. 2024
Bearbeitet: Stephen23
am 15 Okt. 2024
Well, those are some very badly designed files: if they had stored the data as a standard CSV file with one column for each variable then you would have no problems whatsoever. Instead each variable consists of double-quoted text containing all of its values in one long text vector... ugh UGH!
A much better way to arrange that data is to use a table with the data in columns (which is exactly how they should have been saved in the first place), here using the original column/variable names:
Ta = readtable( 'adcoordsdof.csv', Delimiter=',')
Tm = readtable('medcoordsdof.csv', Delimiter=',')
fh = @(c)sscanf(c{:}(2:end),'%f');
Ta = varfun(fh,Ta);
Ta.Properties.VariableNames = strrep(Ta.Properties.VariableNames,'Fun_','') % optional
Tm = varfun(fh,Tm);
Tm.Properties.VariableNames = strrep(Tm.Properties.VariableNames,'Fun_','') % optional
0 Kommentare
Siehe auch
Kategorien
Mehr zu Spreadsheets finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!