Why the numeric data of combined data cannot be sorted well (as the original file) if it written along with the other string data?

조회 수: 5(최근 30일)
Tyann Hardyn
Tyann Hardyn 2021년 8월 29일
편집: Stephen 2021년 8월 30일
Hi matlab community,
I want to ask about my code below that would draw a data and extract it to a new txt file by using multiple data selection. The output of my code is :
1) Each of extracted data files contain with the original consecuted data files;
2) Combined data files that should be contained with each of consecuted data files (This Is My Problem)
Here is my code :
close all
clear clc
clc
[namafile,arah]=uigetfile({'*.txt', 'Text-files (*.txt)'},'Load Data Magnet Format IAGA WDS (Definitif / Variatif)', 'Multiselect','on');
cd(arah);
full = fullfile(arah, namafile);
namafiles = cellstr(namafile); %takes care of case only one file was selected
fulls = string(full);
nfiles1 = size(full,2);
nfiles2 = numel(full);
f = cellstr(fullfile(arah,namafile));
file = length(f);
if file > 1
%Kode untuk multi file
fullnames = fullfile(arah, namafiles);
fullname1 = fullnames{nfiles2};
namas = namafile{nfiles2};
[~, namas] = fileparts(fullname1);
judul2 = length(namas);
defm2 = sum(contains(namafiles, 'dmin'));
varm2 = sum(contains(namafiles, 'vmin'));
hord2 = sum(contains(namafiles, 'dhor'));
if judul2 == 13 || defm2 == nfiles2
for a_defm = 1:nfiles1
fullnamedefm0 = fullnames{a_defm};
stasiun_defm0 = upper(namas(1:3));
stzdefm0 = upper(stasiun_defm0+"Z");
stxdefm0 = upper(stasiun_defm0+"X");
stydefm0 = upper(stasiun_defm0+"Y");
stfdefm0 = upper(stasiun_defm0+"F");
stdfix_defm = convertStringsToChars(std_defm);
tipedefm = lower(namas(end-3:end));
opts = detectImportOptions(fullnamedefm0,'NumHeaderLines',12,'ReadVariableNames',true, 'Delimiter', ' ', 'Whitespace', ' ',...
'ConsecutiveDelimitersRule', 'join', 'EmptyLineRule', 'skip');
opts.SelectedVariableNames = ["DATE","TIME",stzdefm0,stxdefm0,stydefm0,stfdefm0];
opts.VariableNamesLine = 13; % Set variable names line
opts = setvartype(opts,{'DATE','TIME'},'string');
Tdefm0 = readtable(fullnamedefm0,opts);
DATEdefm = Tdefm0{:,1};
TIMEdefm = Tdefm0{:,2};
TIMEdefms = duration(TIMEdefm, 'InputFormat', 'hh:mm:ss.SSS', 'Format', 'hh:mm:ss');
TIMEdefmfix = string(TIMEdefms);
DOZdefm = Tdefm0{:,3};
DOFdefm = Tdefm0{:,6};
DOXdefm = Tdefm0{:,4};
DOYdefm = Tdefm0{:,5};
DOHSdefm = sqrt((DOXdefm.^2)+(DOYdefm.^2));
DOFSdefm = sqrt((DOHSdefm.^2)+(DOZdefm.^2));
periode = DATEdefm +" "+ TIMEdefmfix;
awaldefm = datetime(DATEdefm(1),'InputFormat','yyyy-MM-dd');
akhirdefm = datetime(DATEdefm(end),'InputFormat','yyyy-MM-dd');
date_combine{a_defm} = DATEdefm ;
time_combine{a_defm} = TIMEdefmfix ;
doz_combine{a_defm} = DOZdefm ;
dox_combine{a_defm} = DOXdefm ;
doy_combine{a_defm} = DOYdefm ;
dohs_combine{a_defm} = DOHSdefm ;
dofs_combine{a_defm} = DOFSdefm ;
dof_combine{a_defm} = DOFdefm ;
per_combine{a_defm} = periode;
datedefm_table = sort(vertcat(date_combine{:}));
timedefm_table = vertcat(time_combine{:});
dozdefm_table = vertcat(doz_combine{:});
doxdefm_table = vertcat(dox_combine{:});
doydefm_table = vertcat(doy_combine{:});
dohsdefm_table = vertcat(dohs_combine{:});
dofsdefm_table = vertcat(dofs_combine{:});
dofdefm_table = vertcat(dof_combine{:});
perdefm_table = vertcat(per_combine{:});
dohsdefm_table = sqrt((doxdefm_table.^2)+(doydefm_table.^2));
dofsdefm_table = sqrt((dohsdefm_table.^2)+(dozdefm_table.^2));
awal_defm = strcat(datedefm_table(1,1));
akhir_defm = strcat(datedefm_table(end,1));
tahun_defm = str2double(extractBetween(awal_defm,1,4));
bulan_defm = str2double(extractBetween(awal_defm,6,7));
hari_defm = str2double(extractBetween(awal_defm,9,10));
tahuns_defm = str2double(extractBetween(akhir_defm,1,4));
bulans_defm = str2double(extractBetween(akhir_defm,6,7));
haris_defm = str2double(extractBetween(akhir_defm,9,10));
wawal_defm = datetime(tahun_defm,bulan_defm,hari_defm,0,0,0);
wakhir_defm = datetime(tahuns_defm,bulans_defm,haris_defm,0,0,0);
alpha_defm = string(wawal_defm);
omega_defm = string(wakhir_defm);
eval(['cd ''' arah ''';']);
iagadefm = sprintf('Komponen Magnet Data Definitif IAGA Per-Menit (%s) Periode %s Hingga %s Stasiun %s.txt', tipedefm, awaldefm, akhirdefm, stasiun_defm0);
fulliagadefm = fullfile(arah, iagadefm);
fout = fopen(fulliagadefm,'w');
fprintf(fout,'%s %s %s %s %s %s %s %s\n', 'DATE', 'TIME', 'H(Calc)', 'F(Calc)', 'F(Obs)', 'X(Obs)', 'Y(Obs)', 'Z(Obs)');
for us = 1:length(TIMEdefm)
fprintf(fout,'%s %s %.2f %.2f %.2f %.2f %.2f %.2f\n', DATEdefm(us), TIMEdefmfix(us), DOHSdefm(us), DOFSdefm(us), DOFdefm(us), DOXdefm(us), DOYdefm(us), DOZdefm(us));
end
fclose(fout);
end
multi = "Komponen Magnet Data Definitif IAGA Per-Menit" + " " + "("+tipedefm+ ")" + " " +"Periode"+" "+alpha_defm+ " " + "Hingga" +" "+omega_defm+ " "+ "Stasiun" + " " + stasiun_defm0 ;
[savefile, arah, ~] = uiputfile('*.txt', 'Simpan Hasil Sebagai', multi);
eval(['cd ''' arah ''';']);
if isempty(savefile)
return; %user cancel!
end
fout=fopen(savefile,'w');
fprintf(fout,'%s %s %s %s %s %s %s %s\n', 'DATE', 'TIME', 'H(Calc)', 'F(Calc)', 'F(Obs)', 'X(Obs)', 'Y(Obs)', 'Z(Obs)');
fprintf(fout,'%s %s %.2f %.2f %.2f %.2f %.2f %.2f\n', [datedefm_table, timedefm_table, dohsdefm_table, dofsdefm_table, dofdefm_table, doxdefm_table, doydefm_table, dozdefm_table]');
fclose(fout);
end
end
The above code should work until it process the last process (combined data) without any of errors (use multiselect to select the three of my attached datas). The separated output data file from the above code is not my problem, it just arranged well just like the original data. Here is the pict :
The output of single / separated data conversion is done well, sorted to each of time series. BUT in the combined data ouput by using this code :
multi = "Komponen Magnet Data Definitif IAGA Per-Menit" + " " + "("+tipedefm+ ")" + " " +"Periode"+" "+alpha_defm+ " " + "Hingga" +" "+omega_defm+ " "+ "Stasiun" + " " + stasiun_defm0 ;
[savefile, arah, ~] = uiputfile('*.txt', 'Simpan Hasil Sebagai', multi);
eval(['cd ''' arah ''';']);
if isempty(savefile)
return; %user cancel!
end
fout=fopen(savefile,'w');
fprintf(fout,'%s %s %s %s %s %s %s %s\n', 'DATE', 'TIME', 'H(Calc)', 'F(Calc)', 'F(Obs)', 'X(Obs)', 'Y(Obs)', 'Z(Obs)');
fprintf(fout,'%s %s %.2f %.2f %.2f %.2f %.2f %.2f\n', [datedefm_table, timedefm_table, dohsdefm_table, dofsdefm_table, dofdefm_table, doxdefm_table, doydefm_table, dozdefm_table]');
fclose(fout);
(The above code is same as what in the previos code, i just re-type it again). The output of combined data file is NOT Sorted Well as the Single File one.... How could it be? Here is the picture :
Look at the both pictures... Why the combined data always give me a random numeric ouput data (started from H(Calc), F(Calc), F(Obs), X(Obs), Y(Obs), until Z(Obs)). Is it because of iam using " sort " to my datedefm_table variable and it influence the other variable while in writing process? or maybe i just type an error code....
Really, i cannot create the such of code above Without this matlab community's help before and then here we go, i got an error again^^. And please, i really need your help in solving of my problem.... Because i had already tried so hard to fix this problem but i cant....
Thank you for your attention and cooperation... Iam so grateful if anyone can help my problem...
  댓글 수: 3
Stephen
Stephen 2021년 8월 30일
I don't see any reason why you need to CD to those folders. Changing directories just to read data files is not required and just slows down your code. It is much more efficient and more robust to use absolute/relative filenames:
  • eval(['cd ''' arah ''';']); % avoid this code
  • cd(arah) % much better code
  • Absolute/relative filenames % best code
I agree with dpb: most likely keeping the data in those tables would make it simpler to proccess.

댓글을 달려면 로그인하십시오.

답변(0개)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by