フィルターのクリア

How do I obtain size data from multi loaded text files?

11 ビュー (過去 30 日間)
Brad
Brad 2014 年 6 月 10 日
コメント済み: Brad 2014 年 6 月 16 日
I'm loading up a pair of text files (from the working directory) containing numerical data with the following code;
files = dir('*.txt');
for i=1:length(files)
eval(['load ' files(i).name ' -ascii']);
end
Is there a way to obtain the size dimensions (for each text file) using this approach within the for loop?

採用された回答

dpb
dpb 2014 年 6 月 10 日
編集済み: dpb 2014 年 6 月 11 日
Well, I misspoke on eval; to use the command-line form the filename can't be in a variable so that's the only way to do it.
What you really likely ought to do is switch over to using the functional form for load and also assign the result to a variable so you know the name.
The problem is that when you use the default assignment as done here, load "poofs" the variable name from the file name base name into the workplace(). That's ok for interactive use but difficult to handle easily programmatically in a script or function as then the only way to get access to that variable is either thru *eval or, perhaps a named structure field.
The latter would be something like...
[~,fn]=fileparts(files(i).name); % get the base name from the file name
s.(fn)=load(files(i).name,'-ascii'); % load a structure w/ field that name
[nr,nc]=size(s.(fn)); % and find out how big 'tis
Other than that you're at the mercy of eval and that's no road down which to travel..
See the documentation on structures in the 'Data Types' section for more details on using the dynamic name fields. Basically, the deal is if enclose a field name in paren's, that causes the variable to be evaluated and the value thereof to be used as the field name. Same idea as building a string for eval but much less error-prone.
() Even that isn't strictly true always, depending on the file name. There are a bunch of exceptions for filenames that include numerics or other special symbols so that you can't always get the imported variable name at all easily programmatically. See the doc for *load for details, but it isn't pretty...
  1 件のコメント
Brad
Brad 2014 年 6 月 16 日
Had to use the load function as you suggested. Working with legacy code does have its occasional pit fall. Thanks for the input.

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

その他の回答 (1 件)

dpb
dpb 2014 年 6 月 10 日
編集済み: dpb 2014 年 6 月 10 日
files = dir('*.txt');
for i=1:length(files)
eval(['load ' files(i).name ' -ascii']);
No need for eval here, just write
load file(i).name '-ascii';
As to the question, depends on what you mean by size as to how.
files(i).bytes
contains the disk storage of the file, the size function returns the size of the arrays in Matlab storage. There's also
doc whos % not optional parameters for various uses
I don't understand the question title of "multi-loaded" text files?
  1 件のコメント
Brad
Brad 2014 年 6 月 10 日
dpb, sorry about the confusion. I'm looking to obtain the number of columns and rows for each text file loaded by the load function.
And thanks for the heads up on eval.

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

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by