Merge same field of 36 structures
Ältere Kommentare anzeigen
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
Jan
am 7 Mär. 2019
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
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
per isakson
am 7 Mär. 2019
Did you try
xyz = datastruct.S1.long;
Did you inspect datastruct in the Variable Editor?
nlm
am 7 Mär. 2019
Bearbeitet: Walter Roberson
am 7 Mär. 2019
Antworten (1)
per isakson
am 7 Mär. 2019
Bearbeitet: per isakson
am 7 Mär. 2019
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
am 7 Mär. 2019
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
am 7 Mär. 2019
Bearbeitet: per isakson
am 7 Mär. 2019
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
am 7 Mär. 2019
Bearbeitet: per isakson
am 7 Mär. 2019
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'
per isakson
am 7 Mär. 2019
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
Kategorien
Mehr zu Structures finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!