Read data from matlab struct and merge

4 ビュー (過去 30 日間)
Curious Mind
Curious Mind 2020 年 6 月 29 日
回答済み: Love Matlab 2020 年 6 月 29 日
Hello, please I have multiple .mat files in a folder. Each one is a matlab struct called T and each T contains contains 2 files, p and s. 1. I want to load all the .mat files into matlab workspace, extract and concatenate only the second columns of all p in each of the files into a single matrix called M. 2. Extract and concatenate or merge the second row of all the s of all the files into a matrix called G.
  4 件のコメント
Love Matlab
Love Matlab 2020 年 6 月 29 日
編集済み: Love Matlab 2020 年 6 月 29 日
what do you get when you do : load('FILE.mat') ? when you load each .mat file are you getting a structure or just a N x M matrix?
Curious Mind
Curious Mind 2020 年 6 月 29 日
It comes out as a structure. Within the structure is a p and s

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

回答 (1 件)

Love Matlab
Love Matlab 2020 年 6 月 29 日
Try this. I am still confused about what you exactly what to extract. The cells P, and S after the for loop should contain all P and S data.
%% Select Folder with .mat files
folder = uigetdir(pwd) ; % select folder with images
files = dir(fullfile(folder,'*.mat')); %create structure with folder data
S = cell(1,length(files)); %create empty storage cells
P = cell(1,length(files));
for i = 1:length(files)
name = files(i).name ; %filename
x = load(name); %load.mat file
P{1,i} = [x.p]; %save all p data
S{1,i} = [x.s]; %save all s data
end
allP = cell2mat(P); %change to matrix
M = allP(2:2:end); %extract every 2nd variable (second column)
allS = cell2mat(S); %change to matrix
G = allS(2,:); %extract only the second row

カテゴリ

Help Center および File ExchangeStructures についてさらに検索

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by