Load specific column from mat file

조회 수: 13 (최근 30일)
Lucas Maiß
Lucas Maiß 2017년 11월 20일
댓글: Jan 2017년 11월 20일
Hello, I got a mat file with a struct of different variables. Each variable got a 1X5 struct entry. MAT File:
a = [0 0 0 0 1]
b = [0 0 0 1 0]
c = [0 1 0 0 1]
My simulink model got different variants and each column of the matfile belongs to a variant. So I would like to load that mat file with only one column. Workspace:
a = 1
b = 0
c = 1
Is that possible? Greetings Lucas

답변 (2개)

KSSV
KSSV 2017년 11월 20일
a = a(end) ;
b = b(end) ;
c = c(end) ;

Jan
Jan 2017년 11월 20일
This is not possible in one command, but with a tiny function:
function [a,b,c] = LoadColumn(FileName, col)
Data = load(FileName);
a = Data.a(col);
b = Data.b(col);
c = Data.c(col);
end
This might look trivial - in fact, it is. Does it solve your needs? If not, what is the remaining problem?
  댓글 수: 4
Lucas Maiß
Lucas Maiß 2017년 11월 20일
That look good. Could you explain me what you are doing here: @(v)v(end)
Jan
Jan 2017년 11월 20일
@Lucas: @(v)v(end) is an anonymous function, which replies the last element of the input.
With a loop:
function S = LoadColumn(FileName, col)
S = load(FileName);
Field = fieldnames(S);
for iField = 1:numel(Field)
F = Field{iField};
S.(F) = S.(F)(end);
end
end

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by