Read every line from .txt file and selecting text from txt file.

2 views (last 30 days)
Matlab users,
I have a question regarding selecting GPS and acceleration values at Z-axis for every line of my .txt file.
Here is a sample from my .txt file:
38.4570N,27.1243E,34.70,26,14,9,25,_DDD_323,261,323
38.4570N,27.1243E,34.70,26,14,9,26,_DDD_324,262,325
38.4570N,27.1243E,34.70,26,14,9,26,_DDD_322,261,323
38.4570N,27.1243E,34.70,26,14,9,27,_DDD_324,262,324
38.4570N,27.1243E,34.70,26,14,9,28,_DDD_323,262,323
38.4570N,27.1243E,34.70,26,14,9,29,_DDD_324,263,325
38.4570N,27.1243E,34.70,26,14,9,29,_DDD_324,263,325
38.4570N,27.1243E,34.70,26,14,9,30,_DDD_323,263,325
38.4570N,27.1243E,34.70,26,14,9,31,_DDD_324,263,324
38.4570N,27.1243E,34.70,26,14,9,31,_DDD_322,262,323
And here is my code which i generated with import data tool in matlab.
close all;clear all; clc;
%%Import data from text file.
% Script for importing data from the following text file:
%
% /Users/apple/Documents/MATLAB/Graduation_Project/GPSDATA.TXT
%
% 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 2016/03/26 16:34:13
%%Initialize variables.
filename = '/Users/apple/Documents/MATLAB/Graduation_Project/GPSDATA.TXT';
delimiter = '';
%%Format string for each line of text:
% column1: text (%s)
% For more information, see the TEXTSCAN documentation.
formatSpec = '%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);
%%Post processing for unimportable data.
% No unimportable data rules were applied during the import, so no post
% processing code is included. To generate code which works for
% unimportable data, select unimportable cells in a file and regenerate the
% script.
%%Create output variable
GPSDATA = table(dataArray{1:end-1}, 'VariableNames', {'N271243E34702614925_DDD_323261323'});
%%Clear temporary variables
clearvars filename delimiter formatSpec fileID dataArray ans;
Unfortunatelly i am only able to import my .txt file as table from import data tool, when i intended to choose Column Vector, Numeric Matrix or Cell Array. I get a matrix or vector full with 0 s or Nan s.
My main goal is to select every line of this .txt file and get latitude,longitude,acc_z datas from it like:
38.4570N,27.1243E,34.70,26,14,9,25,_DDD_323,261,323
38.4570N,27.1243E,34.70,26,14,9,26,_DDD_324,262,325
38.4570N,27.1243E,34.70,26,14,9,26,_DDD_322,261,323
lat=[38.4570 38.4570 38.4570];
lon=[27.1243 27.1243 27.1243];
acc=[323 325 323];
Here is main code i am trying to use for this:
A=table2cell(GPSDATA);
RowToColoumn=A';
uzunluk=length(RowToColoumn);
First_Coloumn=RowToColoumn(1);
line=char(First_Coloumn);
lat_raw=line(1:7);
lat=str2num(lat_raw);
format long
lon_raw=line(10:16);
lon=str2num(lon_raw);
format long
acc_raw=line(49:51);
acc=str2num(acc_raw);
format long
I think i have to get the length of my RowToColoumn vector and use a for loop but unfortunately i am not able to think the correct algorithm for that.
P.S: 'I know ı typed column wrong :D'

Accepted Answer

per isakson
per isakson on 26 Mar 2016
Edited: per isakson on 26 Mar 2016
I guess you should use textscan. import is confused by the "E" and the "N". See textscan, Read formatted data from text file or string
This format string matches your data
cac = textscan('38.4570N,27.1243E,34.70,26,14,9,25,_DDD_323,261,323' ...
, '%fN%fE%f%f%f%f%f%s%f%f', 'Delimiter',',' )
outputs
cac =
Columns 1 through 8
[38.4570] [27.1243] [34.7000] [26] [14] [9] [25] {1x1 cell}
Columns 9 through 10
[261] [323]
and
>> cac{8}
ans =
'_DDD_323'

More Answers (0)

Categories

Find more on Large Files and Big Data 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!