Assign variables while importing data
17 ビュー (過去 30 日間)
古いコメントを表示
I have a data set with multiples files, and there are 2 coloumns in each file. I have imported the files using the following code:
files = dir('*.txt');
for i=1:length(files)
eval(['load ' files(i).name ' -ascii']);
end
I have the data in my workspace now but I need to assign variables to each coloumn. Furthermore, I have to use those variables in a function to normalize all the data at the same time and plot in a single graph.
Is there any approach I can take to assign variables so that the code goes through each file one by one, and take the assigned variables to give answer for all dataset at once.
function [zero,norm,ramanzeronorm] = zeronorm(wavenumber,rm)
My variables are wavenumber and rm.
Any help will be appreciated.
0 件のコメント
採用された回答
Stephen23
2020 年 1 月 27 日
編集済み: Stephen23
2020 年 1 月 28 日
Do NOT use eval for importing data, unless you intentionally want to force yourself into writing slow, complex, buggy code. Read this to know why:
Instead you should import the data into a matrix, optionally assigning it to one array using indexing, for example using a cell array as the documentation examples show:
Or using the same structure returned by dir:
S = dir('*.txt');
for k = 1:numel(S)
M = dlmread(S(k).name); % better than LOAD.
... do whatever with matrix M, e.g.:
wn = M(:,1); % wavenumber
rn = M(:,2); % rn
...
S(k).data = M; % optional: store any data you want for later.
end
Then you can trivially access the data in that structure, e.g. using loops or indexing:
For example, you can easily vertically concatenate all of the file data together:
alldata = vertcat(S.data);
2 件のコメント
Stephen23
2020 年 1 月 28 日
編集済み: Stephen23
2020 年 1 月 28 日
You can store any data you want in the structure, e.g.:
for ...
...
S(k).mycolumn = whatever column of data you want to store;
end
mymat = [S.mycolumn];
What size mymat is depends entirely on how often the loop iterates and on the size of each column: if each column you store has size 1024x1 and it iterates five times, then mymat will have size 1024x5.
Read more about how this works:
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Structures についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!