How do I group loaded .txt files on a single array or struct?

1 view (last 30 days)
I'm trying to compare some files' names to group them according to their date. it happens that I have data from 3 different equipment and I'm trying to compare the files' names to build a single array or struct without success.
I loaded the files using:
G = dir('GPS_*.txt');
for n = 1:length(G)
load(G(n).name)
end
H = dir('HOB_*.txt');
for n = 1:length(H)
load(H(n).name)
end
N = dir('NEF_*.txt');
for n = 1:length(N)
load(N(n).name)
end
I have now the files loaded as:
G 12x1 struct
GPS_01122013_1 4846x11 double
GPS_02112013_1 3819x11 double
GPS_02112013_2 3820x11 double
H 12x1 struct
HOB_01122013_1 5226x9 double
HOB_02112013_1 3580x9 double
HOB_02112013_2 3826x9 double
N 13x1 struct
NEF_01122013_1 88x9 double
NEF_02112013_1 63x9 double
NEF_02112013_2 64x9 double
I tried to use strcmp (G.name,H.name) but had no result. I wanna group by date to build a matrix for each day of data.
Thanks in advance for the help

Accepted Answer

dpb
dpb on 4 Dec 2013
Edited: dpb on 5 Dec 2013
That's because the names aren't the same; only the substring date_seqNo portion match--
>> G.name='GPS_01122013_1';
>> H.name='HOB_01122013_1';
>> strcmp(G.name(5:end),H.name(5:end))
ans =
1
>>
If the prefix is always the fixed four characters, the above works ok.
I think I'd approach it a little differently, however, by getting the first list and then processing it sequentially and returning the other files for the same date/sequence number.
Sotoo (caution, aircode)...
G = dir('GPS_*.txt');
for ix=1:length(G)
[~,nam]=fileparts(D(ix).name); % the name portion only
dte=nam(5:end); % date string/sequence substr
D=dir(['*' dte '.txt']); % get the three files that go together
for jx=1:length(D) % and process them as a group as desired
...
Will want to check that D is the right length and such niceties, of course, but that should simplify the problem I'd think...
ADDENDUM:
Actually, now that I think of it, since you know the prefix for the three types, once you've got G then you don't need to do a dir() for the others at all. Just process them by creating the names directly from the one by replacing the first three characters with the other two prefix strings sequentially.
  2 Comments
Luiz Wiese
Luiz Wiese on 5 Dec 2013
Thanks for the help dpb, that gave me a new look to the problem, but after trying your solution and a few other i thought of i have a new problem . When I tried to use:
[~,nam]=fileparts(D(ix).name);
It ends in an error saying that
Undefined variable "D" or class "D".
Error in Circuito_1 (line 22)
[~,nam]=fileparts(D(ix).name);
I'm sorry if this is a simple problem but i'm extremely new to MatLab and still don't know all the tools.
Thanks.
dpb
dpb on 5 Dec 2013
Oh, sorry, that was a typo (remember I did warn "aircode" which means it's typed at the terminal, not tested :) ).
I use D as the return structure name for a dir() call so much it was just automagic to type it there. That would be 'G' in your usage (or any other one of the base file names as a reference would serve as well).

Sign in to comment.

More Answers (0)

Categories

Find more on Environment and Settings 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!