Work with variable names in workspace that are mostly the same with a unique identifier at the end
1 次查看(过去 30 天)
显示 更早的评论
I have a data aquisition system that will automatially convert to a .mat file. However when it does it places a unique identifier based on the unique time stamps associated with that signal.
For example:
PressureSignal_t143 is associated with another variable with a set of time stamps called t143,
Unfortunately the way the logger collected the data this changes with amost every log file. So the next file might be
PressureSignal_t152 and is associated with another variable with a set of time stamps called t152
While its easy to loop through files and run analyis, I've struggled with the constantly changing variable names.
With 20 or so variables and 50 or so files things could get pretty messy.
Any help is greatly appriciated.
Some of the code I've tried:
load(filename, '-regexp', '^PressureSignal')
w = who('-regexp','PressureSignal');
wname = cell2str(w)
tid = wname(end-3:end)
3 个评论
Jeff Miller
2022-9-23
Use
a = load('SampleData1.mat');
varNames = fieldnames(a);
to get the variable names as strings. Then you can write something like:
if contains(varNames{1},'PressureSignal')
plot(a.(varNames{1}))
end
采纳的回答
Chunru
2022-9-23
clear;clc;
%s = load('SampleData1.mat'); % load as a struct
s = load('SampleData2.mat');
% figure out the varname
sf = fields(s);
varname = sf{contains(sf, 'PressureSignal')};
% retrive the variable
P = s.(varname);
plot(P)
title(varname)
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Type Conversion 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!