For Loop: How to add string to matrix?
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hi everyone,
I am new to Matlab, and I think I am on my way with this first problem but having a few issues.
Here is what my original data looks like...
INDEX TRACK NUMBER UTC DATE UTC TIME LOCAL DATE LOCAL TIME LATITUDE N/S LONGITUDE E/W ALTITUDE SPEED
1 1 1/05/2019 21:21:25 2/05/2019 10:21:25 36.28896 S 174.806381 E 0.196573 0.504
2 1 1/05/2019 21:21:30 2/05/2019 10:21:30 36.288898 S 174.806412 E -10.27161 0.144
3 1 1/05/2019 21:21:46 2/05/2019 10:21:46 36.288879 S 174.806396 E -0.534371 0
To this, I want to remove all columns except for 'Local Date', 'Local Time', 'Latitude' and 'Longitude'.
I also want to add a column called 'CallSign' in which all the rows will be filled with the word 'CallSign'.
This is working fine for lat/long, for these two columns I only adjust latitude by multiplying by -1 to get correct values.
My difficulties:
-how to add a word 'Callsign' to every row of that column, which seems like a simple problem...?
-how to convert date to datenum and time to datenum, do they need to be together in one column first? Or can they be converted separately?
-how to add column headers to my output file.
dd = 'input_data';
nowd = cd; %current folder
cd(dd); %go to input folder
d = dir('*.csv');
cd(nowd) %GO BACK TO current folder
%d=dir(fullfile(dd, '*.csv'));
for j = 1:length(d)
tic %tic starts a stopwatch timer, tic saves current time that TOC uses later to measure time elapsed between the two
filename = d(j).name; %get name of file (d.name) of jth iteration
disp(filename); %show filename
try %begin a 'try' block.
fid = fopen(fullfile(dd,filename)); %fopen opens file for access, fullfile builds filename from parts
%%make empty matrices for each variable
callsign = zeros(length(lines),1); %lines gives length of 64 (from default colourmap) -just a quick way of getting lots of rows? %1 represents number of columns
londecdeg = zeros(length(lines),1);
latdecdeg = zeros(length(lines),1);
date = zeros(length(lines),1);
time = zeros(length(lines),1);
tline = fgetl(fid); %fgetl returns next line of file associated with file identifier (fid)
i=1;
%% for each csv file add callsign, convert latdecdeg, date and time %convert to number format
while(1)
try
tline = fgetl(fid );
if( ischar(tline) == false ) %ischar to find out if input is a character array
break;
end
c = strsplit(tline,','); %strsplit splits tline ('next line') at delimiter ',' into c
callsign(i) = str2double(c{1}); fill values column with "Callsign" %str2double converts all rows(?) of column 4 to number format
londecdeg(i) = str2double(c{9})
lat = str2double(c{7}); %conver lat to number format and then to decimal degrees
latdecdeg(i) = lat*(-1)
try
date(i) = datenum(c{5},'dd-mm-yyyy'); %convert date to datenum
catch
date(i) = NaN;
end
i=i+1;
catch
disp('while loop error');
end
end
%% Create Output
out = [date, time, callsign, latdecdeg, londecdeg];
outFileName = strcat('DATENUM_TIME_CALLSIGN_LAT_LON',num2str(j),'.csv'); %labels each file with the same name and concatenates with a diff. number at the end of each
dlmwrite(fullfile('output',outFileName),out,'precision',15);
catch
disp(['ERROR!!!!!! ' filename])
end
toc
end
3 Kommentare
Antworten (0)
Siehe auch
Kategorien
Mehr zu Data Type Conversion finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!