フィルターのクリア

Storing variables from an array loop

1 回表示 (過去 30 日間)
Giuseppe
Giuseppe 2015 年 1 月 16 日
コメント済み: Giuseppe 2015 年 1 月 17 日
Hello,
I'm coding to import several .txt files in matlab and create as many matrix as the number of file. I created the basic loop but I get stuck when it comes to store my dataArray without keeping overwriting it during the loop.
Here the code that I'm trying to work on to obtain new matrix in the workspace for each loop.
delimiter = '\t';
startRow = 2;
formatSpec = '%f%f%f%f%f%f%[^\n\r]';
for k = 1:3
% Read the file.
textFileName = ['SJ' num2str(k) '.txt'];
if exist(textFileName, 'file')
fid = fopen(textFileName, 'rt');
dataArray = textscan(fid, formatSpec, 'Delimiter', delimiter, 'EmptyValue' ,NaN,'HeaderLines' ,startRow-1, 'ReturnOnError', false);
fclose(fid);
else
fprintf('File %s does not exist.\n', textFileName);
end
%Create matrix
GRFdata = [dataArray{1:end-1}];
end
The issue is that GRFdata keeps overwriting whilst I would obtain GRFdata1, GRFdata2, etc.
Anyone could point me out the right way to proceed, please? Thanks
  4 件のコメント
Stephen23
Stephen23 2015 年 1 月 17 日
Basically you should not do this. Using dynamically defined variable names or encoding data within the variable name is a pretty bad idea in MATLAB, as is described on many threads on MATLAB Answers:
The first of these links gives an excellent alternative, which is what you should probably be using for your data: structures . In particular you can dynamically assign the field names, which is a much neater solution than dynamically defining variable names.
Giuseppe
Giuseppe 2015 年 1 月 17 日
Stephen, I'm going to have a look to these documents. Thanks

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

採用された回答

Guillaume
Guillaume 2015 年 1 月 16 日
編集済み: Guillaume 2015 年 1 月 16 日
While you can dynamically create variable names on the fly, it's almost never a good idea. You lose syntax checking, compiler optimisation, ease of debugging, ease of understanding the code, etc.
The best way is to store your various matrices in a cell array:
GRFdata{k} = [dataArray{1:end-1}];
Referring to these matrices afterward is simply:
m = GRFdata{n}; %replace n by the index of the matrix you want to use
If you really want to use different variable names, use eval:
eval(sprintf('GRFdata%d = [dataArray{1:end-1}]', k)); %eugh!
Accessing these matrices is then:
m = eval(sprintf('GRFdata%d', n)); %eugh!
  1 件のコメント
Giuseppe
Giuseppe 2015 年 1 月 16 日
Thanks for pointing out options

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

その他の回答 (1 件)

Giuseppe
Giuseppe 2015 年 1 月 16 日
GRFdata{k} = [dataArray{1:end-1}];
is actually an easy way, with this I get all my file data for each cell. Example in the attached screenshot.
Do I need an other loop the generate different matrix for each cell of the array ? How should I actually index this:
GRF1 = [GRFdata{1:1}]; GRF2 = [GRFdata{2:2}]; GRFn = [GRFdata{n:n}];
  2 件のコメント
Stephen23
Stephen23 2015 年 1 月 17 日
Don't do this. Just keep them in a cell array or structure. Dynamically defining variable names is poor coding practice.
Giuseppe
Giuseppe 2015 年 1 月 17 日
編集済み: Giuseppe 2015 年 1 月 17 日
Stephen, where can I find some basic documentation about the right way (good coding practice)to import data and store it for subsequent processing? Have you got anything to point me out, please? Books, chapters, tutorials, etc. are welcome.
Unfortunately, I do not have a strong computing background but I'm keen to learn.

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

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by