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

1 view (last 30 days)
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 Comments
Tyann Hardyn
Tyann Hardyn on 29 Aug 2021
Edited: Tyann Hardyn on 29 Aug 2021
@dpb Thanks for your answer, Sir. But i already found the problem in the link below :
Yes Sir, Iam really sorry, but My matlab experience is just began, i used to be a web-based programmer (html, php, etc), and iam not familiar with data-based programming. My purpose of this program is to create some of sorted combined data output (as we select from multiselect) and then plot the data from the selected data versus time series which re already provided inside of the selected datas.
And by looking from my data and codes, i already found my mistake. The mistake is come from the selected data of multifile function. The data i select is just random but with the same type (for instance :
By selecting randomly all of the three datas i attached to this link, then matlab would process a file by the order of our selection data. In this case, the
data file would be processing first. So the solution is Sort the namafiles input from uigetfile like this :
[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((sort(namafile)); %takes care of case only one file was selected
%and also sort the data input we select from open dialogue
So that the file would be processed by the right order : tun201703dmin.txt, tun201704dmin.txt, and then tun201705dmin.txt.
Thats the sorted files i want....
And yeah, you re right, after i added "sort" to namafile variable, then i removed the "sort" in order to place the DATE column in the right position like in the original data.
Thanks for your explanation. It would definitely add my references and knowledge about Matlab.
Stephen23
Stephen23 on 30 Aug 2021
Edited: Stephen23 on 30 Aug 2021
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.

Sign in to comment.

Answers (0)

Categories

Find more on Shifting and Sorting Matrices 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!