Workspace variable not available despite visibility in another variable

3 次查看(过去 30 天)
Hi Together,
I have the problem that some signals disappear in workspace, when some signals of measurement files are renamded to variables, which I use in MATLAB code.
Here is the code:
if(exist('Testsignal_1','var'))
Current=Testsignal_1;
end
keepvars(end+1) = {'Current'};
After Run the variable Current and Testsignal_1 disappear. But in variable keepvars the signal Current is still avaible.
Furthermore I do this for other signals and for them, the variables are avaible in the workspace.
Signals and variables only occur once in the script.
I got also no failure in Command Window.
Do you have an idea, why have only for two signals this problem?

采纳的回答

VBBV
VBBV 2024-7-30
编辑:VBBV 2024-7-30
if you want to store the variable names Testsignal_1 and Current then you need to use an indexing variable as below. If this code snippet is present in a bigger code structure which is repeated several times, then the index helps to store the variable names and signals for each run/ iteration. In your code, the name of the signal and variables are not character vectors but defined by a value.
If the if-end condition is not satisfied, then the signal and variable names will NOT appear in workspace. The below code will store both signal and variable names and would appear in workspace if the condition is satisfied.
k = 1; % iterator index
if(exist('Testsignal_1','var'))
Current{k}='Testsignal_1'; % signal name as character vector stored in cell array
k = k+1; % increment
end
keepvars(end+1) = Current; % store or assign the cell array to keepvars data array present in bigger code structure
  1 个评论
FD
FD 2024-7-30
编辑:FD 2024-7-30
Thank you for your fast reply.
The code is working but created a cell array.
But good news I found the failure in my code, so now it works :-)

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Whos 的更多信息

产品


版本

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by