필터 지우기
필터 지우기

Extract data from a .mat file entred by the user

조회 수: 4 (최근 30일)
Khalala Mamouri
Khalala Mamouri 2020년 9월 2일
편집: Stephen23 2020년 9월 5일
Hello team !
So i am making an app, where the user can load a .mat file of diffrent driving profiles. This means that the user file name can be anything. so we can not specify the name of the file to be loaded and use just
load('WLTC_Driving_Cycle.mat')
So the idea is, to use fileName to get the name what ever is the name of the .mat file, and then load it. here is my code
fileName = uigetfile('*.mat'); % Open window to select .mat file and get the name of the file
load(fileName) % Load the file
Speed = fileName.Data; % << The problem is here, dot call can not be used since "fileName" is char, so i cant import de the data
% Extract Data from the file
for i= 1 : length(speed)
s(1,i) = i-1; % time (s)
s(2,i) = speed(i); % Extract speed
end
As we can see, i am not sure how to extract the data from the file, one idea is to do this :
temp = load(fileName)
speed (1,:) = temp.fileName.Data % Matlab Throws an error since he considers fileName is path to get data

답변 (1개)

Stephen23
Stephen23 2020년 9월 2일
편집: Stephen23 2020년 9월 2일
"...one idea is to do this "
You just need to use dynamic fieldnames:
S = load(...);
S.('somefield')
You can get the fieldnames contained in the structure using fieldnames:
Note that your examples (in your question and various comments) do not reflect the content of the mat file, which contains three scalar structures, none of which have a Data field (all three have the same two fields: MCOS and timeseries).
  댓글 수: 2
Cris LaPierre
Cris LaPierre 2020년 9월 2일
편집: Cris LaPierre 2020년 9월 4일
Data is a property of the timeseries object.
Assuming the *.mat file only has a single variable in it, do the following
fileName = uigetfile('*.mat');
S=load(fileName);
fn = fieldnames(S);
speed = S.(fn{1}).Data
Stephen23
Stephen23 2020년 9월 4일
편집: Stephen23 2020년 9월 5일
Or, again assuming only one variable per file:
C = struct2cell(load(filename));
speed = C{1}.Data;

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

카테고리

Help CenterFile Exchange에서 Develop Apps Using App Designer에 대해 자세히 알아보기

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by