Importing variables from a txt or xlsx file

3 ビュー (過去 30 日間)
amberly hadden
amberly hadden 2015 年 8 月 20 日
コメント済み: amberly hadden 2015 年 8 月 20 日
Hello everybody- I'm trying to ready some text/xlsx files from a folder and want to keep the variables from original file to output them in Matlab workspace for example (data-1, data2, data3 and data4 are my txt files) and for example
Data-1
a b c
1 2 2
4 5 6
0 0 1
for i = 1:4
fileName = ['data' num2str(i)];
dataStruct.(fileName) = load([fileName '.txt']);
end
Can I modify this code in a way that when I import let say data-1 using the above loop instead of importing one matrix it should import all of its variables a b c in work place so that I can use them to proceed for my work? so loop will be done after processing one file and will move to the next file data-2 read variables and so on
Thank you,
Amb

採用された回答

Walter Roberson
Walter Roberson 2015 年 8 月 20 日
It is possible, yes, but only using a function that has been declared obsolete for years ("textread")
If you have R2013b or later you should consider using readtable()
Otherwise just split up the array after you read it.
  2 件のコメント
amberly hadden
amberly hadden 2015 年 8 月 20 日
Thanks walter- now I want to link data in following loops close all; clear;
files = dir( '*.xlsx' ) ;
nFiles = length( files ) ;
names = cell( nFiles, 1 ) ;
data = cell( nFiles, 1 ) ;
for dId = 1 : nFiles names{dId} = files(dId).name ; fId = fopen( files(dId).name, 'r' ) ; cols = textscan( fId, '%f %f', 'headerlines', 1 ) ; data{dId} = [cols{:}] ; fclose( fId ) ; end
for dId = 1 : length( data )
fprintf( '\nFile: %s\n', names{dId} ) ;
disp( data{dId} ) ;
end
data=readtable('data.xlsx'); % at this point I would like to read data saved in files and then output parameters for each data file using loop below
for i=1:width(data); x = data.Properties.VariableNames(i); eval(sprintf('%s = data.%s', x{1}, x{1})); end I hope you can give me some suggestions here- Thanks
amberly hadden
amberly hadden 2015 年 8 月 20 日
So here are more details I have list of files name with .xlsx extention from code above all I want is to read table to read names from list while each file is present in directory and retrieve variables something like this
for i = 1:length(names)
D(i) = names(i,:)';
end
C = D(:,1);
for i = 1:length(names)
A(i) =readtable(C(i));
end
for i=1:width(data); x = data.Properties.VariableNames(i); eval(sprintf('%s = data.%s', x{1}, x{1})); end
but its giving error input must be row vector of charactrers

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLive Scripts and Functions についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by