How to import, save and use data in multiple CSV files?
2 次查看(过去 30 天)
显示 更早的评论
Good day,
I have a problem with importing csv files in Matlab.
I have 3 CSV files with each 40 plus rows. The first row have titles. The variabels are splitted with a komma (,) and there are 3 columns in each CSV file.
I have tried to use _csvread_and also import data as a table. But every time I close and open the project, I have to import the files again.
I also can't find a way to use the data in the csv files.
My objective is:
- Interpolate the data from 2 CSV files
- Use the interpolated variabels with the other CSV file to have a result for each row of the other CSV file
- Plot the result as a variabele in function of the first colum of the file that I don't use to interpolate.
Can somebody help me with this problem? Give me any advice on importing the CSV files, splitting in a normal table and reading a specific cell for calculations?
Thanks in advance!
Daan
0 个评论
回答(2 个)
Star Strider
2015-10-29
Post the relevant parts of your code, and attach at least two of the files (if they are all essentially the same format).
I would use the textscan function to read the files if they have string data as well. The csvread function imports only numeric data.
0 个评论
timo
2015-10-29
编辑:timo
2015-10-29
Some hints I have CSV
a1, a2, a3, a4
1 ,2, 3, 4
x=py.open('eggs.csv', 'rb')
reader = py.csv.reader(x)//Or spamreader = py.csv.reader(x, pyargs('delimiter',' ','quotechar','|'))
line1=reader.next;
l1{1,2};
You need to have python installed
And you can see in Python is easy to read CSV files lol
Using Matlab auto generated code LOL
%% Import data from text file. % Script for importing data from the following text file: % % C:\Users\timo\Documents\MATLAB\eggs.csv % % To extend the code to different selected data or a different text file, % generate a function instead of a script.
% Auto-generated by MATLAB on 2015/10/29 19:57:22
%%Initialize variables.
filename = 'C:\Users\timo\Documents\MATLAB\eggs.csv';
delimiter = ',';
%%Read columns of data as strings:
% For more information, see the TEXTSCAN documentation.
formatSpec = '%s%s%s%s%[^\n\r]';
%%Open the text file.
fileID = fopen(filename,'r');
%%Read columns of data according to format string.
% This call is based on the structure of the file used to generate this
% code. If an error occurs for a different file, try regenerating the code
% from the Import Tool.
dataArray = textscan(fileID, formatSpec, 'Delimiter', delimiter, 'ReturnOnError', false);
%%Close the text file.
fclose(fileID);
%%Convert the contents of columns containing numeric strings to numbers.
% Replace non-numeric strings with NaN.
raw = repmat({''},length(dataArray{1}),length(dataArray)-1);
for col=1:length(dataArray)-1
raw(1:length(dataArray{col}),col) = dataArray{col};
end
numericData = NaN(size(dataArray{1},1),size(dataArray,2));
for col=[1,2,3,4]
% Converts strings in the input cell array to numbers. Replaced non-numeric
% strings with NaN.
rawData = dataArray{col};
for row=1:size(rawData, 1);
% Create a regular expression to detect and remove non-numeric prefixes and
% suffixes.
regexstr = '(?<prefix>.*?)(?<numbers>([-]*(\d+[\,]*)+[\.]{0,1}\d*[eEdD]{0,1}[-+]*\d*[i]{0,1})|([-]*(\d+[\,]*)*[\.]{1,1}\d+[eEdD]{0,1}[-+]*\d*[i]{0,1}))(?<suffix>.*)';
try
result = regexp(rawData{row}, regexstr, 'names');
numbers = result.numbers;
% Detected commas in non-thousand locations.
invalidThousandsSeparator = false;
if any(numbers==',');
thousandsRegExp = '^\d+?(\,\d{3})*\.{0,1}\d*$';
if isempty(regexp(thousandsRegExp, ',', 'once'));
numbers = NaN;
invalidThousandsSeparator = true;
end
end
% Convert numeric strings to numbers.
if ~invalidThousandsSeparator;
numbers = textscan(strrep(numbers, ',', ''), '%f');
numericData(row, col) = numbers{1};
raw{row, col} = numbers{1};
end
catch me
end
end
end
%%Allocate imported array to column variable names
a1 = cell2mat(raw(:, 1));
a2 = cell2mat(raw(:, 2));
a3 = cell2mat(raw(:, 3));
a4 = cell2mat(raw(:, 4));
%%Clear temporary variables
clearvars filename delimiter formatSpec fileID dataArray ans raw col numericData rawData row regexstr result numbers invalidThousandsSeparator thousandsRegExp me;
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Text Files 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!