フィルターのクリア

How can I use the data of a set of matrices labelled in order

45 ビュー (過去 30 日間)
Christian
Christian 2024 年 7 月 10 日 20:56
編集済み: Matt J 2024 年 7 月 11 日 12:06
Hi, I managed to open all files in a folder which leaves me with the matrices data_1, data_2, data_3 .....data_n.
Now I want to add one of the row from each of the n matrices into one single set of data but I don't know how i can address the matrix in a for loop.
for i=1:1:n
ag((((i-1)*n)+1):i*ni)=data'i';
end
  4 件のコメント
Walter Roberson
Walter Roberson 2024 年 7 月 10 日 21:58
files = dir('C:\Work\HumenTestsJune24\2023-Taifun-Sula\8-31\31\*.txt');
filenames = fullfile({files.folder}, {files.name});
for k = 1:length(files)
Data{k} = load(filenames{k}, '-ascii');
end
Christian
Christian 2024 年 7 月 11 日 2:30
編集済み: Christian 2024 年 7 月 11 日 2:30
Dear Walter,
thank you, this worked perfectly. I can adress the cells and take it from there!

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

回答 (2 件)

Matt J
Matt J 2024 年 7 月 11 日 0:32
編集済み: Matt J 2024 年 7 月 11 日 0:34
data_1=10;
data_2=20;
data_3=30;
data_4=40;
data_5=50;
Data=arrayfun(@(i)evalin('caller', "data_"+i), 1:5,'uni',0)
Data = 1x5 cell array
{[10]} {[20]} {[30]} {[40]} {[50]}
  2 件のコメント
Stephen23
Stephen23 2024 年 7 月 11 日 4:52
Note that the EVALIN documentation states "In most cases, using the evalin function is also less efficient than using other MATLAB functions and language constructs, and the resulting code can be more difficult to read and debug. Consider using an alternative to evalin."
The simpler and much more efficient alternative is to use indexing.
Matt J
Matt J 2024 年 7 月 11 日 12:06
編集済み: Matt J 2024 年 7 月 11 日 12:06
The point of the answer is not to be efficient, however. It is to undo the damage and convert the data to a form where it can be indexed.

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


Stephen23
Stephen23 2024 年 7 月 11 日 4:50
編集済み: Stephen23 2024 年 7 月 11 日 6:26
"Thank you also for pointing about about dynamic variables, but as a beginner I am lacking options."
You decided to use LOAD, so one option is to read the LOAD documentation. At the bottom of the LOAD page are some links to some related topics**, including to this page:
It shows the recommended approach using indexing. You should use indexing. Indexing is simple, reliable, and efficient. Indexing is a MATLAB superpower!
READMATRIX would likely be a better choice than LOAD. You could also use the same structure returned by DIR:
S = dir('C:\Work\HumenTestsJune24\2023-Taifun-Sula\8-31\31\*.txt');
S = natsortfiles(S); % optional, see text below
for k = 1:numel(S)
F = fullfile(S(k).folder,S(k).name);
S(k).data = readmatrix(F);
end
All of the imported filedata will be in the structure S. This has the benefit that the imported filedata are stored together with corresponding filenames in one array. You can trivially access it using indexing, for example the 2nd file:
S(2).name
S(2).data
If you expect the files to be processed in alphanumeric order (of their filenames) then you could download my function NATSORTFILES and use it to sort the directory S as shown:
** The MATLAB documentation contains a lot of information. The more you practice browsing it, the more you will be able to use it effectively!

カテゴリ

Help Center および File ExchangeData Import and Analysis についてさらに検索

製品


リリース

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by