Verfasst am: 21.01.2015, 21:25
Titel: Komisches Problem mit importfile
Hi
Wie schon der Titel sagt habe Ich ein merkwürdiges Problem mit der von Matlab generierten Funktion ''importfile''. Ich habe eine Reihe von txt-Dateien, die alle identisch(!) aufgebaut sind, d.h Header, Anzahl der Werte(24000) usw. sind gleich.
Wenn ich jetzt mit der importfile Funktion die txt-Dateien in Matlab einlese, funktioniert das bei den meisten Dateien auch (24000 Werte pro Channel). Allerdings bekomme Ich bei bestimmten txt-Dateien nur 18000 Werte pro Channel, obwohl die txt-Datei (bis auf die Werte) identisch ist.
Es handelt sich bei den Dateien um oberflächen, sowie intrakardiale EKG's.
Im Anhang findet ihr Sinusrhythmus.txt, bei der die Funktion die gewünschten 24000 Werte pro Channel speichert und Vorhofflattern.txt bei der die Funktion nicht richtig funktioniert.
Ich hoffe Ich habe mein Problem klar gemacht.
Falls irgendjemand weiß wo das problem liegt, wird mich das sehr freuen.
Vielen Dank für eure Hilfe!
Von Matlab generierter Code:
Code:
function[CHANNEL0,CHANNEL1,CHANNEL2] = importfile(filename, startRow, endRow)
%IMPORTFILE Import numeric data from a text file as column vectors.
% [CHANNEL0,CHANNEL1,CHANNEL2] = IMPORTFILE(FILENAME) Reads data from % text file FILENAME for the default selection.
%
% [CHANNEL0,CHANNEL1,CHANNEL2] = IMPORTFILE(FILENAME, STARTROW, ENDROW) % Reads data from rows STARTROW through ENDROW of text file FILENAME.
%
% Example: % [CHANNEL0,CHANNEL1,CHANNEL2] = importfile('AV-Block III.txt',8, 24007);
%
% See also TEXTSCAN.
% Auto-generated by MATLAB on 2014/12/02 12:55:16
%% Initialize variables.
delimiter = ';';
if nargin<=2
startRow = 8;
endRow = inf;
end
%% Read columns of data as strings: % For more information, see the TEXTSCAN documentation.
formatSpec = '%*s%*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, endRow(1)-startRow(1)+1, 'Delimiter', delimiter, 'HeaderLines', startRow(1)-1, 'ReturnOnError', false);
for block=2:length(startRow) frewind(fileID);
dataArrayBlock = textscan(fileID, formatSpec, endRow(block)-startRow(block)+1, 'Delimiter', delimiter, 'HeaderLines', startRow(block)-1, 'ReturnOnError', false);
for col=1:length(dataArray)
dataArray{col} = [dataArray{col};dataArrayBlock{col}];
end end
%% Convert the contents of columns containing numeric strings to numbers. % Replace non-numeric strings with NaN.
raw = [dataArray{:,1:end-1}];
numericData = NaN(size(dataArray{1},1),size(dataArray,2));
for col=[1,2,3] % 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;
ifany(numbers=='.');
thousandsRegExp = '^\d+?(\.\d{3})*\,{0,1}\d*$';
ifisempty(regexp(thousandsRegExp, '.', 'once'));
numbers = NaN;
invalidThousandsSeparator = true;
end end % Convert numeric strings to numbers. if ~invalidThousandsSeparator;
numbers = strrep(numbers, '.', '');
numbers = strrep(numbers, ',', '.');
numbers = textscan(numbers, '%f');
numericData(row, col) = numbers{1};
raw{row, col} = numbers{1};
end catch me
end end
ich bekomme bei beiden Dateien 24000 Werte. Mit welcher MATLAB-Version arbeitest du?
Übrigens dürfte es deutlich effizienter sein, wenn du direkt mit textscan arbeitest statt den generierten Code zu nutzen.
Also als Ich es heute wieder probiert habe, hat wieder alles funktioniert. Ich habe am code allerdings nichts verändert. ich arbeite mit Matlab R2014a.
Trotzdem vielen Dank!
Einstellungen und Berechtigungen
Du kannst Beiträge in dieses Forum schreiben. Du kannst auf Beiträge in diesem Forum antworten. Du kannst deine Beiträge in diesem Forum nicht bearbeiten. Du kannst deine Beiträge in diesem Forum nicht löschen. Du kannst an Umfragen in diesem Forum nicht mitmachen. Du kannst Dateien in diesem Forum posten Du kannst Dateien in diesem Forum herunterladen
MATLAB, Simulink, Stateflow, Handle Graphics, Real-Time Workshop, SimBiology, SimHydraulics, SimEvents, and xPC TargetBox are registered trademarks and The MathWorks, the L-shaped membrane logo, and Embedded MATLAB are trademarks of The MathWorks, Inc.