How to concatenate different rows
Mostra commenti meno recenti
Hello, i have some trouble doing my project. I have 2 different mat file (skak1.mat with 268x1000000 dimension and skak2.mat with 63x1000000 dimension) I want to concatenate 2 files above. Anyone can help me please?
5 Commenti
Rik
il 2 Apr 2020
I don't see what changed between the original question and your current text. Did my answer help you? Do you understand it? If not, feel free to post a comment.
Della N
il 3 Apr 2020
I'm sorry too late to answer. I hv tried to load variable and then make as new variable but in the end they just keep 2 file above in new variable it doesnt make them concatenated. I want concatenated 2 file above become 'new skak.mat' with 331x1000000 dimension. I hv tried some code but doesnt work. Here's the some code :
pathname = 'E:\Matlab\skak' %there's an error they cant load filestoload
filestoload = dir(fullfile(pathname,'*mat'));
filescontent = cell(size(filestoload));
for fidx = 1:numel(filestoload)
m = load(filestoload{fidx});
filescontent{fidx} = m.(filestoload{fidx});
end
[nrows, ncols] = cellfun(@size, filescontent);
if all(nrows == nrows(1)) %same number of rows
concatenatedfiles = horzcat(filescontent{:});
elseif all(ncols == ncols(1)) %same number of columns
concatenatedfiles = vertcat(filescontent{:});
else
error('no commn dimension');
end
save ('baru pol.mat','concatenatedfiles');
PathName = uigetdir; %there's an error using vercat they said
file_all = dir(fullfile(PathName,'*.mat'));
matfile = file_all([file_all.isdir] == 0);
clear file_all PathName
x=[];
for i=1:length(matfile)
x=[x; load(matfile(i).name)];
end
b=[];
for j=1:length(x)
b=[b; x(j, 1).variable];
end
save ('new skak.mat','matfile','x','b');
Della N
il 3 Apr 2020
and last code i hv tried is here, but in the last code i haven't tried because my pc suddenly always turn off when i tried.
a1=load('skak1.mat');
f1=fieldnames(a1);
a2=load('skak2.mat');
f2=fieldnames(a2);
v=[a1.(f1{1});a2.(f2{1})];
save('new skak.mat','v');
Rik
il 3 Apr 2020
Just to have the situation clear:
You have two files 'E:\Matlab\skak1.mat' and 'E:\Matlab\skak2.mat'. The first contains a variable called skak1 (268x1000000) and the second contains a variable called skak2 (63x1000000). Your goal is to have a mat file 'E:\Matlab\new skak.mat' that contains a variable skak_new (331x1000000).
Is that all correct?
Della N
il 3 Apr 2020
yes alright sir.
Risposte (1)
You can't concatenate files. (Technically you can, but showing how you could do it will not be helpful for you)
What you need to do is load the two matrices with the load function, after which you can concatenate them as any other variable.
pathname = 'E:\Matlab'
S=load(fullfile(pathname,'skak1.mat'));
skak1=S.skak1;
S=load(fullfile(pathname,'skak2.mat'));
skak2=S.skak2;
skak_new=[skak1;skak2];
save(fullfile(pathname,'new skak.mat'),'skak_new');
As long as everything fits in memory the above code should do what you need. That might be your main issue, since the result will require about 2.5 GB of contiguous memory (so 5 GB in total). If this is a problem for you, please comment below and I will try to help you with a way to write to mat files without loading everything in memory. For that it is important to know which release of Matlab you are using.
8 Commenti
Della N
il 3 Apr 2020
after I try your code there's an error.
Reference to non-existent field 'skak1'.
Error in terbaru (line 44)
skak1=S.skak1;
Rik
il 3 Apr 2020
That means skak1.mat does not contain a variable with the name skak1. You should change the field name to the variable name that is in that mat file.
Della N
il 3 Apr 2020
pardon me, i didnt understand what you mean. If you would please give me some example how to do. because in my file there's no variable name in it, there are only numbers of number with 268x1000000 and 63x1000000 dimension.
Rik
il 3 Apr 2020
Yes there is. A mat file doesn't just contain values, it contains variables. If you run the code below, what do you see?
pathname = 'E:\Matlab'
S=load(fullfile(pathname,'skak1.mat'));
fieldnames(S)
Della N
il 3 Apr 2020
this is the answer. the code right is 'skak1=S.feat' is't right?
'E:\Matlab'
ans =
1×1 cell array
{'feat'}
Rik
il 3 Apr 2020
Yes, that is the code you should use.
Della N
il 4 Apr 2020
I hv tried the new code but my pc are getting slower and cant do anything, but still running the code. What should i do? Waiting till end or i can try another choice? Thankyou.
Rik
il 4 Apr 2020
You are probably near the maximum variable size your computer can handle. You can check in your task manager whether the RAM is almost full and whether the disk is active because of page swapping. If it takes more than about 15 minutes I would expect something to be wrong.
I think another conclusion is that your computer is not suitable for working with this data size.
Categorie
Scopri di più su Functions in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!