Extract data using variable names
34 ビュー (過去 30 日間)
古いコメントを表示
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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1825944/image.png)
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;
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1825945/image.png)
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
0 件のコメント
採用された回答
Stephen23
2025 年 2 月 12 日 17:13
編集済み: Stephen23
2025 年 2 月 12 日 20:42
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)
D = vertcat(S.data)
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.
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Manage Products についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!