How can I handle multiple files at their import ?
Show older comments
Hello everyone,
I have implemented a matlab code, which import data from a text file in order to calculate some statistics. When the code is executed, the user defines the "filemode" and a system "gnss", which are both combined to a string that leads to a txt file named filemodegnss.pos.
clear all;
close all;
format long;
msg1 = 'Specify your filemode [e.g. unb3_M1S_A] => ';
filemode = input(msg1, 's');
msg2 = 'Specify your GNSS [e.g. g/gr/ge/grec] => ';
gnss = input(msg2, 's');
filename = [filemode gnss '.pos'];
f = fopen(filename, 'r');
if gnss == 'g'
startRow = 19;
else startRow = 20;
end
formatSpec = '%10s%13s%15s%15s%15s%4s%4s%9s%9s%9s%9s%9s%9s%7s%s%[^\n\r]';
dataArray = textscan(f, formatSpec, 'Delimiter', '', 'WhiteSpace', '', 'HeaderLines' ,startRow-1, 'ReturnOnError', false);
fclose(f);
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=[3,4,5,6,7,8,9,10,11,12,13,14,15]
% 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
dateFormats = {'Enter custom format here.', 'HH:MM:SS'};
for col=[1,2]% Convert the contents of column with dates to serial date numbers using date format string (datenum).
for row=1:length(dataArray{col})
try
numericData(row, col) = datenum(dataArray{col}{row}, dateFormats{col==[1,2]});
raw{row, col} = numericData(row, col);
catch me
end
end
end
%%Replace non-numeric cells with NaN
R = cellfun(@(x) ~isnumeric(x) && ~islogical(x),raw); % Find non-numeric cells
raw(R) = {NaN}; % Replace non-numeric cells
%%Create output variable
P = cell2mat(raw);
%%Clear temporary variables
clearvars filename startRow formatSpec fileID dataArray ans raw col numericData rawData row regexstr result numbers invalidThousandsSeparator thousandsRegExp me dateFormats R;
for n = 1:(size(P,1))
X = P(n,3);
sigmaX = P(n,8);
Y = P(n,4);
sigmaY = P(n,9);
Z = P(n,5);
sigmaZ = P(n,10);
Coords(n,:) = [X sigmaX Y sigmaY Z sigmaZ];
end
obsperiod = input('Specify the obs period [Press 24, 12, 6, 4, 2, 1, 0.5 h] => ');
if obsperiod == 24
t = 24;
C = Coords(t*60*2,:)
elseif obsperiod == 12
t = 12;
C1 = Coords(1*(t*60*2+1)-0,:);
C2 = Coords(2*(t*60*2+1)-1,:);
C = [C1; C2]
elseif obsperiod == 6
t = 6;
for i = 1:(24/t)
C1(i,:) = Coords(i*(t*60*2+1-1),:);
end
C = C1
elseif obsperiod == 4
t = 4;
for i = 1:(24/t-1)
C1(i,:) = Coords(i*(t*60*2+1),:);
end
C2 = Coords((24/t)*(t*60*2+1)-1,:);
C = [C1; C2]
elseif obsperiod == 2
t = 2;
for i = 1:(24/t)
C1(i,:) = Coords(i*(t*60*2+1-1),:);
end
C = C1
elseif obsperiod == 1
t = 1;
for i = 1:(24/t)
C1(i,:) = Coords(i*(t*60*2+1-1),:);
end
C = C1
elseif obsperiod == 0.5
t = 0.5;
for i = 1:(24/t)
C1(i,:) = Coords(i*(t*60*2+1-1),:);
end
C = C1
elseif obsperiod == 0.25
t = 0.25;
for i = 1:(24/t-1)
C1(i,:) = Coords(i*(t*60*2+1),:);
end
C2 = Coords((24/t)*(t*60*2+1)-1,:);
C = [C1; C2]
end
avgsdx = mean(C(:,2));
avgsdy = mean(C(:,4));
avgsdz = mean(C(:,6));
RMSx = rms(C(:,2));
RMSy = rms(C(:,4));
RMSz = rms(C(:,6));
What I want to do is define more systems called "GNSS" in the start of the program's execution and give a different name to the P matrix that it's made later in order to handle their data. How exactly can I do this ? I tried to do a while loop like this below, in order to define my systems and map the filemode with every each system GNSS, but when I call "exit" to terminate the loop and move on, the code still wants my to insert an input:
msg2 = 'Specify your GNSS =>';
while strcmp(msg2, 'exit')
gnss = input(msg2, 's');
msg2 = 'Specify your GNSS =>'
end
Any suggestion ?
Thank you in advance.
Accepted Answer
More Answers (0)
Categories
Find more on Cell Arrays 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!