combine a number of files together? cat,vertcat and horzcat do not work...

Hey all!
I have 254 files named blah_blah_001 until blah_blah_254. Each file has 723 x 3127 dimension and I want to combine all to create a 723 x 3127 x 254 matrix. I have tried loops but I am not sure how to write so that each file number is with 3 digits. Here is my attempt:
all = NaN(723,3127,254);
% attempt to put all arcs together..
for i=1:254;
for arc = 001:254;
all(:,:,i) = [TJJ_arc_(arc)]
end;
end;
After this I wish to calculate a nanmean to get 723 x 254...
Can someone help me?

2 件のコメント

Azzi Abdelmalek
Azzi Abdelmalek 2014 年 7 月 24 日
What type of files?
Michael
Michael 2014 年 7 月 24 日
They are included in a single .mat file. For example, blah.mat is loaded with all 254 files...

サインインしてコメントする。

 採用された回答

Geoff Hayes
Geoff Hayes 2014 年 7 月 24 日
編集済み: Geoff Hayes 2014 年 7 月 24 日
Use sprintf to create the file name on each iteration
allData = NaN(723,3127,254);
for k=1:254
% create the file name
filename = sprintf('TJJ_arc_%03d.mat',k);
% load the data
data = load(filename);
% save the data
allData(:,:,k) = data;
end
NOTE that all is a built-in MATLAB function so your code should avoid using this as a variable name.
NOTE also how I replaced your iterator i with k. i and j are used in MATLAB as the representation of the imaginary number, so it is good practice to avoid using them in for loops.
Try the above and see what happens!

4 件のコメント

Michael
Michael 2014 年 7 月 25 日
Thanks Geoff, however, it doesn't seem to work exactly. It complains that 'Error using load. Unable to read file TJJ_arc002: No such file or directory.'.
I tried removing .mat but it didn't work. Do you know what I should change? maybe it's because the files are all loaded into one .mat file and do not have the .mat extension?
Geoff Hayes
Geoff Hayes 2014 年 7 月 25 日
Michael - what do you mean by the files are all loaded into one .mat file? In your question, you stated that there were 254 files with a prefix and a 3 digit suffix. Do you really mean that you have 254 (723 x 3127) variables all loaded into a single mat file?
Michael
Michael 2014 年 7 月 25 日
Yes I did, sorry for the confusion. I realised later after submitting the question. I managed to sort it like so:
adata = NaN(723,3127,254);
for k=1:254
k
tic
% create the file name
filename = sprintf('TJJ_arc%03d',k);
load('TJJ.mat',filename);
a = eval(filename);
% load the data
%data = load(filename);
% save the data
adata(:,:,k) = a;
toc
clearvars -except adata
end
Thanks for your help though, Geoff! you put my in the right direction.
Geoff Hayes
Geoff Hayes 2014 年 7 月 25 日
Cool. You may want to change the above filename variable to varname so that it is clear that a variable named whatever is being loaded rather than a file.

サインインしてコメントする。

その他の回答 (1 件)

Michael
Michael 2014 年 7 月 25 日

0 投票

Thanks Geoff, however, it doesn't seem to work exactly. It complains that 'Error using load. Unable to read file TJJ_arc002: No such file or directory.'.
I tried removing .mat but it didn't work. Do you know what I should change? maybe it's because the files are all loaded into one .mat file and do not have the .mat extension?

カテゴリ

ヘルプ センター および File ExchangeFile Operations についてさらに検索

質問済み:

2014 年 7 月 24 日

コメント済み:

2014 年 7 月 25 日

Community Treasure Hunt

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

Start Hunting!

Translated by