Work with variable names in workspace that are mostly the same with a unique identifier at the end
1 view (last 30 days)
Show older comments
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 Comments
Jeff Miller
on 23 Sep 2022
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
Accepted Answer
Chunru
on 23 Sep 2022
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 Comments
More Answers (0)
See Also
Categories
Find more on Data Type Conversion in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!