Merge same field of 36 structures

I want to merge same field (e.g., long which is a matrix) from 36 structures of lenghth (266) which has 20 fields for each structure For example, I have structures S1, S2, S3, S4, S4 ... S36 etc of length varying between 266 to 306 with same field names lat, long, date, temp, air etc which are matrices of variable length varying from [1500 to 15000 by 7]. I want to merge S1 to S36 of field "long" and "date", i.e., S = [S1.long, S2.long, ...].The error I receive for the below code is, "Reference to non-existent field 'long'.
numfiles = length(filenames);
results = cell(numfiles, 1);
U1 =[];
for K = 1 : numfiles
thisfile = filenames{K};
datastruct = load(thisfile);
xyz = datastruct.long; %extract particular variable
processed_xyz = vertcat(U1,xyz); %call appropriate function to handle the data
results{K} = processed_xyz;
end
This is suggested by a matlab user, however when I implement it it does not recognize the long field in the datastruct structure.

4 Kommentare

What does "merge" mean? What exactly is the wanted output? What does "it it does not recognize the long field" mean? Do you get an error message? Then please be so kind to share it with trhe readers. We cannot run the code due to the lack of inputs.
vertcat(U1,xyz) is free of use, when U1 is the empty matrix []. Please take the time to explain, which class and size the inputs are, and what do you want as output. The current code can be abbreviated:
numfiles = length(filenames);
results = cell(numfiles, 1);
for K = 1 : numfiles
datastruct = load(filenames{K});
results{K} = datastruct.long;
end
per isakson
per isakson am 7 Mär. 2019
Maybe you want to try to fix your code with the help of: Debug a MATLAB Program and Examine Values While Debugging
Did you try
xyz = datastruct.S1.long;
Did you inspect datastruct in the Variable Editor?
nlm
nlm am 7 Mär. 2019
Bearbeitet: Walter Roberson am 7 Mär. 2019
This works,
xyz = datastruct.S1.long;
However, I want to run it in loop for 36 STRUCTURES.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

per isakson
per isakson am 7 Mär. 2019
Bearbeitet: per isakson am 7 Mär. 2019

0 Stimmen

Try this
%%
numfiles = length(filenames);
results = cell(numfiles, 1);
for K = 1 : numfiles
datastruct = load( filenames{K} );
U1 = zero( 0, 7 );
for jj = 1 : 36
U1 = vertcat( U1, datastruct.(sprintf('S%d',jj)).long ); %#ok<AGROW>
end
results{K} = U1;
end

7 Kommentare

nlm
nlm am 7 Mär. 2019
I get an error as "Reference to non-existent field S"
per isakson
per isakson am 7 Mär. 2019
Bearbeitet: per isakson am 7 Mär. 2019
Not with my code, I assume
>> jj = 35;
>> sprintf('S%d',jj)
ans =
'S35'
What code throws that error?
Maybe you want to try to fix your code with the help of: Debug a MATLAB Program and Examine Values While Debugging
nlm
nlm am 7 Mär. 2019
Bearbeitet: per isakson am 7 Mär. 2019
%%
files = length(filenames);
results = cell(numfiles, 1);
for K = 1 : numfiles
datastruct = load( filenames{K} );
U1 = zero( 0, 7 );
for jj = 1 : 36
U1 = vertcat( U1, datastruct.(sprintf('S%d',jj)).long ); %#ok<AGROW>
end
results{K} = U1;
end
The datastruct loads only one structure, and not all the structures in the folder. why is that ? Though filenames{K} is a cell of length 36
per isakson
per isakson am 7 Mär. 2019
Bearbeitet: per isakson am 7 Mär. 2019
What does
S = whos( '-file', filenames{1} )
output?
"Reference to non-existent field S" Report the full error message
"why is that" read the documentatio n on load
"Though filenames{K} is a cell of length 36" No, it's the name of a file.
nlm
nlm am 7 Mär. 2019
Bearbeitet: per isakson am 7 Mär. 2019
whos( '-file', filenames{1} )
Name Size Bytes Class Attributes
S1 248x1 10140287550 struct
"Reference to non-existent field S" This is the full message
"why is that" read the documentation on load : I'll refer
"Though filenames{K} is a cell of length 36" : Yes it is the name of the file, I meant length of K
per isakson
per isakson am 7 Mär. 2019
Bearbeitet: per isakson am 7 Mär. 2019
I now assume that
whos( '-file', filenames{2} )
will output S2, et cetera
"This is the full message" IMO: it's much easier to debug functions than scripts.
I assume that the error was caused by jj being empty
>> jj = [];
>> sprintf('S%d',jj)
ans =
'S'
Maybe, this works
function results = ccsm( filenames )
%%
numfiles = length(filenames);
results = cell(numfiles, 1);
for K = 1 : numfiles
datastruct = load( filenames{K} );
U1 = zero( 0, 7 );
for jj = 1 : length( datastruct.(sprintf('S%d',K) ) )
U1 = vertcat( U1, datastruct.(sprintf('S%d',K))(jj).long ); %#ok<AGROW>
end
results{K} = U1;
end
end

Melden Sie sich an, um zu kommentieren.

Kategorien

Gefragt:

nlm
am 7 Mär. 2019

Kommentiert:

am 7 Mär. 2019

Community Treasure Hunt

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

Start Hunting!

Translated by