merging workspace from different .mat files
12 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Stephen23
2021-10-27
编辑:Stephen23
2021-10-27
"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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Workspace Variables and MAT-Files 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!