フィルターのクリア

How to load selected columns in a for loop?

4 ビュー (過去 30 日間)
Joe
Joe 2024 年 4 月 15 日
編集済み: Shivani 2024 年 4 月 17 日
I am trying to load a lot of data. I wrote a for loop to load the .mat files, but I only want certain columns from the files. The columns I need to load do not change between the data sets.
For example, how do I load columns 2, 5, 200, etc inside the loop?
for i= [1:5 7:156]
for ii = [6 157:244]
load(['LL_00' num2str(i) '.mat'])
load(['LL_0' num2str(ii) '.mat'])
end
end
  1 件のコメント
Mathieu NOE
Mathieu NOE 2024 年 4 月 15 日
simply load all the data then select which cols you want
col_selected = [2;5;200];
data_selected = data(:,col_selected);

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

回答 (1 件)

Shivani
Shivani 2024 年 4 月 17 日
編集済み: Shivani 2024 年 4 月 17 日
Hello @Joe,
Please note that in MATLAB .MAT files can contain multiple variables of various sizes, so loading specific columns would require knowing the variable names inside those .MAT files.
To load specific columns from variables within .MAT files, you would first load the file and then extract the columns you need from the loaded variables. I assuming that the variable from which you are looking to extract columns to be 'data’ in the code snippet below.
columns_to_extract = [2, 5, 200];
for i = [1:5, 7:156]
loaded_data_i = load(filename_i);
specific_columns_i = loaded_data_i.data(:, columns_to_extract);
end
I hope this helps!

カテゴリ

Help Center および File ExchangeWorkspace Variables and MAT-Files についてさらに検索

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by