merging workspace from different .mat files
9 views (last 30 days)
Show older comments
HI all,
I'm very new to Matlab im trying to combine some data together. as the data is in serveral different files which each represent a different user which i wish to ultimatly compare I think the problem ive run into is my files contain the same variable names so i cant load them form different .mat files at the same time so i think i either need to rename all of the variables in the file from say altitude to user1_altitude but there is a lot so would need to automate this as each file contains approximatly 200 columns airspeed altitude etc.. and each of those contaings value of 3271x1 alternatively could i combine 2 users data so i can directly compare each persons performance
user 1 user2 = combine user
altitude 3271x1 altitude 3271x1 altitude 3271x2
airspeed 3271x1 airspeed 3271x1 airspeed 3271x2
i hope this makes sence so i should end up being able to graph multiple users at the same time
thank you
tim
0 Comments
Accepted Answer
Stephen23
on 27 Oct 2021
Edited: Stephen23
on 27 Oct 2021
"I think the problem ive run into is my files contain the same variable names..."
That is not a problem at all, actually that is very good news.
"...so i cant load them form different .mat files at the same time..."
Yes, you can. In fact, having the exactly the same names in each file makes your code simpler and much more robust.
"...so i think i either need to rename all of the variables in the file from say altitude to user1_altitude..."
Ugh, do NOT do that.
Forcing meta-data (e.g. user IDs) into variable names is a sign that you are doing something wrong.
Numbering variables like that is also a sign that you are doing something wrong. Trying to access lots of numbered variables is one way that beginners force themselves into writing slow, complex, buggy code:
You did not give much information about your filenames, etc. but something like this should get you started:
P = 'absolute or relative path to where the files are saved';
S = dir(fullfile(P,'*.mat'));
for k = 1:numel(S)
F = fullfile(P,S(k).name);
S(k).data = load(F);
end
T = [S.data]; % this requires that all MAT files have the same variable names
All of the file data is stored in the structure array T, which you can easily access using indexing and fieldnames:
For example, the second file:
S(2).name % filename
T(2).airspeed
T(2).altitude
"alternatively could i combine 2 users data so i can directly compare each persons performance"
Of course, you can trivially concatenate the any of the imported data into numeric matrices, just as you request:
idx = [2,5]: % the indices of the users that you want to compare
air = [T(idx).airspeed]
alt = [T(idx).altitude]
You can also loop over the fieldnames to process them all:
Your approach (numbering all of the variables) would make these tasks slower and much more complex.
More Answers (0)
See Also
Categories
Find more on Whos 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!