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 个评论
Michael Wyciechowski
Thank you for your thoughts. I'm still struggling a bit, not really sure how to utilize the "contains" function here because it seems to require a string or char as an input, which I'd have to create based on the variable name. Maybe I'll include more information. As you can see I've cheated a bit in the code by including the t143 identifier, just so it would run. This would be a manual operation which I'm trying to avoid. You can see this if you try to run the same code for SampleData2.mat, which has t145 and PressureSignal_t145. What would it take to simply plot the pressure signals in each of the 2 sample files, based only on the text "PressureSignal".
% Sample m-file
clear;clc;
load('SampleData1.mat');
% load('SampleData2.mat');
str = getVarName(PressureSignal_t143);
TF = contains(str,'PressureSignal')
TF = logical
1
plot(PressureSignal_t143)
function out = getVarName(var)
out = inputname(1);
end
Jeff Miller
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
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 个)

类别

Help CenterFile Exchange 中查找有关 Data Type Conversion 的更多信息

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by