Project DATA: read, process and visualize
Show older comments
Hi MathWorks community,
i´m desperately seekin´ for help with my project which is actually "only" plot a couple of curves in a PLOT3 diagram. I thought it wouldn´t be as much work, so I did it the first place by hand, like creating variables and put the data in.. just took me 3h.
However, I wanna use a script to import datam which look like this:
Panel 2 2013-11-11 17-26_15.dat
Pyrh. (w/m2)
741.38
741.04
743.03
Like tabbed rows in a *.txt file. So, I tried to use the automatic function generation function. It´s working awesome for the one file (Panel 2 2013-11-11 17-26_15.dat).
The file looks like this:
function [VV,IA] = importfile(filename, startRow, endRow)
%IMPORTFILE Import numeric data from a text file as column vectors.
% [VV,IA] = IMPORTFILE(FILENAME) Reads data from text file FILENAME for
% the default selection.
%
% [VV,IA] = IMPORTFILE(FILENAME, STARTROW, ENDROW) Reads data from rows
% STARTROW through ENDROW of text file FILENAME.
%
% Example:
% [VV,IA] = importfile('Panel 2 2013-11-11 17-10_15.dat',3, 28);
%
% See also TEXTSCAN.
% Auto-generated by MATLAB on 2013/11/14 12:14:07
%%Initialize variables.
if nargin<=2
startRow = 3;
endRow = inf;
end
% Read columns of data as strings:
% For more information, see the TEXTSCAN documentation.
formatSpec = '%*16s%16s%16s%[^\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, endRow(1)-startRow(1)+1, 'Delimiter', '', 'WhiteSpace', '', 'HeaderLines', startRow(1)-1, 'ReturnOnError', false);
for block=2:length(startRow)
frewind(fileID);
dataArrayBlock = textscan(fileID, formatSpec, endRow(block)-startRow(block)+1, 'Delimiter', '', 'WhiteSpace', '', 'HeaderLines', startRow(block)-1, 'ReturnOnError', false);
for col=1:length(dataArray)
dataArray{col} = [dataArray{col};dataArrayBlock{col}];
end
end
%%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]
% 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
VV = cell2mat(raw(:, 1));
IA = cell2mat(raw(:, 2));
So, at that point I have 2 problems:
1. The function is overwriting the VV and IA everytime?
2. I found some lines to get the names of the files, but I´m not able to write a routine to use the first .dat file by running the import function, take out the desired data and save it in a variable. And then take the next one.
The names of the files are in a 61x1 struct and there as chars like 'Panel 2 2013-11-11 17-10_15.dat', which would fit perfectly in the importfile function.
Can anyone of you guys help me with those two questions?
Kind regards, Alex
Answers (0)
Categories
Find more on String Parsing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!