Extract data using variable names

조회 수: 11 (최근 30일)
Rodrigo
Rodrigo 2025년 2월 12일
댓글: Rodrigo 2025년 3월 5일
Hello Matalab Community.
Let me ask your help to find out a way to improve porcessing my data.
I have files with lots of variables, data Type is structure.
I want to analyze data, let's say I want to review VSPD so I used below commadns:
%extract data, convert to double
V_Time = VSPD.time;
V_VSPD = VSPD.signals.values;
Now I can easly use double data to plot it or do something else.
Becasue I got several files to go through and saveral variables, I'm trying to create a code to make this process faster:
clear all; % Clears all memory
clc; % Clears the command window screen
fprintf('select folder containing data files\n');
currentpath=pwd; %%Identify current folder
[FileName,PathName] = uigetfile('@*.mat','Select the .dat-file(s)','MultiSelect','on');
if (PathName == 0)
fprintf('Data directory was not selected...script will be terminated\n\n');
return
end
cd(PathName);
files=dir('@*.mat');
num_files=length(files(:,1));
for i=1:num_files
fprintf('Progress: %d/%d\n\n',i, num_files)
load(files(i,1).name)
end
FileName = files.name;
PathName = files.folder;
if isnumeric(FileName)
return
end
FileName = cellstr(FileName);
V = { ...
'VSPD'...
};
N = numel(FileName);
my goal is to create a list of the variables that I want to use(extract) for my analysis in this case V
Then use V to somehow create V_Time = VSPD.time and V_VSPD = VSPD.signals.values;
A = cell2mat(V);
B= '.time';
C= '.signals.values';
AA = [A C];
BB = convertCharsToStrings(C);
CC = BB;
%%V_Time = A + C ?????
any feedback will be highly appreciated

채택된 답변

Stephen23
Stephen23 2025년 2월 12일
편집: Stephen23 2025년 2월 12일
The most important change is to always LOAD into an output variable (which is a scalar structure):
S = load(..);
Also avoid CD in code (using absolute/relative filenames is more robust and much more efficient).
Why are you using UIGETFILE to select files... which you then completely ignore by using DIR to match all filenames? It makes your intent very unclear. Using UIGETDIR makes the intent much clearer.
P = '.'; % uigetdir();
if isnumeric(P)
fprintf('Data directory was not selected...script will be terminated\n\n');
return
end
S = dir(fullfile(P,'@*.mat'));
for k = 1:numel(S)
F = fullfile(P,S(k).name);
D = load(F);
C = struct2cell(D);
S(k).meta = fieldnames(D);
S(k).data = vertcat(C{:});
end
The file data are all stored in the structure S which is trivial to access in a loop, for example the 1st file:
S(1).name; % filename
S(1).meta; % VSPD, etc.
S(1).data; % file data
If all of the imported data structures have the same fields, then you can easily concatenate them together:
C = vertcat(S.meta)
C = 8x1 cell array
{'VSBART' } {'VSBART_RT' } {'VSBART_RT_PD'} {'VSBART_RT_PU'} {'VSFDC' } {'VSPD' } {'VS_RATE' } {'VS_RATEPH' }
D = vertcat(S.data)
D = 8x1 struct array with fields:
Channelname Channeltype unit timeunit period device time signals min max
which makes accessing your data trivial a loop, for example the 1st data structure:
T = D(1).time;
V = D(1).signals.values;
plot(T,V)
The best approach by far would be to design the data better, without any meta-data in the variable names.
Forcing meta-data into variable names makes processing data slow and fragile. Best avoided.
  댓글 수: 4
Stephen23
Stephen23 2025년 2월 28일
P = '.'; % uigetdir();
S = dir(fullfile(P,'@*.mat'));
for k = 1:numel(S)
F = fullfile(P,S(k).name);
D = load(F);
C = struct2cell(D);
S(k).meta = fieldnames(D);
S(k).data = vertcat(C{:});
end
C = vertcat(S.meta)
C = 16x1 cell array
{'VSBART' } {'VSBART_RT' } {'VSBART_RT_PD'} {'VSBART_RT_PU'} {'VSFDC' } {'VSPD' } {'VS_RATE' } {'VS_RATEPH' } {'VSBART' } {'VSBART_RT' } {'VSBART_RT_PD'} {'VSBART_RT_PU'} {'VSFDC' } {'VSPD' } {'VS_RATE' } {'VS_RATEPH' }
D = vertcat(S.data)
D = 16x1 struct array with fields:
Channelname Channeltype unit timeunit period device time signals min max
X = strcmpi({D.Channelname},'VSPD');
Y = [D(X).signals];
Z = vertcat(Y.values)
Z = 45110×1
0 0 0 0 0 0 0 0 0 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Rodrigo
Rodrigo 2025년 3월 5일
great, thanks a lot

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Import and Analysis에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by